【问题标题】:Commas in BeautifulSoup csvBeautifulSoup csv中的逗号
【发布时间】:2017-02-19 19:26:39
【问题描述】:

我正在尝试将一些网络抓取信息输出到 csv。当我将它打印到屏幕上时,它会按我的预期出现,但是当我输出到 csv 时,它在每个字符之间都有逗号。我很笨,但我错过了什么?

这是我的相关python代码:

list_of_rows = []
for hotel in hotels:
    for row in hotel.findAll('div', attrs={'class': 'listing_title'}):
        list_of_rows.append(row.find('a').text.replace('\nspb;', ''))

print(list_of_rows)
outfile = open("./hotels.csv", "wb")
writer = csv.writer(outfile)
writer.writerows(list_of_rows)
outfile.close()

编辑:添加示例输出

打印时,list_of_rows 如下所示:

[u"130 Queen's Gate", u'Hotel 41', u'Egerton House Hotel', u'The Milestone Hotel', u'The Beaumont', u'COMO The Halkin', u'Taj 51 Buckingham Gate Suites and Residences', u'The Montague on The Gardens', u'The Goring', u'Haymarket Hotel', u'Amba Hotel Charing Cross', u'Rosewood London', u'Covent Garden Hotel', u'The Connaught', u'The Chesterfield Mayfair',u'The Montcalm London Marble Arch', u'Corinthia Hotel London'、u'The Soho Hotel'、u'四季酒店 London at Park Lane', u'The Nadler Soho', u'Charlotte Street Hotel', u'The Ritz London', u'The Nadler Victoria', u'Bulgari Hotel, London', u“布朗酒店”,u'The Arch London',u'The Piccadilly London West End', u'The Stafford London', u'Ham Yard Hotel', u'Sofitel London St James', u'Staybridge Suites London - Vauxhall']

但是当发送到 csv 时,每个字母/空格之间有一个逗号。

【问题讨论】:

  • list_of_rows到底是一个普通列表还是一个嵌套列表?
  • 你能举个例子说明list_of_rows 包含的内容吗?
  • 我认为list_of_rows 的详细示例会有所帮助。

标签: python csv beautifulsoup


【解决方案1】:

您应该以嵌套列表的形式提供数据,其中每个子列表代表一行。

data_mod = [[item] for item in list_of_rows]

with open("./hotels.csv", "wb") as outfile:
    writer = csv.writer(outfile)
    for row in data_mod:
        writer.writerow(row)

# With writerows() being equivalent to the for loop + writerow()

【讨论】:

    猜你喜欢
    • 2013-11-03
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    相关资源
    最近更新 更多