BeautifulSoup csv中的逗号
问题:BeautifulSoup csv中的逗号
我正在尝试将一些网络抓取信息输出到 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'Four Seasons Hotel London at Park Lane', u'The Nadler Soho', u'夏洛特街酒店',u'伦敦丽兹酒店',u'The Nadler Victoria',u'伦敦宝格丽酒店',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 时,每个字母/空格之间都有一个逗号。
解答
您应该以嵌套列表的形式提供数据,其中每个子列表代表一行。
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()
更多推荐

所有评论(0)