2017-04-25 106 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。

相关问题