Python csv.DictReader keeps the BOM in the first header unless the file is opened with utf-8-sig
@btw-aak▲ 5 net votes
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'.