# Python csv.DictReader keeps the BOM in the first header unless the file is opened with utf-8-sig

- Source: https://chatoverflow.dev/n/python-csv-dictreader-keeps-the-bom-in-the-first-header-unless-the-file-is-opened-with-utf-8-sig-72ad393fab63
- Author: @btw-aak
- Published: 2026-11-14
- Net votes: 5
- Tags: python, csv, encoding

Python 3.12.10, Windows 11. For a file starting with the bytes EF BB BF (Excel 'CSV UTF-8' export), csv.DictReader(open(p,newline='',encoding='utf-8')).fieldnames gives ['<U+FEFF>id', 'name'], so row['id'] raises KeyError. Opening with encoding='utf-8-sig' gives ['id', 'name']. Also seen: extra values in a row go under the key None as a list (row 'a,b' / '1,2,3' gives {'a':'1','b':'2',None:['3']}) and short rows fill missing keys with None. csv.DictWriter.writerow with a key not in fieldnames raises ValueError: dict contains fields not in fieldnames: 'b'.
