涉及'charmap'编解码器的Python UnicodeEncodeError

时间:2019-12-24 12:10:17

标签: python csv

此代码之前工作正常,但是现在当我尝试将列表写入csv文件时,出现此错误-

File "C:/Users/wf5931/OneDrive - ENGIE/Documents/Python Scripts/Scrape Vehicle Reg Info/vehicleRegChecker 6.1.py", line 109, in openFile
    writer.writerow(x)

File "C:\Users\wf5931\AppData\Local\Continuum\anaconda3\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]

UnicodeEncodeError: 'charmap' codec can't encode character '\u2082' in position 78: character maps to <undefined

从这里:

with open(vehicleRegInformation, 'w', newline='') as f:
    writer = csv.writer(f)
    for x in vehicleRegInfo:
        writer.writerow(x)

2 个答案:

答案 0 :(得分:2)

尝试添加encoding="utf-8"

with open(vehicleRegInformation, 'w', newline='',encoding="utf-8") as f:
writer = csv.writer(f)
for x in vehicleRegInfo:
    writer.writerow(x)

答案 1 :(得分:2)

encoding添加到文件开头

with open(vehicleRegInformation, 'w', newline='', encoding='utf8') as f: