[英]Python ASCII codec can't encode character error during write to CSV
I'm not entirely sure what I need to do about this error.我不完全确定我需要对这个错误做些什么。 I assumed that it had to do with needing to add .encode('utf-8').我认为这与需要添加 .encode('utf-8') 有关。 But I'm not entirely sure if that's what I need to do, nor where I should apply this.但我不完全确定这是否是我需要做的,也不知道我应该在哪里应用它。
The error is:错误是:
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)
This is the base of my python script.这是我的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 library is broken. Python 2.x CSV 库已损坏。 You have three options.你有三个选择。 In order of complexity:按复杂程度排序:
Edit: See below
Use the fixed library
https://github.com/jdunck/python-unicodecsv (
编辑:见下文
pip install unicodecsv
).
使用固定库
https://github.com/jdunck/python-unicodecsv (
pip install unicodecsv
)。
Use as a drop-in replacement - Example:
用作替代品 - 示例:
with open("myfile.csv", 'rb') as my_file: r = unicodecsv.DictReader(my_file, encoding='utf-8')
Read the CSV manual regarding Unicode: https://docs.python.org/2/library/csv.html (See examples at the bottom)阅读有关 Unicode 的 CSV 手册: https : //docs.python.org/2/library/csv.html (请参阅底部的示例)
Manually encode each item as UTF-8:将每个项目手动编码为 UTF-8:
for cell in row.findAll('td'): text = cell.text.replace('[','').replace(']','') list_of_cells.append(text.encode("utf-8"))
Edit, I found python-unicodecsv is also broken when reading UTF-16 .编辑,我发现 python-unicodecsv 在读取 UTF-16 时也坏了。 It complains about any 0x00
bytes.它抱怨任何0x00
字节。
Instead, use https://github.com/ryanhiebert/backports.csv , which more closely resembles Python 3 implementation and uses io
module..相反,使用https://github.com/ryanhiebert/backports.csv ,它更类似于 Python 3 实现并使用io
模块。
Install:安装:
pip install backports.csv
Usage:用法:
from backports import csv
import io
with io.open(filename, encoding='utf-8') as f:
r = csv.reader(f):
The issue lies with the csv library in python 2. From the unicodecsv project page问题在于 python 2 中的 csv 库。来自 unicodecsv项目页面
Python 2's csv module doesn't easily deal with unicode strings, leading to the dreaded “'ascii' codec can't encode characters in position …” exception. Python 2 的 csv 模块不能轻松处理 unicode 字符串,导致可怕的“'ascii' codec can't encode characters in position ...”异常。
If you can, just install unicodecsv如果可以,只需安装 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.