【发布时间】: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"。
【问题讨论】:
我想从一个 XML 文件中提取一些数据。这是 XML 文件:<post height="4093" score="14" file_url="xxxx" parent_id="" sample_url="xxxx" sample_width="850" sample_height="1202 />"
我只需要提取存储file_url 的字符串。我想要这个"xxxx"。
【问题讨论】:
可以使用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'])
【讨论】: