python:使用元素树处理XML项数组,最好是;“合并”;
我有一个名为projectDet[]的数组。它包含大约350个项目 数组中的每个项都是xml信息(下面的单个项)。每个项目的xml格式相同,但id和值不同。 我更喜欢的是将所有这些都放在一个大的XML变量中,我可以使用元素树从中提取元素。现在我不知道如何使用元素树遍历数组中的300个项 我有一个代码,可以检查一组不同的XML的ID,然后如果XML数据a中的ID与XML数据B中的ID匹配,我将“计费小时数”取出,并将其添加到与ID匹配的最后一个CSV行中。这适用于不在数组中的其他XML。因此,我觉得最简单的方法是使用我的代码,但我需要以某种方式将所有这些条目“合并”到一个变量中,我可以将其导入现有函数 因此,有没有一种方法可以循环遍历这个数组,并将每个项合并到一个xml中。它们都将具有相同的树结构。。i、 e根/团队成员/项目和根/任务/项目 谢谢你的建议python:使用元素树处理XML项数组,最好是;“合并”;,python,arrays,xml,elementtree,Python,Arrays,Xml,Elementtree,我有一个名为projectDet[]的数组。它包含大约350个项目 数组中的每个项都是xml信息(下面的单个项)。每个项目的xml格式相同,但id和值不同。 我更喜欢的是将所有这些都放在一个大的XML变量中,我可以使用元素树从中提取元素。现在我不知道如何使用元素树遍历数组中的300个项 我有一个代码,可以检查一组不同的XML的ID,然后如果XML数据a中的ID与XML数据B中的ID匹配,我将“计费小时数”取出,并将其添加到与ID匹配的最后一个CSV行中。这适用于不在数组中的其他XML。因此,我觉
<root>
<team_members type="list">
<item type="dict">
<id>1137</id>
<cost_rate type="float">76.0</cost_rate>
<budget_spent_percentage type="null" />
<projected_hours type="float">0.0</projected_hours>
<user_id type="int">1351480</user_id>
<total_hours type="float">0.0</total_hours>
<name>Bob R</name>
<budget_left type="null" />
</item>
<item type="dict">
<id>1137</id>
<cost_rate type="null" />
<budget_spent_percentage type="null" />
<projected_hours type="float">2072.0</projected_hours>
<user_id type="null" />
<total_hours type="float">0.0</total_hours>
<name>Samm</name>
<budget_left type="null" />
</item>
</team_members>
<nonbillable_detailed_report_url type="str">/reports/detailed/any</nonbillable_detailed_report_url>
<detailed_report_url type="str">/reports/any</detailed_report_url>
<billable_detailed_report_url type="str">/reports/any</billable_detailed_report_url>
<tasks type="list">
<item type="dict">
<id>1137</id>
<budget_left type="null" />
<budget_spent_percentage type="null" />
<billed_rate type="float">0.0</billed_rate>
<over_budget type="null" />
</item>
<item type="dict">
<id>1137</id>
<budget_left type="null" />
<budget_spent_percentage type="null" />
<billed_rate type="float">0.0</billed_rate>
<over_budget type="null" />
</item>
<item type="dict">
<id>1137</id>
<budget_left type="null" />
<budget_spent_percentage type="null" />
<billed_rate type="float">0.0</billed_rate>
<over_budget type="null" />
<total_hours type="float">0.0</total_hours>
<budget type="null" />
</item>
<item type="dict">
<id>1137</id>
<budget_left type="null" />
<budget_spent_percentage type="null" />
<billed_rate type="float">0.0</billed_rate>
<over_budget type="null" />
<total_hours type="float">0.0</total_hours>
<budget type="null" />
</item>
<item type="dict">
<id>1137</id>
<budget_left type="null" />
<budget_spent_percentage type="null" />
<billed_rate type="float">0.0</billed_rate>
<over_budget type="null" />
<total_hours type="float">0.0</total_hours>
<budget type="null" />
</item>
<item type="dict">
<id>1137</id>
<budget_left type="null" />
<budget_spent_percentage type="null" />
<billed_rate type="float">0.0</billed_rate>
<over_budget type="null" />
<total_hours type="float">0.0</total_hours>
<budget type="null" />
</item>
</tasks>
</root>
1137
76
0
1351480
0
鲍勃R
1137
2072
0
萨姆
/报告/详细/任何
/报告/任何
/报告/任何
1137
0
1137
0
1137
0
0
1137
0
0
1137
0
0
1137
0
0
考虑使用在列表中迭代附加
的所有子项。但首先捕获
的第一个完整元素,然后追加:
import xml.etree.ElementTree as ET
cnt = 1
for i in projectDet:
if cnt == 1:
main = ET.fromstring(i)
else:
team = ET.fromstring(i).findall('.//team_members')
main.append(team[0])
nonbill = ET.fromstring(i).findall('.//nonbillable_detailed_report_url')
main.append(nonbill[0])
detrpt = ET.fromstring(i).findall('.//detailed_report_url')
main.append(detrpt[0])
bill = ET.fromstring(i).findall('.//billable_detailed_report_url')
main.append(bill[0])
task = ET.fromstring(i).findall('.//tasks')
main.append(task[0])
cnt+=1
# OUTPUT LARGE XML (main OBJ)
print(ET.tostring(main).decode("UTF-8"))
它是一个numpy数组还是一个列表?@Parfait你好!我是通过projectDet=[]创建的,所以我想这是一个列表?