我有一個看起來像這樣的xml。(它更長,所以沒有粘貼整個東西)我正在嘗試使用 read_xml 讀取提到的檔案,但它只是列印一個充滿 NaN 值的表。我該如何解決?(Newby 在 XML 檔案方面)
import numpy as np
import pandas as pd
from tkinter import filedialog as fd
filename = fd.askopenfilename()
df = pd.read_xml('{}'.format(filename), )
print(df)
<ScheduleMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" DtdVersion="3" DtdRelease="3">
<MessageIdentification v="20211022_DA_POS_65XGENEXMARKET0I" />
<MessageVersion v="1" />
<MessageType v="A01" />
<ProcessType v="A01" />
<ScheduleClassificationType v="A01" />
<SenderIdentification v="65XGENEXMARKET0I" codingScheme="A01" />
<SenderRole v="A01" />
<ReceiverIdentification v="10X1001C--00007L" codingScheme="A01" />
<ReceiverRole v="A04" />
<MessageDateTime v="2021-10-21T10:02:02Z" />
<ScheduleTimeInterval v="2021-10-21T22:00Z/2021-10-22T22:00Z" />
<ScheduleTimeSeries>
<SendersTimeSeriesIdentification v="S_10Y1001A1001B012_65YBG-ENERGRIDDB" />
<SendersTimeSeriesVersion v="1" />
<BusinessType v="A02" />
<Product v="8716867000016" />
<ObjectAggregation v="A03" />
<InArea v="10Y1001A1001B012" codingScheme="A01" />
<OutArea v="10Y1001A1001B012" codingScheme="A01" />
<InParty v="65YBGGENEX000002" codingScheme="A01" />
<OutParty v="65YBG-ENERGRIDDB" codingScheme="A01" />
<MeasurementUnit v="MAW" />
<Period>
<TimeInterval v="2021-10-21T22:00Z/2021-10-22T22:00Z" />
<Resolution v="PT1H" />
<Interval>
<Pos v="1" />
<Qty v="0" />
</Interval>
uj5u.com熱心網友回復:
我將從驗證 XML 檔案開始。根據您共享的代碼,這似乎不是一個有效的 XML 檔案。
為了通過 pandas 讀取 XML 檔案并將其轉換為 csv 或 excel 檔案,您可以使用 pandas_read_xml 庫:
import pandas_read_xml as pdx
然后您可以通過以下代碼行讀取檔案:
df = pdx.read_xml('path-to-your-XML-file.xml')
您還需要在讀取 XML 檔案后進行展平:
df = pdx.fully_flatten(df)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/418295.html
標籤:
