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
  Recommended way to read/create PDF file? Winfried 3 2,785 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,314 Nov-09-2023, 10:56 AM
Last Post: mg24
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 629 Oct-29-2023, 12:40 PM
Last Post: Mark17
  How to read module/class from list of strings? popular_dog 1 424 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  read file txt on my pc to telegram bot api Tupa 0 1,050 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,043 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,161 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,016 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,149 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,501 Feb-18-2023, 02:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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