【问题标题】:Extracting xml data提取xml数据
【发布时间】:2020-10-25 16:54:29
【问题描述】:

我想从一个 XML 文件中提取一些数据。这是 XML 文件:
<post height="4093" score="14" file_url="xxxx" parent_id="" sample_url="xxxx" sample_width="850" sample_height="1202 />"

我只需要提取存储file_url 的字符串。我想要这个"xxxx"

【问题讨论】:

标签: python xml string


【解决方案1】:

可以使用xml.etree.ElementTreexml解析器:

import xml.etree.ElementTree as ET

xml = '''<post height="4093" score="14" file_url="xxxx" parent_id="" sample_url="xxxx" sample_width="850" sample_height="1202" />'''
root = ET.fromstring(xml)
print(root.attrib['file_url'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-25
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2020-07-02
    • 2015-09-08
    • 2021-03-18
    相关资源
    最近更新 更多