2016-10-11 5 views
1

私はcsvにいくつかのウェブスクラップ情報を出力しようとしています。私はそれが私が期待どおりに出てくる画面にそれを印刷するときに私はcsvに出力すると、それはすべての文字の間にカンマを持っています。私はばかげていますが、何が欠けていますか?CommSas in BeautifulSoup csv

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() 

EDIT:

は、ここに私の関連するPythonのコードですが追加されました出力例

印刷すると、list_of_rowsは次のように出てくる:

[U "130クイーンズゲート" 、u'Hotel 41 '、u'Egerton House Hotel'、u'The マイルストーンホテル、ビューモント '、ハコイン'、ウタタ51 B u'The Gering '、u'Haymarket Hotel'、u'AmbaホテルCharing Cross '、u'Rosewood London'、u'Coventガーデンホテル '、' uckingham Gate Suites and Residences '、ザモンタギューオンザ ガーデンズ、 、u'The Soho Hotel '、u'The Sohoホテル'、u'Fhe Seasons Hotel ロンドンのPark Lane 'には、レストラン、バーまたはラウンジ、コーヒーショップ/カフェがあります。 、u'The Nadler Soho '、u'Charlotte Street Hotel'、 u'The Ritz London '、'ナドラービクトリア '、u'Bulgari Hotel、ロンドン' u "Brown's Hotel"、u'The Arch London '、u'The Piccadilly London West 終わり、スタッフォードロンドン'、u'Ham Yard Hotel '、ロンドンロンドンSt James'、u'Staybridge Suites London - Vauxhall ']

しかし、csvに送信されると、各文字/スペースの間にカンマがあります。

+0

'list_of_rows'は通常のリストですか、最後にネストされたリストですか? – roganjosh

+0

'list_of_rows'には何が含まれているかの例を追加できますか? – mfitzp

+0

'list_of_rows'の詳細な例が役立つと思います。 – Acepcs

答えて

0

データをネストされたリストとして指定する必要があります。各サブリストは単一の行を表します。

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()