Python Forum
How to read file inside class - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to read file inside class (/thread-26450.html)

Pages: 1 2


How to read file inside class - Mekala - May-02-2020

Hi,
I have below code, I am passing the file path and I want to read data. I use the below code, but it's not reading anything.

import pandas as pd
import os
class pythonTraining():    
    def mydata(self, filepath):
        Path = "D:\Data"
        print(os.path.join(Path, filepath, "outsummary.csv"))
        full_filepath = os.path.join(Path, filepath, "outsummary.csv")      
        #print("The full_filepath is: "+str(full_filepath))
        mydata = pd.read_csv(full_filepath)
x = pythonTraining()
x.mydata("PythonCodes")
Kindly help, how to modify my script


RE: How to read file inside class - buran - May-02-2020

something within these lines
import pandas as pd
import os
class PythonTraining():    
    def read_data(self, filepath):
        base_path = "D:\Data" 
        full_filepath = os.path.join(base_ath, filepath, "outsummary.csv")      
        self.data = pd.read_csv(full_filepath)
x = PythonTraining()
x.read_data("PythonCodes")
print(x.data.head())
the other options is read_data() to return the dataframe and you to assign it to a name.
As a side note - do you really think it's a good idea to hardcode the base_path and the csv file name? why not pass the full path to read_data method instead?


RE: How to read file inside class - Mekala - May-02-2020

Thanks, it works, but why the "data" is not stored in the data variable (it just prints in the console). How can I store the data in the data variable? my original code also reads data, but I can not see it in the my_data variable. I just only do not want to print it out, in fact I want to store the data in some variable.


RE: How to read file inside class - ndc85430 - May-02-2020

What do you mean? On line 7, the code creates an instance variable called data, which stores the result of pd.read_csv. If that variable didn't exist, the printing on line 10 wouldn't work.


RE: How to read file inside class - Mekala - May-02-2020

I meant, how can I able to store and can be able to see "data" variable in the variable explorer? is it possible?


RE: How to read file inside class - buran - May-02-2020

x is instance of your class. it has instance attribute data. you can work with x.data as with any other name any way you like. I just print it, so that you can see it reads and data is accessible.

alternative is your method to return what it reads
import pandas as pd
import os
class PythonTraining():    
    def read_data(self, filepath):
        base_path = "D:\Data" 
        full_filepath = os.path.join(base_ath, filepath, "outsummary.csv")      
        return pd.read_csv(full_filepath)
x = PythonTraining()
data = read_data("PythonCodes")
print(data.head())



RE: How to read file inside class - buran - May-02-2020

(May-02-2020, 09:21 AM)Mekala Wrote: variable explorer
what is variable explorer? that is something from your IDE, not the language itself, so we don't know. With my code (not yours) probably you can look at x variable and it should also display its attributes.

Note that this feature if the IDE is something that helps to debug, it's not meant to be used as tool to work with your variables.


RE: How to read file inside class - Mekala - May-02-2020

Sorry, I am using "Spyder". The data is printing in console, but I can not see this variable in variable explorer space. Sorry to confuse you.


RE: How to read file inside class - buran - May-02-2020

I am not using Spyder, so probably someone else would step in.


RE: How to read file inside class - Mekala - May-02-2020

That's fine, one more thing, I want to pass this variable to another, I use the below code, but it gives an error that data is undefined. how we pass variables to one instance to another.
I calculate mean of column "s3" in data. "self.xmean = data['s3'].mean() + v1"

import pandas as pd
import os
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)
        return self.data
    def cal_rowmean(self, v1):         
        self.xmean = data['s3'].mean() + v1
        print(self.xmean)
        
x = PythonTraining()
x.read_data("PythonCodes")
x.cal_rowmean(10)
print(x.data.head())