【发布时间】:2016-01-01 14:07:20
【问题描述】:
我不完全确定我需要对这个错误做些什么。我认为这与需要添加 .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 csv url utf-8 beautifulsoup