我想将西里尔文符号写入 csv 文件,但出现 unicode 编码错误。英文符号完美无缺。我正在使用 Python 3.6.2。
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-6: ordinal not in range(128)
import csv
with open("test.csv", 'w') as csvfile:
csvfile = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
hello = 'привет, мир!'
csvfile.writerow([hello])
最佳答案
打开文件时声明文件的编码。 newline=''
根据 csv 也是必需的文档。
import csv
with open('test.csv','w',encoding='utf8',newline='') as csvfile:
csvfile = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
hello = 'привет, мир!'
csvfile.writerow([hello])
关于python - 将俄语符号写入 csv 时出现 Unicode 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46870929/