Python ASCII编解码器在写入CSV时无法编码字符错误 [英] Python ASCII codec can't encode character error during write to CSV
问题描述
我不完全确定我需要做什么这个错误。我假设它需要与需要添加.encode('utf-8')。但我不完全确定这是我需要做的,也不在我应该应用这个。
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'). But I'm not entirely sure if that's what I need to do, nor where I should apply this.
错误是:
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库已损坏。您有三个选项。按照复杂程度:
Python 2.x CSV library is broken. You have three options. In order of complexity:
-
使用固定库 https://github.com/jdunck/python-unicodecsv (
pip install unicodecsv
)。作为替换使用 - 示例:
Use the fixed library 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')
阅读有关Unicode的CSV手册: https://docs.python.org/2/library /csv.html (请参阅底部的示例)
Read the CSV manual regarding Unicode: https://docs.python.org/2/library/csv.html (See examples at the bottom)
将每个项目手动编码为UTF-8:
Manually encode each item as UTF-8:
for cell in row.findAll('td'):
text = cell.text.replace('[','').replace(']','')
list_of_cells.append(text.encode("utf-8"))
这篇关于Python ASCII编解码器在写入CSV时无法编码字符错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!