2016-10-12 58 views
1

我捕獲數據,並存儲爲beautifulsoup出口從場以csv

data.append(newdata) 

每個完整的記錄,然後保存爲:

list_of_rows.append(data) 

我然後試圖保存到一個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) 
outfile.close() 

但是,當我加載到csv中時,所有東西都被保存到第一個字段中。我如何正確分解它?

編輯

每一行看起來像

[[u'Staybridge Suites London - Vauxhall', '\nTushar K\n', '\nIlford\n', 0, u'2 reviews', '5 of 5 stars', '29 September 2016', u'\nHome comes at staybridge........it nice with stay bridge.....awesome ambiance, kitchen, rooms, break fast area.............\nEverything is at place.....\nTalking about people of stay bridge... they all are very much cooperative, kind, best service people I have ever saw, meet...... its absolutely fantastic with stay bridge..........love u guysss.....\n']]

+0

你爲什麼以二進制方式打開文件?不相關,但你可以使用'writer.writerows(data_mod)' –

+0

我不知道我是在二進制打開 - 該行應該是什麼? 'writer.writerows(data_mod)'仍然不允許我在單獨的字段中加載到csv中;一切都進入第一列 – HenryM

+0

''wb「'b->二進制模式,你只需要'w'。 'data_mod'看起來像什麼? –

回答

1

保鮮膜別在列表中的行,你已經有一個列表的列表。您還需要剝離或換行每行會得到它自己的路線,而不是寫在同一個排和編碼成UTF-8:

with open("./hotels.csv", "w") as outfile: 
    writer = csv.writer(outfile) 
    writer.writerows([s.strip().encode("utf-8") if isinstance(s, unicode) else s 
        for s in row] for row in list_of_rows) 
+0

我剛剛嘗試過這一點,我得到'UnicodeDecodeError:'ascii'編解碼器無法解碼位置1中的字節0xc3:當編寫csv文件時,序號不在範圍(128)內。 – HenryM

+0

編輯應該起作用,你從哪裏得到這些數據?你有unicode,str和整數混在一起。 –

+0

我把它從網頁上刮掉 - tripadviser – HenryM