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: csv.DictReader.fieldnames interprets unicode as ascii
Type: compile error Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: serhiy.storchaka, sparan
Priority: normal Keywords:

Created on 2020-03-06 13:41 by sparan, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg363506 - (view) Author: Andreas Spar (sparan) Date: 2020-03-06 13:41
with open(filename, "rt") as csvfile:
        csv_reader = csv.DictReader(csvfile, delimiter=csv_delimiter)
        filednames = csv_reader.fieldnames

In Python 3.8 csv expects utf-8 encoded files but apperently doens't read the header with utf-8 format.
If the csv file has an header named 'Französisch' it will be saved as 'Französisch'.
msg363522 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-03-06 15:14
csv.DictReader does not work with encodings. It works with already decoded strings.

You have to specify the correct encoding in open() (and "t" in mode is ignored):

with open(filename, "r", encoding="utf-8") as csvfile:

By default open() uses locale encoding. It is likely 'cp1252' on American and Western European Windows.
History
Date User Action Args
2022-04-11 14:59:27adminsetgithub: 84057
2020-03-06 15:14:24serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg363522

resolution: not a bug
stage: resolved
2020-03-06 13:41:31sparancreate