这是代码,输出为\xef\xbb\xbf\
# -*- coding: utf-8 -*-
import codecs
import csv
import sys
with open(u'csv.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
print row
答案 0 :(得分:0)
python 2 csv
模块不能很好地处理unicode编码。最好的解决方案是开始使用集成了unicode的python 3,非常适合多语言编程。如果您需要坚持使用python 2,unicodecsv
模块(pip install unicodecsv
)是处理非ascii文件的csv
的替代品。
# -*- coding: utf-8 -*-
import sys
try:
import unicodecsv as csv
except ImportError:
sys.stderr.write(
"`sudo pip install unicodecsv` for unicode csv support\n")
exit(1)
with open(u'testfile.csv', "rb") as csvfile:
reader = csv.reader(csvfile, encoding="utf-8-sig")
for row in reader:
print u", ". join(row)