Link to home
Start Free TrialLog in
Avatar of João serras-pereira
João serras-pereiraFlag 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?


Avatar of gelonida
gelonida
Flag of France image

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
Avatar of João serras-pereira

ASKER

will do!!!!!!!
ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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!!!!
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



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

BTW - I am using PyCharm + TKinter

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

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