[英]Python: UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 0: ordinal not in range(128) -> Excel
I am trying to wrap my head around this for a while now and have not yet seen any solution that does not confuse me. 我正在努力解决这个问题,并且还没有看到任何可以使我感到困惑的解决方案。
I got a script in python that should write an array with words (German Names) into an excel file. 我在python中获得了一个脚本,该脚本应将带有单词(德语名称)的数组写入excel文件。
cell = [name_1, name_2, name_3]
import csv
fl = open('company_data.csv', 'w')
writer = csv.writer(fl)
writer.writerow(['Name_1', 'Name_2', 'Name_3'])
for values in cell:
writer.writerow(values)
fl.close()
The error that comes is ...,line 135, in writer.writerow(values) UnicodeEncodeError: 'ascii' codec can't encode character u'\\xfc' in position 0: ordinal not in range(128) [Finished in 1.2s with exit code 1] 出现的错误是...,第135行,在writer.writerow(values)中UnicodeEncodeError:'ascii'编解码器无法在位置0编码字符u'\\ xfc':序数不在range(128)中[在1.2中完成s,退出代码为1]
The names include the German characters ü,ä,ö etc. 名称包括德语字符ü,ä,ö等。
How do I fix this? 我该如何解决?
I think you have to open the file and specify that you want to write unicode. 我认为您必须打开文件并指定要编写unicode。 Aussming you want utf-8: 说明您要utf-8:
import codecs
fl = codecs.open("company_data.csv", "w", "utf-8")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.