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
#1
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
Reply
#2
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?
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
#3
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.
Reply
#4
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.
Reply
#5
I meant, how can I able to store and can be able to see "data" variable in the variable explorer? is it possible?
Reply
#6
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())
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
#7
(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.
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
#8
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.
Reply
#9
I am not using Spyder, so probably someone else would step in.
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
#10
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())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Solved] Using readlines to read data file and sum columns Laplace12 4 91 9 hours ago
Last Post: Laplace12
  Read file, reformat and write new file bryanmartin113 1 201 Jun-08-2021, 09:27 PM
Last Post: Larz60+
  Read/Write binary file deanhystad 3 533 Feb-01-2021, 10:29 AM
Last Post: Larz60+
  how to change the range of read CSV file every time python file runs greenpine 6 597 Dec-08-2020, 10:11 PM
Last Post: greenpine
  How to read CSV file one row at the time in a range and some delay in between greenpine 2 679 Nov-20-2020, 02:26 PM
Last Post: greenpine
  can't read QRcode in large file simoneek 0 487 Sep-16-2020, 08:52 AM
Last Post: simoneek
  Practice problem using lambda inside the class jagasrik 3 528 Sep-12-2020, 03:18 PM
Last Post: deanhystad
  Binary File Read Aussie 6 1,577 Sep-03-2020, 03:57 AM
Last Post: deanhystad
  Read strings and numbers in columns from a file suvadip 4 887 Aug-11-2020, 09:37 PM
Last Post: suvadip
  read a parameter after updating it in another class MKS2020 3 735 Jul-25-2020, 08:27 AM
Last Post: MKS2020

Forum Jump:

User Panel Messages

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