gpt4 book ai didi

python - 如何使用 python 将此 XML 文件转换为 CSV?

转载 作者:行者123 更新时间:2023-12-04 07:41:02 25 4
gpt4 key购买 nike

我在此附上我试图转换为 csv 的 xml 文件

<?xml version="1.0"?>
<response>
<data>
<shops>
<shop id="204019">
<name>Bannockburn</name>
<status>Open</status>
<company id="25">Franchise</company>
<shopAttributes>
<shopAttribute attrName="shop_OPEN_DATE">2008-07-16</shopAttribute>
<shopAttribute attrName="CLOSE_DATE"/>
<shopAttribute attrName="shop_DISTRIBUTION_CTR_GENERAL" startDate="2019-03-19">90</shopAttribute>
<shopAttribute attrName="shop_DISTRIBUTION_CTR_GENERAL" startDate="1900-01-01" endDate="2019-03-18"/>
</shopAttributes>
<addresses>
<address type="PUBLIC">
<addressLine1>1211 Half Day Road</addressLine1>
<addressLine2></addressLine2>
<city>Bannockburn</city>
<stateProvince>IL</stateProvince>
<postalCode>60015</postalCode>
<country>USA</country>
<latitude>42.199461</latitude>
<longitude>-87.860582</longitude>
</address>
</addresses>
</shop>
<shop id="204020">
<name>Plainfield - North Plainfield</name>
<status>Open</status>
<company id="25">Franchise</company>
<shopAttributes>
<shopAttribute attrName="shop_OPEN_DATE">2007-05-18</shopAttribute>
<shopAttribute attrName="CLOSE_DATE"/>
<shopAttribute attrName="shop_DISTRIBUTION_CTR_GENERAL" startDate="2019-03-19">90</shopAttribute>
<shopAttribute attrName="shop_DISTRIBUTION_CTR_GENERAL" startDate="1900-01-01" endDate="2019-03-18"/>
</shopAttributes>
<addresses>
<address type="PUBLIC">
<addressLine1>12632 IL Route 59</addressLine1>
<addressLine2>Suite #102</addressLine2>
<city>Plainfield</city>
<stateProvince>IL</stateProvince>
<postalCode>60585</postalCode>
<country>USA</country>
<latitude>41.653125</latitude>
<longitude>-88.204527</longitude>
</address>
</addresses>
</shop>
</shops>
</data>
</response>
这是我想转换为 csv 的 xml 文件,有人可以帮助我如何在 python 中做到这一点吗?
下面是我尝试使用的代码,但我还没有真正理解如何去做,看到了一些例子,但不是很清楚
from xml.etree import ElementTree

tree = ElementTree.parse('Store.xml')
root = tree.getroot()

for att in root:
first = att.find('shops').text
print('{}'.format(first))
但我在这里没有。

最佳答案

不是一个完整的解决方案,而是回答您为什么会收到 None ,那是因为你的店铺实际上更深一层,在data下标签。
这段代码可能会让您了解如何访问底层属性,您可以将这些属性收集到列表或其他容器中以构建 CSV。

from xml.etree import ElementTree

tree = ElementTree.parse('Store.xml')
root = tree.getroot()
data = root.find('data')

for shops in data:
for shop in shops:
name = shop.find('name').text
sid = shop.attrib
status = shop.find('status').text
attrs = shop.find('shopAttributes')
open_date = attrs.find(".//shopAttribute/[@attrName='shop_OPEN_DATE']").text
print(f"Name: {name}, ID: {sid}, Status: {status}, open: {open_date}")
open_date是如何使用 XPath 的示例访问属性。代码返回:
Name: Bannockburn, ID: {'id': '204019'}, Status: Open, open: 2008-07-16
Name: Plainfield - North Plainfield, ID: {'id': '204020'}, Status: Open, open: 2007-05-18

关于python - 如何使用 python 将此 XML 文件转换为 CSV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67464706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com