ElementTree Python: 如果兄弟姐妹是嵌套的,如何提取兄弟姐妹?
ElementTree Python: How to extract siblings if siblings are nested?
我有一个 XML 文件,我正试图将其转换为 Excel 数据集。 XML是这样排列的:
<XML Data>
<Record>
<ID>
<Client id="01"></Client>
</ID>
<Service>
<Product id="A"></Product>
<Product id="B"></Product>
<Product id="C"></Product>
</Service>
</Record>
<Record>
<ID>
<Client id="02"></Client>
</ID>
<Service>
<Product id="A"></Product>
<Product id="B"></Product>
<Product id="Y"></Product>
</Service>
</Record>
<Record>
<ID>
<Client id="24"></Client>
</ID>
<Service>
<Product id="U"></Product>
</Service>
</Record>
</XML Data>
如您所见,每条记录都显示了具有多项服务的单个客户端。
我正在尝试仅使用 ElementTree 来完成这项工作。这是 return 每个客户端 ID 的所有服务的错误代码——我无法弄清楚如何将它获取到 return 客户端实际拥有的每个服务:
for x in root.findall("Record/ID/Client"):
client = x.get("id")
for y in root.findall('.//Service/Product'):
service = y.get("id")
print(client, service)
我正在尝试以 CSV 格式将其排列成这样:
ClientID ServiceID
01 A
01 B
01 C
02 A
02 B
02 Y
24 U
如有任何建议,我们将不胜感激!我查过这个,但只能找到显示如何提取实际兄弟姐妹的资源——因为客户端 ID 和服务 ID 是 parents 到我想提取的 children,这证明有点混乱。谢谢!
而不是第一个 select Client
,第一个 select Record
。
然后你的第二个 for 循环可以从 root.finall
更改为 x.findall
将只找到当前 Record
的 Product
个元素。
示例...
XML输入(test.xml;修复无效根元素)
<XML_Data>
<Record>
<ID>
<Client id="01"></Client>
</ID>
<Service>
<Product id="A"></Product>
<Product id="B"></Product>
<Product id="C"></Product>
</Service>
</Record>
<Record>
<ID>
<Client id="02"></Client>
</ID>
<Service>
<Product id="A"></Product>
<Product id="B"></Product>
<Product id="Y"></Product>
</Service>
</Record>
<Record>
<ID>
<Client id="24"></Client>
</ID>
<Service>
<Product id="U"></Product>
</Service>
</Record>
</XML_Data>
Python
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
for x in root.findall("Record"):
client = x.find("ID/Client").get("id")
for y in x.findall('.//Service/Product'):
service = y.get("id")
print(client, service)
打印输出
01 A
01 B
01 C
02 A
02 B
02 Y
24 U
我有一个 XML 文件,我正试图将其转换为 Excel 数据集。 XML是这样排列的:
<XML Data>
<Record>
<ID>
<Client id="01"></Client>
</ID>
<Service>
<Product id="A"></Product>
<Product id="B"></Product>
<Product id="C"></Product>
</Service>
</Record>
<Record>
<ID>
<Client id="02"></Client>
</ID>
<Service>
<Product id="A"></Product>
<Product id="B"></Product>
<Product id="Y"></Product>
</Service>
</Record>
<Record>
<ID>
<Client id="24"></Client>
</ID>
<Service>
<Product id="U"></Product>
</Service>
</Record>
</XML Data>
如您所见,每条记录都显示了具有多项服务的单个客户端。
我正在尝试仅使用 ElementTree 来完成这项工作。这是 return 每个客户端 ID 的所有服务的错误代码——我无法弄清楚如何将它获取到 return 客户端实际拥有的每个服务:
for x in root.findall("Record/ID/Client"):
client = x.get("id")
for y in root.findall('.//Service/Product'):
service = y.get("id")
print(client, service)
我正在尝试以 CSV 格式将其排列成这样:
ClientID ServiceID
01 A
01 B
01 C
02 A
02 B
02 Y
24 U
如有任何建议,我们将不胜感激!我查过这个,但只能找到显示如何提取实际兄弟姐妹的资源——因为客户端 ID 和服务 ID 是 parents 到我想提取的 children,这证明有点混乱。谢谢!
而不是第一个 select Client
,第一个 select Record
。
然后你的第二个 for 循环可以从 root.finall
更改为 x.findall
将只找到当前 Record
的 Product
个元素。
示例...
XML输入(test.xml;修复无效根元素)
<XML_Data>
<Record>
<ID>
<Client id="01"></Client>
</ID>
<Service>
<Product id="A"></Product>
<Product id="B"></Product>
<Product id="C"></Product>
</Service>
</Record>
<Record>
<ID>
<Client id="02"></Client>
</ID>
<Service>
<Product id="A"></Product>
<Product id="B"></Product>
<Product id="Y"></Product>
</Service>
</Record>
<Record>
<ID>
<Client id="24"></Client>
</ID>
<Service>
<Product id="U"></Product>
</Service>
</Record>
</XML_Data>
Python
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
for x in root.findall("Record"):
client = x.find("ID/Client").get("id")
for y in x.findall('.//Service/Product'):
service = y.get("id")
print(client, service)
打印输出
01 A
01 B
01 C
02 A
02 B
02 Y
24 U