![](/img/trans.png)
[英]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.