如何将ElementTree.Element转换为字符串?
对于Python 3:xml_str = ElementTree.tostring(xml, encoding='unicode')
对于Python 2:xml_str = ElementTree.tostring(xml, encoding='utf-8')
为了与Python 2&3兼容:xml_str = ElementTree.tostring(xml).decode()
示例用法from xml.etree import ElementTree
xml = ElementTree.Element("Person", Name="John")
xml_str = ElementTree.tostring(xml).decode()
print(xml_str)
输出:
解释
不管名称意味着什么,^{}在Python 2&3中默认返回bytestring。这是Python 3中的一个问题,即uses Unicode for strings。In Python 2 you could use the str type for both text and binary data.
Unfortunately this confluence of two different concepts could lead to
brittle code which sometimes worked for either kind of data, sometimes
not. [...]
To make the distinction between text and binary data clearer and more pronounced, [Python 3] made text and binary data distinct types that cannot blindly be mixed together.
如果我们知道使用的是什么版本的Python,我们可以将编码指定为unicode或utf-8。否则,如果我们需要与Python 2&3兼容,可以使用^{}来转换为正确的类型。
作为参考,我将比较Python 2和Python 3之间的.tostring()结果。ElementTree.tostring(xml)
# Python 3: b''
# Python 2:
ElementTree.tostring(xml, encoding='unicode')
# Python 3:
# Python 2: LookupError: unknown encoding: unicode
ElementTree.tostring(xml, encoding='utf-8')
# Python 3: b''
# Python 2:
ElementTree.tostring(xml).decode()
# Python 3:
# Python 2:
感谢Martijn Peters指出str数据类型在Python 2和3之间发生了变化。
为什么不使用str()?
在大多数情况下,使用^{}将是将对象转换为字符串的“cannonical”方式。不幸的是,将它与Element一起使用会将对象在内存中的位置返回为十六进制字符串,而不是对象数据的字符串表示形式。from xml.etree import ElementTree
xml = ElementTree.Element("Person", Name="John")
print(str(xml)) #