您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在写入CSV期间,Python ASCII编解码器无法编码字符错误

在写入CSV期间,Python ASCII编解码器无法编码字符错误

Python 2.x CSV库已损坏。您有三个选择。按照复杂度的顺序:

编辑:请参见下文 使用固定库 https://github.com/jdunck/python-unicodecsvpip 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):
python 2022/1/1 18:45:34 有550人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶