【发布时间】:2020-05-24 02:10:55
【问题描述】:
output_file = open(OUTPUT_FILENAME,'w',newline='') #create new file
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
#some logic
for row in items:
#di dictionary
dict_writer.writerow(di)
你好,我是 python 新手。我在 Linux (centos) 上创建了这个脚本,我运行了它,它运行良好, 我尝试在 Windows 上运行它,但出现此错误
Traceback (most recent call last):
File "C:\Users\user157\Desktop\test.py", line 180, in <module>
dict_writer.writerow(di)
File "C:\Users\user157\AppData\Local\Programs\Python\Python38-32\lib\csv.py", line 154, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "C:\Users\user157\AppData\Local\Programs\Python\Python38-32\lib\encodings\cp1256.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xe3' in position 33: character maps to <undefined>
我在写字典之前尝试过使用解决,但还是一样的错误
for k,v in di.items():
try:
di[k] =v.encode().decode('utf-8')
except:
pass
我在 centos 上有 python 3.7.5,在 windows 上有 3.8.2
【问题讨论】:
标签: python python-3.x