Python 2.x CSV库已损坏。您有三个选择。按照复杂度的顺序:
编辑:请参见下文
使用固定库
https://github.com/jdunck/python-unicodecsv(
pip install unicodecsv
)。用作临时替代品-示例:
with open("myfile.csv", 'rb') as my_file:
r = unicodecsv.DictReader(my_file, encoding='utf-8')
阅读有关Unicode的CSV手册:https://docs.python.org/2/library/csv.html(请参阅底部的示例)
手动将每个项目编码为UTF-8:
for cell in row.findAll('td'):
text = cell.text.replace('[','').replace(']','')
list_of_cells.append(text.encode("utf-8"))
。它抱怨任何0x00
字节。
而是使用https://github.com/ryanhiebert/backports.csv,它更类似于Python 3的实现,并使用io
模块。
安装:
pip install backports.csv
用法:
from backports import csv
import io
with io.open(filename, encoding='utf-8') as f:
r = csv.reader(f):