Avatar of João serras-pereira
João serras-pereira
Flag for Portugal asked on

key error on print csv column on Python

Hi

I am experiencing the error "key error" on my Python code.


The goal is to open a csv file (I am attaching it) with heading and process the file. Thje first row contains field names and then the values on the following rows.


The relevant code is:



# open the table
print("File:[" + selectedFile + "]")
with open(selectedFile, "r") as tableFile:
    reader = csv.DictReader(tableFile)
    for row in reader:
        print(row['villCode'])

Open in new window

and as output, I am having:


ile:[/Users/jsp/Documents/02 GIS/01 GW/01 dbms/02 baseTab/11 tabelão e coesão/01 current/tabelaoBaseData.slim.gpsOK.20211030.05C.csv]
Traceback (most recent call last):
  File "/Users/jsp/Documents/02 GIS/93 myApps/001 getVillageImages/main.py", line 265, in <module>
    print(row['villCode'])
KeyError: 'villCode'

The CSV looks OK.tabelaoBaseData.slim.gpsOK.20211030.05C.csv


can anyone help?


Python* CSV Import

Avatar of undefined
Last Comment
João serras-pereira

8/22/2022 - Mon
gelonida

this seems strange. try following for debugging:

# open the table
print("File:[" + selectedFile + "]")
with open(selectedFile, "r") as tableFile:
    firstline = next(tableFile)
print("First line:", repr(firstline))
with open(selectedFile, "r") as tableFile:
    reader = csv.DictReader(tableFile)
    for row in reader:
        print("Existing keys are:", list(row.keys()))
        print(row['villCode'])

Open in new window

and send the output
João serras-pereira

ASKER
will do!!!!!!!
ASKER CERTIFIED SOLUTION
gelonida

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
João serras-pereira

ASKER
I am getting this:

File:[/Users/jsp/Documents/02 GIS/01 GW/01 dbms/02 baseTab/11 tabelão e coesão/01 current/tabelaoBaseData.slim.gpsOK.20211030.05C.csv]
First line: '\ufeffvillCode,villName,sectorCode,sctorName,villType,vilAccess,vilHabitat,latVillage,longVillage,populacaoTotal,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n'
Traceback (most recent call last):
  File "/Users/jsp/Documents/02 GIS/93 myApps/001 getVillageImages/main.py", line 296, in <module>
    print("Existing keys are:", list(keys(row)))
NameError: name 'keys' is not defined



But the file, open in TextEdit is:

villCode,villName,sectorCode,sctorName,villType,vilAccess,vilHabitat,latVillage,longVillage,populacaoTotal,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1310005,Apresma,131,Quinhamel,rural,limited,single cluster,11.91861,-15.9506,153,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1310010,Sabor Balanta,131,Quinhamel,rural,limited,single cluster,11.93167,-15.7822,187,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1310015,Bidina,131,Quinhamel,rural,permanent,single cluster,11.88972,-15.8869,227,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1310020,Bissa de Cima,131,Quinhamel,rural,permanent,single cluster,11.83944,-15.9119,1622,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1310030,Bissauzinho,131,Quinhamel,rural,limited,single cluster,11.92472,-15.8092,964,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1310040,Ponta Bilimate,131,Quinhamel,rural,limited,single cluster,11.88694,-15.7228,544,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1310050,Blim,131,Quinhamel,rural,permanent,single cluster,11.78222,-15.9283,1511,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1310060,Blom Bijimita,131,Quinhamel,rural,limited,single cluster,11.98806,-15.7808,3714,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1310070,Blom de Ondame,131,Quinhamel,rural,permanent,single cluster,11.79667,-15.9067,3714,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

(I am resending the file)

tabelaoBaseData.slim.gpsOK.20211030.05C.csv

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
João serras-pereira

ASKER
In the end I opted for a slightly different solution - just inserted a dummy column on the specific source. Excel... Therefore if it comes from another source computer, it will still work.
Thanks!!!!
gelonida

If some of the files have a BOM and others don't there's another solution, which you could go for without having to grow the file size by introducing a dummy column.

You open the file and read the first character.

If it is the BOM you continue as usual if it's not the BOM you reset the file read pointer with the seek function

with open(selectedFile, "r") as tableFile:
    first_char = tableFile.read(1)


    if first_char != "\ufeff":  # rewind file if first char was no BOM
        tableFile.seek(0)
    reader = csv.DictReader(tableFile)

Open in new window



João serras-pereira

ASKER
Gee! Thanks!!!!!! will DO!

BTW - I am using PyCharm + TKinter

would you recommend a drag&drop IDE to design my interface?

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
gelonida

Apologies, I don't have a lot of experience with drag&drop IDE's for TKinter, so can't really give solid advice.
João serras-pereira

ASKER
ok!