2012-11-21 140 views
9

我有一個dict,它提供了url響應。像:UnicodeEncodeError:'ascii'編解碼器無法對字符進行編碼

>>> d 
{ 
0: {'data': u'<p>found "\u62c9\u67cf \u591a\u516c \u56ed"</p>'} 
1: {'data': u'<p>some other data</p>'} 
... 
} 

在使用這個數據值xml.etree.ElementTree功能(d[0]['data'])我得到的最有名的錯誤消息:

UnicodeEncodeError: 'ascii' codec can't encode characters...

我應該怎麼做這個Unicode字符串,使之適合ElementTree解析器?

PS。請不要用Unicode給我發鏈接& Python解釋。我已經不幸地閱讀了這一切,並且無法使用它,希望別人也能。

回答

23

你將不得不手動對其進行編碼,以UTF-8:

ElementTree.fromstring(d[0]['data'].encode('utf-8')) 

作爲API只需要編碼的字節作爲輸入。對於這樣的數據,UTF-8是一個很好的默認值。

這將能夠解碼從那裏再次UNICODE:

>>> from xml.etree import ElementTree 
>>> p = ElementTree.fromstring(u'<p>found "\u62c9\u67cf \u591a\u516c \u56ed"</p>'.encode('utf8')) 
>>> p.text 
u'found "\u62c9\u67cf \u591a\u516c \u56ed"' 
>>> print p.text 
found "拉柏 多公 園" 
+0

是的,這是我想的第一件事,我總是嘗試。問題在於'ElementTree.tostring'。你可以試試'ElementTree.tostring(p,method ='text')'並告訴它爲什麼不起作用?謝謝 – theta

+1

啊,對不起。這太明顯了。 '.tostring()'有可選參數'encoding',默認情況下它可能被設置爲ascii,因此添加'encoding ='utf-8''即可。歡呼聲 – theta

+0

@theta:呵呵,就是要告訴你。 :-) –

相關問題