Do you really have a CSV file, or just a file containing names? If the latter then you don't need to use csv.reader()
, just read the file in the normal way.
If the former then the rows are returned as a list of strings, each string being one of the fields in the file. By default a comma separates the fields. You need to access the individual fields as required. Your code passes the whole row (a list) into the person
constructor, so the value of member name
will be that list, not the name as a string.
Assuming that there is only one field in the CSV file, row[0]
will be that field in the row returned by the CSV reader. You can alter the function to access the first item in the row like this:
def importList():
with open ('attendees3.csv', newline='') as f:
reader = csv.reader(f)
return [person(row[0]) for row in reader]
If you don't have a "real" CSV file, you can read it like this:
def importList():
with open ('attendees3.csv') as f:
return [person(line.strip()) for line in f]
This code simply reads each line, removes any leading and trailing whitespace, and creates person
objects.