繁体   English   中英

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

[英]Python ASCII codec can't encode character error during write to CSV

我不完全确定我需要对这个错误做些什么。 我认为这与需要添加 .encode('utf-8') 有关。 但我不完全确定这是否是我需要做的,也不知道我应该在哪里应用它。

错误是:

line 40, in <module>
writer.writerows(list_of_rows)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 1
7: ordinal not in range(128)

这是我的python脚本的基础。

import csv
from BeautifulSoup import BeautifulSoup

url = \
'https://dummysite'

response = requests.get(url)

html = response.content

soup = BeautifulSoup(html)

table = soup.find('table', {'class': 'table'})

list_of_rows = []
for row in table.findAll('tr')[1:]:
list_of_cells = []
for cell in row.findAll('td'):
    text = cell.text.replace('[','').replace(']','')
    list_of_cells.append(text)
list_of_rows.append(list_of_cells)

outfile = open("./test.csv", "wb")
writer = csv.writer(outfile)
writer.writerow(["Name", "Location"])
writer.writerows(list_of_rows)

Python 2.x CSV 库已损坏。 你有三个选择。 按复杂程度排序:

  1. 编辑:见下文 使用固定库 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')
  2. \n
\n

  1. 阅读有关 Unicode 的 CSV 手册: https : //docs.python.org/2/library/csv.html (请参阅底部的示例)

  2. 将每个项目手动编码为 UTF-8:

     for cell in row.findAll('td'): text = cell.text.replace('[','').replace(']','') list_of_cells.append(text.encode("utf-8"))

编辑,我发现 python-unicodecsv 在读取 UTF-16 时也坏了 它抱怨任何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 2 中的 csv 库。来自 unicodecsv项目页面

Python 2 的 csv 模块不能轻松处理 unicode 字符串,导致可怕的“'ascii' codec can't encode characters in position ...”异常。

如果可以,只需安装 unicodecsv

pip install unicodecsv

import unicodecsv

writer = unicodecsv.writer(csvfile)
writer.writerow(row)

除了Alastair的出色建议之外,我发现最简单的选择是使用 python3 而不是 python 2。我的脚本中所需的只是根据 Python3 的语法open语句中的wb更改为简单的w

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM