gpt4 book ai didi

python - 将抓取的数据写入 csv 文件时出错

转载 作者:行者123 更新时间:2023-12-04 16:22:32 25 4
gpt4 key购买 nike

我正在从网站上抓取一些信息,其中一个字段存储在我的列表中,如下所示:[u'Dover Park', u'30 \u2013 38 Dover Rise']\2013应该是 .

尝试写入 .csv 文件时,出现以下错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 3: ordinal not in range(128)。

这是我的代码:

import re
import mechanize
from BeautifulSoup import BeautifulSoup

url = 'http://www.dummy.com'

br = mechanize.Browser()

page = br.open(url)

html = page.read()
html = html.decode('utf-8')
soup = BeautifulSoup(html)

table = soup.find('table', width='800')

property_list = []

for row in table.findAll('tr')[1:]:
for field in row.findAll('td', width='255'):
property_list.append(field.findAll(text=True))

for condo in property_list:
for field in condo:
if field == '  ':
condo.remove(field)

for condo in property_list:
if len(condo) < 2:
condo.append(condo[0])
if condo[1]:
condo[1] = condo[1].replace(',','')

for condo in property_list:
for field in condo:
field = field.encode('utf-8')

import csv

myfile = open('condos.csv', 'wb')
try:
wr = csv.writer(myfile)
wr.writerow(('Name','Address'))
for condo in property_list:
print condo
wr.writerow(condo)
finally:
myfile.close()

最佳答案

也许你可以尝试:

#!/usr/bin/python
# --*-- coding:UTF-8 --*--

import codecs
streamWriter = codecs.lookup('utf-8')[-1]
sys.stdout = streamWriter(sys.stdout)

your_var.decode('utf-8')

关于python - 将抓取的数据写入 csv 文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8153570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com