将XML文件中包含的部分数据转换为CSV fi

2024-04-20 06:15:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在python中将XML文件转换为CSV文件。下面我附上一张照片的霍姆XML文件看起来像,我想为每个id(如PZH01\u MST\u 0690\u 00)采取最低流量,在这种情况下,它将是:60。有人能帮我吗?我想有经验的人做这件事相当容易。基本上,我想将这个XML文件转换成CSV文件,并有2列(1列具有id,2列具有最低的流量)。任何帮助都将不胜感激,谢谢!你知道吗

<?xml version="1.0" encoding="UTF-8"?>

-<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">


-<SOAP:Body>


-<d2LogicalModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" modelBaseVersion="2" xmlns="http://datex2.eu/schema/2/2_0">


-<exchange>


-<supplierIdentification>

<country>nl</country>

<nationalIdentifier>NLNDW</nationalIdentifier>

</supplierIdentification>

</exchange>


-<payloadPublication lang="nl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="MeasuredDataPublication">

<publicationTime>2019-10-31T11:12:42.005Z</publicationTime>


-<publicationCreator>

<country>nl</country>

<nationalIdentifier>NLNDW</nationalIdentifier>

</publicationCreator>

<measurementSiteTableReference targetClass="MeasurementSiteTable" version="1139" id="NDW01_MT"/>


-<headerInformation>

<confidentiality>noRestriction</confidentiality>

<informationStatus>real</informationStatus>

</headerInformation>


-<siteMeasurements xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<measurementSiteReference targetClass="MeasurementSiteRecord" version="6" id="PZH01_MST_0690_00"/>

<measurementTimeDefault>2019-10-31T11:11:00Z</measurementTimeDefault>


-<measuredValue index="1">


-<measuredValue>


-<basicData xsi:type="TrafficFlow">


-<vehicleFlow>

<vehicleFlowRate>720</vehicleFlowRate>

</vehicleFlow>

</basicData>

</measuredValue>

</measuredValue>


-<measuredValue index="2">


-<measuredValue>


-<basicData xsi:type="TrafficFlow">


-<vehicleFlow>

<vehicleFlowRate>60</vehicleFlowRate>

</vehicleFlow>

</basicData>

</measuredValue>

</measuredValue>


-<measuredValue index="3">


-<measuredValue>


-<basicData xsi:type="TrafficFlow">


-<vehicleFlow>

<vehicleFlowRate>60</vehicleFlowRate>

</vehicleFlow>

</basicData>

</measuredValue>

</measuredValue>


-<measuredValue index="4">


-<measuredValue>


-<basicData xsi:type="TrafficFlow

Tags: 文件orgidhttpindextypecountryxmlns
1条回答
网友
1楼 · 发布于 2024-04-20 06:15:41

我建议您使用python-benedict,它是一个dict子类,支持从/到最常见格式的I/O操作,包括xmlcsv。你知道吗

安装:pip install python-benedict

文档:https://github.com/fabiocaccamo/python-benedict

from benedict import benedict as bdict

# xml data-string, or xml filepath or xml url
xml_data = ''

d = bdict.from_xml(xml_data)
d.standardize()

# print the current dict to check it
print(d.dump())

# now convert it to csv:

# keypath (using the dot syntax) to the item that is a list of dicts
items_keypath = 'path.to.list.of.dicts'

# define filepath if you need to save csv to disk
csv_filepath = None

csv_data = d.to_csv(key=items_keypath, filepath=csv_filepath)
print(csv_data)

非常简单:)

相关问题 更多 >