Python 3:CSV utf-8编码 [英] Python 3: CSV utf-8 encoding
本文介绍了Python 3:CSV utf-8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Python 3编写具有非ascii字符的CSV。
I'm trying to write a CSV with non-ascii character using Python 3.
import csv
with open('sample.csv', 'w', newline='', encoding='utf-8') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow("嗨")
当我打开Excel文件时,请参见 å—¨
。我在这里做错什么了吗?
When I open the Excel file, see å—¨
instead. Am I doing something wrong here?
推荐答案
您需要向Excel指出这是UTF-8文件;
You need to indicate to Excel that this is a UTF-8 file; it won't assume so automatically.
您可以通过放置文件开头的字节顺序标记(BOM):
with open('sample.csv', 'w', newline='', encoding='utf-8') as csvfile:
csvfile.write('\ufeff')
这篇关于Python 3:CSV utf-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文