繁体   English   中英

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

[英]Python UnicodeEncodeError involving 'charmap' codec

这段代码以前运行良好,但现在当我尝试将列表写入 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)

尝试添加encoding="utf-8"

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

encoding添加到文件打开

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM