Hi, i'm currently writing some code in python that opens up a .csv file and splits the row data into a class called Students...so far so good!
Then each instance of this class is added to a list -also good.
student_list = []
#opens the csv file, takes out the information and saves them in the Student Class
student_file = open('student_list.csv')
student_file_reader = csv.reader(student_file)
for row in student_file:
row_string = str(row)
row_parts1,row_parts2,row_parts3,row_parts4,row_parts5 = row_string.split(',')
student_list.append(Students(row_parts1,row_parts2,row_parts3,row_parts4,row_parts5))
print (student_list)
however when i try and print student_list i get this back
[<__main__.Students object at 0x000002B6C904D748>, <__main__.Students object at 0x000002B6C904D828>, <__main__.Students object at 0x000002B6C904D908>]
all five elements of the class are strings, and i know the answer lies with a
def __repr__(self):
doohickey but I'm not sure how to phrase it.
Any pointers would be great.
(First-ish post ...I hope this place is friendlier than Stack Overflow)
Ok chaps and chapettes, stand down..I've got this
I changed the code to :
student_list = [] #opens the csv file, takes out the information and saves them in the Student Class student_file = open('student_list.csv') student_file_reader = csv.reader(student_file) for row in student_file: row_string = str(row) row_parts1,row_parts2,row_parts3,row_parts4,row_parts5 = row_string.split(',') student_list.append(Students(row_parts1,row_parts2,row_parts3,row_parts4,row_parts5)) print (student_list[0])streamlining the append to the list by a line, then added to my class the repr bit
class Students(): def __init__(self,student_id, first_name,second_name,student_parent,parent_email): self.student_id = student_id self.first_name = first_name self.second_name = second_name self.student_parent = student_parent self.parent_email = parent_email #def __repr__(self): #return {self.student_id,self.first_name,self.second_name,self.student_parent,self.parent_email} def __repr__(self): return self.student_id + " " + self.first_name + " " +self.second_name + " " + self.student_parent + " " + self.parent_emailnow it prints the information from the the first index of the list.
Huzzah!
Hope this helps someone.