python xml字符串_将Python XML元素树转换为字符串

本文介绍了如何在Python中将XML元素树转换为字符串,包括在Python 2和3中的兼容性处理。主要方法是使用`ElementTree.tostring()`函数,并通过指定不同的encoding参数来适应不同版本的Python。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何将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)) #

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值