So I have this file. So my understanding is that DictReader reads the csv file creating a dictionary for each line. The I create the first and last name by calling on name"key" and first, last and house goes again into a dictionary that is appended to the students list. Please let me know if any of the steps I am describing are wrong or why I am getting the error below. Thank you!
import csv
students =[]
with open("before.csv") as file:
reader = csv.DictReader(file)
for row in reader:
first,last = row["name"].strip().split(',')
house = row["house"]
students.append({"first":first, "last":last, "house":row["house"]})
print (students)
$ python before.csv
Traceback (most recent call last):
File "/workspaces/115945517/before.csv", line 1, in <module>
name,house
^^^^
NameError: name 'name' is not defined
>Solution :
When you say
python before.csv
you’re telling python to execute the csv file as a python script. A csv file is just a way of storing data, "comma separated values".
You need to run
python your_script.py
where your_script
is the name of the python file (.py
) that you’re working on.