Skip to content

Instantly share code, notes, and snippets.

@palewire
Last active September 7, 2016 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palewire/23765d7349fb40c6edded67f8effea4b to your computer and use it in GitHub Desktop.
Save palewire/23765d7349fb40c6edded67f8effea4b to your computer and use it in GitHub Desktop.
first_name last_name
ben welsh
peter jamison
import csv
class Person(object):
"""
A human.
"""
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
def __str__(self):
return "{} {}".format(self.first_name, self.last_name)
# Open the file
file_obj = open("./data.csv", "r")
# Have the csv module parse it
data_list = csv.DictReader(file_obj)
# Create an empty list for the person objects
person_list = []
# Loop through the csv data
for row in data_list:
# Create a new person object
person_obj = Person(row['first_name'], row['last_name'])
# Add it to our object list
person_list.append(person_obj)
# Loop through that object list
for obj in person_list:
# Do stuff, like print it out.
print(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment