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

时间:2011-11-16 14:49:32

标签: python csv beautifulsoup mechanize

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

\2013应该是

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

UnicodeEncodeError:'ascii'编解码器无法对位置3中的字符u'\ u2013'进行编码:序数不在范围内(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()

1 个答案:

答案 0 :(得分:0)

也许你可以试试:

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

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

your_var.decode('utf-8')