This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python dictreader KeyError issue
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eorochena, eric.smith, steven.daprano
Priority: normal Keywords:

Created on 2018-12-31 10:53 by eorochena, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg332810 - (view) Author: Eduardo Orochena (eorochena) Date: 2018-12-31 10:53
def load_file(filename):

    with open(filename, 'r', encoding='utf-8') as fin:
        header = fin.readline()
        print('Found ' + header)

        reader = csv.DictReader(fin)

        for row in reader:
            print(type(row), row)
            print('Beds {} '.format(row['beds']))

This results in a KeyError exception

whilst 

open_f = open(filename, 'r', encoding='utf-8')

    read_it = csv.DictReader(open_f)

    for i in read_it:
        print('Beds {}'.format(i['beds']))

behaves as expected
msg332811 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2018-12-31 11:05
Can you please provide a *simple* and *complete* demonstration, including the *full* traceback? As given, we cannot run the supplied code and don't know what the contents of the csv file are supposed to be.

See here for more detail: http://www.sscce.org/

But my *guess*, on reading this, is that the line 

    header = fin.readline()

causes the difference. In the first sample, you skip past the first line of the csv file, and so when the dictreader reads the rest of the file, it never sees the first row. But in the second example, it does see the first row.

What happens if you change the first example to this?

        header = fin.readline()
        print('Found ' + header)
        fin.seek(0)
        reader = csv.DictReader(fin)

Does that solve your problem?

Likewise, in the second case, if you change it to this:

    open_f = open(filename, 'r', encoding='utf-8')
    __ = open_f.readline()
    read_it = csv.DictReader(open_f)


what happens?
msg332831 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-12-31 20:34
Steven is correct: your problem is that in the first example you're reading the header row before you pass the file to DictReader, so the DictReader cannot know what your columns are named. (Actually, your code uses the second row of your file as the column names: you should have been able to determine this by looking at the value of "row" that was printed).

I'm going to close this issue. Please do some more investigation. If you can produce a short example that we can execute ourselves, including any needed data files, and you think it still shows an error, please feel free to reopen this issue.
History
Date User Action Args
2022-04-11 14:59:09adminsetgithub: 79807
2018-12-31 20:34:16eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg332831

resolution: not a bug
stage: resolved
2018-12-31 11:05:46steven.dapranosetnosy: + steven.daprano
messages: + msg332811

components: + Library (Lib), - Build
type: crash -> behavior
2018-12-31 10:53:23eorochenacreate