python:处理一个包含 XML 项的数组,最好使用元素树 "merge"
python: handling an array of XML items with element tree preferably "merge"
我有一个名为 projectDet[] 的数组。它包含大约 350 个项目。
数组中的每个项是xml个信息(下面的单个项目)。每个项目的 xml 格式相同,但 ID 和值不同。
我更喜欢将所有这些都放入一个大的 XML 变量中,这样我就可以使用元素树将元素拉出来。现在我不知道如何使用元素树遍历数组中的 300 个项目。
我有代码检查另一组 XML 的 ID,然后如果 XML 数据 A 中的 ID 与 xml 数据 B 中的 ID 匹配,我将 "billable hours" 并将其添加到与 id 匹配的最终 CSV 行中。这适用于数组中不存在的其他 XML。所以我觉得最简单的方法是使用我现有的有效代码,但我需要以某种方式 "merge" 将所有这些条目放入一个变量中,我可以将其传递到我现有的函数中。
那么有没有一种方法可以遍历这个数组并将每个项目合并为一个 xml。它们都具有相同的树结构.. 即 root/team_member/item 和 root/tasks/item
感谢任何建议。
<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>
考虑使用 append()
在列表中迭代追加 <root>
的所有子项。但最初捕获 <root>
的第一个完整元素,然后在其后追加:
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"))
我有一个名为 projectDet[] 的数组。它包含大约 350 个项目。
数组中的每个项是xml个信息(下面的单个项目)。每个项目的 xml 格式相同,但 ID 和值不同。 我更喜欢将所有这些都放入一个大的 XML 变量中,这样我就可以使用元素树将元素拉出来。现在我不知道如何使用元素树遍历数组中的 300 个项目。
我有代码检查另一组 XML 的 ID,然后如果 XML 数据 A 中的 ID 与 xml 数据 B 中的 ID 匹配,我将 "billable hours" 并将其添加到与 id 匹配的最终 CSV 行中。这适用于数组中不存在的其他 XML。所以我觉得最简单的方法是使用我现有的有效代码,但我需要以某种方式 "merge" 将所有这些条目放入一个变量中,我可以将其传递到我现有的函数中。
那么有没有一种方法可以遍历这个数组并将每个项目合并为一个 xml。它们都具有相同的树结构.. 即 root/team_member/item 和 root/tasks/item
感谢任何建议。
<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>
考虑使用 append()
在列表中迭代追加 <root>
的所有子项。但最初捕获 <root>
的第一个完整元素,然后在其后追加:
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"))