Sign in
Log inSign up

Python: How to print class objects within a list?

James Novis's photo
James Novis
·Apr 11, 2019

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)

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct