Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NameError: name " " is not defined (new to coding cand figure out why it doesn't work)

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading