【发布时间】: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