Python Forum
How to read file inside class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to read file inside class
#11
self in the class is the instance itself. If you want to use a instance attribute inside the class you need to use it with self.
class PythonTraining():    
    def read_data(self, filepath):
        base_path = "D:\Data" 
        full_filepath = os.path.join(base_path, filepath, "outsummary2.csv")      
        self.data = pd.read_csv(full_filepath)

    def cal_rowmean(self, v1):         
        self.xmean = self.data['s3'].mean() + v1
        print(self.xmean)
Note, that read_data doesn't need to return self.data.
Also, note that you need to call read_data() before you call cal_rowmean or you will get AttributeError. It may be better if you refactor your class and move what reading data in class __init__ method (you don't have defined one now)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#12
You lose flexibility and most of the point of this if hardcore most of the stuff inside the class.
It's no so easy with Pandas as often need flexibility to change a lot of stuff(also when read in files).

Here a demo,where take stuff in from outside of the class.
The can continue in normal way to work with df DataFrame object.
Output:
Movie;Year Seven;1995 The Godfather;1972 Jaws;1975
import pandas as pd
import os

class PythonTraining():
    def __init__(self, base_path, file_name):
        self.base_path = base_path
        self.file_name = file_name

    def read_data(self, sep=';', header='infer'):
        full_filepath = os.path.join(self.base_path, self.file_name)
        self.data = pd.read_csv(full_filepath, sep=sep, header=header)


if __name__ == '__main__':
    # Now from outside give path and filename
    base_path = r'E:\div_code\home'
    file_name = 'movies.csv'
    df = PythonTraining(base_path, file_name)

    # Give paramater to csv read
    sep = ';'
    #header = None
    df.read_data(sep)
    df = df.data
As you use Spyder here a Screenshot,see that i can continue to work with df object in IPython console.
[Image: t4EpwK.png]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,260 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Read TXT file in Pandas and save to Parquet zinho 2 1,355 Sep-15-2024, 06:14 PM
Last Post: zinho
  Pycharm can't read file Genericgamemaker 5 1,716 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 4,154 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,443 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 5,252 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 4,024 Nov-09-2023, 10:56 AM
Last Post: mg24
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 9,387 Oct-29-2023, 12:40 PM
Last Post: Mark17
  How to read module/class from list of strings? popular_dog 1 1,367 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  read file txt on my pc to telegram bot api Tupa 0 2,767 Jul-06-2023, 01:52 AM
Last Post: Tupa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020