python2 csv 编码错误_Python ASCII编解码器无法在写入CSV期间对字符错误进行编码

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

错误是:line 40, in

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值