[英]How to write xml file with multiple root element using ElementTree in python
[英]How to read XML with python ElementTree when there are multiple entries in each element
编辑以包含一些额外的信息
我有一些 XML 格式的数据,我已经设法使用 Python 中的ElementTree
进行解析。 在主数据的每一行中都有一个名为volume
的标签,然后是一组数据,然后我将其放入一个数组中(我后来将其保存为 pandas 数据帧)。 我遇到的问题是我需要知道我正在调用字段名称的条目的名称(即 [ message
, messagetimestamp
, settlementdate
日期, settlementperiod
期等......]才能调用数据。
我想使用一些代码来告诉我该字段名称列表是什么,而不是手动输入它们。 我的 XML 术语在这里可能有误,但数据看起来像所附图像。
XML 作为文本:
<response xmlns="http://www.netareports.com/backend/realtime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.netareports.com/backend/realtime http://www.netareports.com/backend/realtime.xsd" status="1" timestamp="2021-03-19T16:37:23">
<periodacceptedvolume message="BMRS.DISPTAV" messagetimestamp="2020-11-30T01:52:32" settlementdate="2020-11-30T00:00:00" settlementperiod="3" bmunitid="E_GYAR-1" bidofferpairnumber="-1" offervolume="0" bidvolume="-44.75" taggedoffervolume="0" repricedoffervolume="0" originallypricedoffervolume="0" taggedbidvolume="-44.531" repricedbidvolume="0" originallypricedbidbolume="-43.69"/>
<periodacceptedvolume message="BMRS.DISPTAV" messagetimestamp="2020-11-30T01:52:32" settlementdate="2020-11-30T00:00:00" settlementperiod="3" bmunitid="T_DAMC-1" bidofferpairnumber="1" offervolume="240" bidvolume="0" taggedoffervolume="240" repricedoffervolume="0" originallypricedoffervolume="0" taggedbidvolume="0" repricedbidvolume="0" originallypricedbidbolume="0"/>
<periodacceptedvolume message="BMRS.DISPTAV" messagetimestamp="2020-11-30T01:52:32" settlementdate="2020-11-30T00:00:00" settlementperiod="3" bmunitid="T_DIDCB6" bidofferpairnumber="1" offervolume="220" bidvolume="0" taggedoffervolume="220" repricedoffervolume="0" originallypricedoffervolume="0" taggedbidvolume="0" repricedbidvolume="0" originallypricedbidbolume="0"/>
<periodacceptedvolume message="BMRS.DISPTAV" messagetimestamp="2020-11-30T01:52:32" settlementdate="2020-11-30T00:00:00" settlementperiod="3" bmunitid="T_DINO-6" bidofferpairnumber="-1" offervolume="0" bidvolume="-2.592" taggedoffervolume="0" repricedoffervolume="0" originallypricedoffervolume="0" taggedbidvolume="-2.592" repricedbidvolume="0" originallypricedbidbolume="0"/>
<periodacceptedvolume message="BMRS.DISPTAV" messagetimestamp="2020-11-30T01:52:32" settlementdate="2020-11-30T00:00:00" settlementperiod="3" bmunitid="T_MEDP-1" bidofferpairnumber="1" offervolume="170" bidvolume="0" taggedoffervolume="170" repricedoffervolume="0" originallypricedoffervolume="0" taggedbidvolume="0" repricedbidvolume="0" originallypricedbidbolume="0"/>
我用来阅读它的代码是:
import xml.etree.ElementTree as ET
import urllib.request
url = #not included as it includes a password but the XML data is copied from the url
download = urllib.request.urlopen(url).read()
tree = ET.fromstring(download)
fieldsV=['message','messagetimestamp','settlementdate','settlementperiod','bmunitid','bidofferpairnumber','offervolume','bidvolume','taggedoffervolume','repricedoffervolume','originallypricedoffervolume','taggedbidvolume','repricedbidvolume','originallypricedbidbolume']
data = []
for child in tree:
data.append([(child.get(name) or '') for name in fields])
这工作正常,但fields
是我手动输入的列表,我想使用代码生成列表,以便我可以将其应用于其他类似的 XML 文件。 任何帮助将非常感激!
假设 XML 根的所有子元素(在这种情况下为periodacceptedvolume
元素)包含相同的属性列表,您只需获取该元素的一个样本并从那里读取属性名称列表:
....
tree = ET.fromstring(download)
sample_node = tree.find('*')
fieldsV=[a for a in sample_node.attrib]
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.