0

I keep getting a key-error in pset7, Houses:

Traceback (most recent call last):
  File "import.py", line 24, in <module>
    names = cutting(row["name"])
KeyError: 'name'

This key exists in the csv file though, and I did use

reader = csv.DictReader(argv[1], "r")

to open the csv file as a dictionary, so I'm confused as to why it says it doesn't exist. This is my iteration:

for row in reader:
    names = cutting(row["name"])
    house = row["house"]
    birth = row["birth"]
    db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?, ?, ?, ?, ?)",
        names[0], names[1], names[2], house, birth)

where

cutting()

is just:

# FUNCTION TO CUT def cutting(name):
    # SPLIT THE NAME INTO DIFFERENT SEGMENTS BY SPACE
    names = name.split()
    # RETURN AS IS IF THERE IS A MIDDLE NAME
    return names if len(names) == 3 else [names[0], None, names[1]]
1

Program doesn't seem to open the file for reading. Perhaps review the syntax in the doc

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
1
  • can't believe I completely missed this, thank you so much! – Hana Ali Aug 6 '20 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .