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
#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


Messages In This Thread
How to read file inside class - by Mekala - May-02-2020, 06:49 AM
RE: How to read file inside class - by buran - May-02-2020, 07:07 AM
RE: How to read file inside class - by Mekala - May-02-2020, 08:54 AM
RE: How to read file inside class - by ndc85430 - May-02-2020, 08:54 AM
RE: How to read file inside class - by Mekala - May-02-2020, 09:21 AM
RE: How to read file inside class - by buran - May-02-2020, 09:22 AM
RE: How to read file inside class - by buran - May-02-2020, 09:26 AM
RE: How to read file inside class - by Mekala - May-02-2020, 09:29 AM
RE: How to read file inside class - by buran - May-02-2020, 09:31 AM
RE: How to read file inside class - by Mekala - May-02-2020, 09:49 AM
RE: How to read file inside class - by buran - May-02-2020, 10:08 AM
RE: How to read file inside class - by snippsat - May-02-2020, 11:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Read csv file through PyCharm kimx0961 3 113 Yesterday, 07:05 PM
Last Post: perfringo
  Why it does not print(file.read()) Rejaul84 1 246 Jul-01-2021, 10:37 PM
Last Post: bowlofred
  Read and write active Excel file euras 4 370 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
  [Solved] Trying to read specific lines from a file Laplace12 7 521 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  [Solved] Using readlines to read data file and sum columns Laplace12 4 313 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  Read file, reformat and write new file bryanmartin113 1 345 Jun-08-2021, 09:27 PM
Last Post: Larz60+
  Custom file class deanhystad 11 1,225 Feb-01-2021, 05:09 PM
Last Post: nilamo
  Read/Write binary file deanhystad 3 737 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 758 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 1,056 Nov-20-2020, 02:26 PM
Last Post: greenpine

Forum Jump:

User Panel Messages

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