0

I am just learning about classes in python and am having a little trouble understanding them. I have this code that reads a CSV file into a class. An example of some of the class names from the file are Age, Education, Gender, Salary, etc. I am wanting to be able to access and print out everything from say the Age variable. I know I can print a specific element with print(instances[1].Age), but the list has many elements and I would like to be able to access all of them. I am thinking a for loop would work but I am unsure as to how to write it.

 class File:
    def __init__(self, row, header):
        self.__dict__ = dict(zip(header, row))
data = list(csv.reader(open('data_file.csv')))
instances = [File(i, data[0]) for i in data[1:]]

This is what I have to read the CSV file into the class, from here I am having trouble accessing the variables.

Becca
  • 59
  • 1
  • 4
  • are you asked to use **class** in this challenge? coz it will be much easier to use **with open** and for loop. –  Sep 22 '20 at 18:14
  • I can use **with open** but it was stated that using **class** was preferred. I have managed to make it work using the **with open** method but I'm still open to ideas for the other way. – Becca Sep 22 '20 at 19:28

2 Answers2

0

dir() will list everything a module has.

class B:
    x = 5
    def __init__(self):
        print("hello")
        
    def method(self):
        print("world")
        
        
b  = B();
print(dir(B))

objectMethods =  [] #stores methods
objectMemberVars = [] #stores member variables as tuples (name,value)
for attr in dir(b):
    if callable(getattr(b,attr)):
        objectMethods.append(getattr(b,attr)) 
    else:
        objectMemberVars.append((attr,getattr(b,attr)))
        
print(objectMethods)
print(objectMemberVars)
print(objectMethods[1])

output :

hello
['__doc__', '__init__', '__module__', 'method', 'x']
[<bound method B.__init__ of <__main__.B instance at 0x7fc3afa41320>>, <bound method B.method of <__main__.B instance at 0x7fc3afa41320>>]
[('__doc__', None), ('__module__', '__main__'), ('x', 5)]
<bound method B.method of <__main__.B instance at 0x7fc3afa41320>>

You can do this for class definitions and instances. The difference is that methods of class definitions are represented as unbound while methods of instances are bound

I referenced this post: Finding what methods a Python object has

0

I made this answer on the base of the following csv file example

title,name,date,age,feedback

Great Customer Service,John,2017-12-21,25,The customer service here is very good. They helped me find a 2017 Camry with good condition in reasonable price. Compared to other dealers, they provided the lowest price. Definitely recommend!

Since you are not forced to use class, it's much easier to do the job with list.

import csv


with open('C:/Users/user/Desktop/folder/log.csv') as f:

    data = []
    reader = csv.DictReader(f)
    for rows in reader:
      data.append(rows)
    for row in data:
        print(row['age'])

output

25