2016-11-15 60 views
4

我試圖從TripAdvisor吸取500家餐廳的列表;然而,第308餐廳後,我收到以下錯誤:UnicodeEncodeError:'ascii'編解碼器無法編碼字符u' u2019'在位置6:序號不在範圍內(128)

Traceback (most recent call last): 
    File "C:/Users/dtrinh/PycharmProjects/TripAdvisorData/LinkPull-HK.py", line 43, in <module> 
    writer.writerow(rest_array) 
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 6: ordinal not in range(128) 

我試過幾件事情我在計算器上找到,但沒有什麼工作截至目前。我想知道是否有人可以看看我的代碼,並看到任何可能的解決方案。

 for item in soup2.findAll('div', attrs={'class', 'title'}): 
      if 'Cuisine' in item.text: 
       item.text.strip() 
       content = item.findNext('div', attrs=('class', 'content')) 
       cuisine_type = content.text.encode('utf8', 'ignore').strip().split(r'\xa0') 
     rest_array = [account_name, rest_address, postcode, phonenumber, cuisine_type] 
     #print rest_array 
     with open('ListingsPull-Amsterdam.csv', 'a') as file: 
       writer = csv.writer(file) 
       writer.writerow(rest_array) 
    break 
+0

'cuisine_type'是一個列表,因爲你使用'.split'(我不確定你爲什麼在非休息空間分裂......)。但是,傳遞給'.writerow'的行的內容需要是字符串或數字。另外,如[文檔](https://docs.python.org/2/library/csv.html)中所述,使用Python 2'csv'模塊時,應該以二進制模式打開CSV文件, 。您可能會發現這篇文章有用:[Pragmatic Unicode](http://nedbatchelder.com/text/unipain.html),它由SO老將Ned Batchelder編寫。 –

回答

4

rest_array包含Unicode字符串。當您使用csv.writer來編寫行時,您需要序列化字節字符串(您在Python 2.7中)。

我建議你使用「UTF-8」編碼:

with open('ListingsPull-Amsterdam.csv', mode='a') as fd: 
    writer = csv.writer(fd) 
    rest_array = [text.encode("utf8") for text in rest_array] 
    writer.writerow(rest_array) 

注:請,因爲你的影子內置功能file()open()功能的別名),不要使用file作爲變量。

如果你想用Microsoft Excel打開這個CSV文件,你可以考慮使用另一種編碼,例如「cp1252」(它允許u「\」字符)。

+0

這就像一個魅力!謝謝! – dtrinh

1

您正在向csv輸出文件寫入非ascii字符。確保使用適當的字符編碼打開輸出文件,以允許對字符進行編碼。安全的賭注通常是UTF-8。試試這個:

with open('ListingsPull-Amsterdam.csv', 'a', encoding='utf-8') as file: 
    writer = csv.writer(file) 
    writer.writerow(rest_array) 

編輯這是Python的3.x的,對不起。

+0

我認爲他不會在Python 3上出現這個錯誤。他的問題被標記爲「python-2.7」。 –

+0

你是對的,錯過了 –

相關問題