2014-02-28 49 views
2

我需要一些列表編碼的幫助。我是新來的蟒蛇,對不起。我使用Python 2.7.3python編碼CSV列表

我有兩個列表(entidad & valores),我需要得到它們的編碼或其他東西。

我的代碼:

import urllib 
from bs4 import BeautifulSoup 
import csv 

sock = urllib.urlopen("http://www.fatm.com.es/Datos_Equipo.asp?Cod=01HU0010") 
htmlSource = sock.read() 
sock.close() 
soup = BeautifulSoup(htmlSource) 

form = soup.find("form", {'id': "FORM1"}) 
table = form.find("table") 

entidad = [item.text.strip() for item in table.find_all('td')] 

valores = [item.get('value') for item in form.find_all('input')] 
valores.remove('Imprimir') 
valores.remove('Cerrar') 
header = entidad 
values = valores 

print values 

out = open('tomate.csv', 'w') 

w = csv.writer(out) 
w.writerow(header) 
w.writerow(values) 
out.close() 

日誌:UnicodeEncodeError: 'ASCII' 編解碼器不能編碼字符

什麼想法?提前致謝!!

+1

你谷歌你的錯誤信息?這裏是最重要的結果:http://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20 – amos

+0

我總是谷歌這個簡單的事情,但我不能'很好理解,對不起。 – juasmilla

回答

2

您應該編碼您的數據爲UTF-8手動,csv.writer做這一切並不是你:

w.writerow([s.encode("utf-8") for s in header]) 
w.writerow([s.encode("utf-8") for s in values]) 
#w.writerow(header) 
#w.writerow(values) 
+1

非常感謝!非常有幫助! ;) – juasmilla

0

已經有人在這裏發現UnicodeEncodeError in csv writer in Python

UnicodeEncodeError in csv writer in Python
Today I was writing a program that generates a csv file after some processing. But I got the following error while trying on some test data:

writer.writerow(csv_li) UnicodeEncodeError: 'ascii' codec can't encode character u'\xbf' in position 5: ordinal not in range(128)

I looked into the documentation of csv module in Python and found a class named UnicodeWriter. So I changed my code to

writer = UnicodeWriter(open("filename.csv", "wb"))

Then I tried to run it again. It got rid of the previous UnicodeEncodeError but got into another error.

self.writer.writerow([s.encode("utf-8") for s in row]) AttributeError: 'int' object has no attribute 'encode'

So, before writing the list, I had to change every value to string.

row = [str(item) for item in row]

I think this line can be added in the writerow function of UnicodeWriter class.

這似乎是同一類型的問題