开启python字典。到Excel工作表

我的以下代码有问题。


import urllib2

import csv

from bs4 import BeautifulSoup

soup = BeautifulSoup(urllib2.urlopen('http://www.ny.com/clubs/nightclubs/index.html').read())

clubs = []

trains = ["A","C","E","1","2","3","4","5","6","7","N","Q","R","L","B","D","F"]

for club in soup.find_all("dt"):

    clubD = {}

    clubD["name"] = club.b.get_text()

    clubD["address"] = club.i.get_text()

    text = club.dd.get_text()

    nIndex = text.find("(")

    if(text[nIndex+1]=="2"):

        clubD["number"] = text[nIndex:nIndex+15]

    sIndex = text.find("Subway")

    sIndexEnd = text.find(".",sIndex)

    if(text[sIndexEnd-1] == "W" or text[sIndexEnd -1] == "E"):

        sIndexEnd2 = text.find(".",sIndexEnd+1)

        clubD["Subway"] = text[sIndex:sIndexEnd2]

    else:

        clubD["Subway"] = text[sIndex:sIndexEnd]

    try:

        cool = clubD["number"]

    except (ValueError,KeyError):

        clubD["number"] = "N/A"

    clubs.append(clubD)

keys = [u"name", u"address",u"number",u"Subway"]

f = open('club.csv', 'wb')

dict_writer = csv.DictWriter(f, keys)

dict_writer.writerow([unicode(s).encode("utf-8") for s in clubs])

我收到错误ValueError:dict包含不在字段名称中的字段。我不明白这怎么可能。任何帮助将是巨大的。我正在尝试将字典转换为Excel文件。


素胚勾勒不出你
浏览 144回答 1
1回答

小唯快跑啊

clubs是词典列表,而每个词典都有四个字段:名称,地址,编号和Subway。您将需要对每个字段进行编码:# Instead of:#dict_writer.writerow([unicode(s).encode("utf-8") for s in clubs])# Do this:for c in clubs:    # Encode each field: name, address, ...    for k in c.keys():        c[k] = c[k].encode('utf-8').strip()    # Write to file    dict_writer.writerow(c)更新我查看了您的数据,其中一些字段以结尾换行\n,因此我更新了代码以同时编码和去除空格。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python