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.