【问题标题】:How to get string dump of lxml Element如何获取lxml元素的字符串转储
【发布时间】:2018-06-14 20:04:28
【问题描述】:

我有一个 lxml Element 对象:

>>> from lxml import etree
>>> xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<state type=\"before_battle\">\n</state>"
>>> etree.fromstring(xml_str.encode('utf-8'))
<Element state at 0x7fd04b957e48>

如何获取Element的字符串转储?

【问题讨论】:

    标签: python xml lxml


    【解决方案1】:

    首先将元素对象存储在一个变量中

    >>> d = etree.fromstring(xml_str.encode('utf-8'))
    

    然后使用lxml.etree模块中的tostring函数:

    >>> etree.tostring(d)
    '<state type="before_battle">\n</state>'
    

    有关其他用例,您可以查看lxml.etree Tutorial。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 2013-08-19
      相关资源
      最近更新 更多