2017-04-25 109 views
2

有人知道爲什麼這個代碼是確定:蟒ET.tostring(根,編碼=「的Unicode」,方法=「XML」)提高類型錯誤:需要對類字節對象,而不是「STR」

text='''<?xml version="1.0"?> 
<data> 
    <country name="Liechtenstein"> 
     <rank>1</rank> 
     <year>2008</year> 
     <gdppc>141100</gdppc> 
     <neighbor name="Austria" direction="E"/> 
     <neighbor name="Switzerland" direction="W"/> 
    </country> 
</data>''' 
root=ET.fromstring(text) 
ET.tostring(root, method='xml') 
ET.tostring(root, encoding='UTF-8', method='xml') 

但是當我使用Unicode編碼: ET.tostring(root, encoding='Unicode', method='xml')

我得到:

Traceback (most recent call last): 
    ... omissis ... 
    File "/home/ago/anaconda3/lib/python3.6/xml/etree/ElementTree.py", line 915, in _serialize_xml 
    write("<" + tag) 
TypeError: a bytes-like object is required, not 'str' 

TypeError: a bytes-like object is required, not 'str' 

據蟒蛇3.6 doc中的toString我可以用 '統一' ...

"Use encoding="unicode" to generate a Unicode string (otherwise, a bytestring is generated)."

我可以使用ElementTree.write(... encoding='Unicode' ...)沒有問題。

我用:

Python 3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 12:22:00) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux 

Python 3.4.2 (default, Oct 8 2014, 10:45:20) 
[GCC 4.9.1] on linux 

我有不同的行爲:提前

ET.tostring(root, encoding='Unicode') 
Traceback (most recent call last): 
    ... omissis ... 
    File "/usr/lib/python3.4/xml/etree/ElementTree.py", line 917, in _serialize_xml 
    write("<" + tag) 
TypeError: 'str' does not support the buffer interface 

感謝

+0

如果使用'encoding ='unicode'(小寫字母'u'),錯誤就會消失,不是嗎? – mzjn

+0

賓果!謝謝mzjn,我誤讀了我在我的問題中引用的內容:**「使用encoding =」unicode「生成一個Unicode字符串(否則生成一個字符串)。」** ... ElementTree.write()使用'enc_lower = encoding.lower()'降低字符串。即使tostring似乎調用ElementTree.write,它的工作方式也不同。我將深入ET編碼... – agossino

回答

0

'Unicode'不是有效編碼爲不是編碼,而是一系列碼點(請參閱utf-8 vs unicode)。

Python 3以Unicode格式存儲字符串,所以爲了使它成爲bytes-like object,需要先編碼它(例如到utf-8)。

ElementTree.fromstring預計bytes-like object編碼與一個特定的編碼,而不是Unicode。

作爲一個附註,反過來將採用bytes-like object並使用bytes-like object的編碼解碼爲Unicode。

相關問題