将 Python XML ElementTree 转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33814607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a Python XML ElementTree to a String
提问by user984003
I need to convert an XML ElementTreeto a String after altering it. It's the toString part that isn't working.
我需要在更改XML ElementTree后将其转换为 String。这是 toString 部分不起作用。
import xml.etree.ElementTree as ET
tree = ET.parse('my_file.xml')
root = tree.getroot()
for e in root.iter('tag_name'):
e.text = "something else" # This works
# Now I want the the complete XML as a String with the alteration
I've tried various versions of the below line, with ET or ElementTree as various names, and importing toString, etc. etc,
我尝试了以下行的各种版本,使用 ET 或 ElementTree 作为各种名称,并导入 toString 等,等等,
s = tree.tostring(ET, encoding='utf8', method='xml')
I have seen Convert Python ElementTree to stringand some others, but I'm not sure how to apply it to my example.
我已经看到Convert Python ElementTree to string和其他一些,但我不确定如何将它应用于我的示例。
回答by Stephen Briney
This should work:-
这应该有效:-
xmlstr = ET.tostring(root, encoding='utf8', method='xml')
回答by Stevoisiak
How do I convert ElementTree.Element
to a String?
我如何转换ElementTree.Element
为字符串?
For Python 3:
对于 Python 3:
xml_str = ElementTree.tostring(xml, encoding='unicode')
For Python 2:
对于 Python 2:
xml_str = ElementTree.tostring(xml, encoding='utf-8')
For compatibility with both Python 2 & 3:
为了与 Python 2 和 3 兼容:
xml_str = ElementTree.tostring(xml).decode()
Example usage
示例用法
from xml.etree import ElementTree
xml = ElementTree.Element("Person", Name="John")
xml_str = ElementTree.tostring(xml).decode()
print(xml_str)
Output:
输出:
<Person Name="John" />
Explanation
解释
Despite what the name implies, ElementTree.tostring()
returns a bytestring by default in Python 2 & 3. This is an issue in Python 3, which uses Unicode for strings.
尽管顾名思义,ElementTree.tostring()
在 Python 2 和 3 中默认返回一个字节串。这是 Python 3 中的一个问题,它使用 Unicode 来表示字符串。
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 2 中,您可以将
str
类型用于文本和二进制数据。不幸的是,这种两种不同概念的融合可能会导致脆弱的代码,这些代码有时适用于任何一种数据,有时则不适用。[...]为了使文本和二进制数据之间的区别更清晰、更明显,[Python 3] 使文本和二进制数据具有不同的类型,不能盲目混合在一起。
Source: Porting Python 2 Code to Python 3
If we know what version of Python is being used, we can specify the encoding as unicode
or utf-8
. Otherwise, if we need compatibility with both Python 2 & 3, we can use decode()
to convert into the correct type.
如果我们知道正在使用的 Python 版本,我们可以将编码指定为unicode
or utf-8
。否则,如果我们需要与 Python 2 和 3 兼容,我们可以使用decode()
转换为正确的类型。
For reference, I've included a comparison of .tostring()
results between Python 2 and Python 3.
作为参考,我已经包含了.tostring()
Python 2 和 Python 3 之间的结果比较。
ElementTree.tostring(xml)
# Python 3: b'<Person Name="John" />'
# Python 2: <Person Name="John" />
ElementTree.tostring(xml, encoding='unicode')
# Python 3: <Person Name="John" />
# Python 2: LookupError: unknown encoding: unicode
ElementTree.tostring(xml, encoding='utf-8')
# Python 3: b'<Person Name="John" />'
# Python 2: <Person Name="John" />
ElementTree.tostring(xml).decode()
# Python 3: <Person Name="John" />
# Python 2: <Person Name="John" />
Thanks to Martijn Petersfor pointing out that the str
datatype changed between Python 2 and 3.
感谢Martijn Peters指出str
Python 2 和 3 之间的数据类型发生了变化。
Why not use str()?
为什么不使用 str()?
In most scenarios, using str()
would be the "cannonical" way to convert an object to a string. Unfortunately, using this with Element
returns the object's location in memory as a hexstring, rather than a string representation of the object's data.
在大多数情况下, usingstr()
是将对象转换为字符串的“规范”方式。不幸的是,使用 this withElement
返回对象在内存中的位置作为十六进制字符串,而不是对象数据的字符串表示。
from xml.etree import ElementTree
xml = ElementTree.Element("Person", Name="John")
print(str(xml)) # <Element 'Person' at 0x00497A80>