Python的ASCII codeC无法连接code字符错误写在CSV [英] Python ASCII codec can't encode character error during write to CSV
问题描述
我不完全知道我需要做的这个错误。我认为它与需要添加.EN code做(UTF-8)。但我不能完全肯定,如果这就是我需要做的,也不是我应该应用它。
的错误是:
40行,上述<&模块GT;
writer.writerows(list_of_rows)
UNI $ C $岑codeError:ASCIIcodeC无法连接code字符U'\\ u2013'在位置1
7:顺序不在范围内(128)
这是我的python脚本的基础。
导入CSV
从BeautifulSoup进口BeautifulSoupURL = \\
https://开头dummysite响应= requests.get(URL)HTML = response.content汤= BeautifulSoup(HTML)表= soup.find('表',{'类':'表'})list_of_rows = []
在table.findAll('TR')行[1:]:
list_of_cells = []
在row.findAll('TD')电池:
文字= cell.text.replace('[','')。REPLACE(']','')
list_of_cells.append(文本)
list_of_rows.append(list_of_cells)OUTFILE =打开(./ test.csv,WB)
作家= csv.writer(OUTFILE)
writer.writerow([名称,位置])
writer.writerows(list_of_rows)
的Python 2.x的CSV库是坏了。你有三个选择。在复杂的顺序:
-
使用固定库 https://github.com/jdunck/python -uni codeCSV (
点子安装UNI codeCSV
)。使用作为一个简易替换 - 例如:开放(myfile.csv,RB)为my_file:
R = UNI codecsv.DictReader(my_file,编码=UTF-8) -
阅读有关统一code中的CSV手册: https://开头docs.python.org/2/library/csv.html~~V (见底部例子)
-
手动连接code每个项目为UTF-8:
在row.findAll('TD')电池:
文字= cell.text.replace('[','')。REPLACE(']','')
list_of_cells.append(text.en code(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.
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.
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. You have three options. In order of complexity:
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')
Read the CSV manual regarding Unicode: https://docs.python.org/2/library/csv.html (See examples at the bottom)
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 codeC无法连接code字符错误写在CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!