2016-08-17 70 views
1

我有一個名爲projectDet []的數組。它包含約350項。python:處理XML元素的數組,最好是「合併」

EACH數組中的項目是xml信息(單個項目在下面)。每個項目的XML都是相同的格式,但不同的ID和值。 我更喜歡將所有這些變成一個大的XML變量,我可以將元素從元素樹中抽出。現在我不知道如何使用元素樹遍歷數組中的300個項目。

我有代碼檢查一組不同的XML作爲ID,然後如果XML數據A中的ID與xml數據B中的ID匹配,我將「可計費小時」取出並添加到最終的CSV行匹配id。這適用於不在數組中的其他XML。所以我覺得最簡單的方法就是使用我的代碼,但我需要以某種方式將所有這些條目「合併」到一個變量中,以便將它們傳遞到我現有的函數中。

那麼有沒有辦法循環這個數組,並將每個項目合併到一個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> 
+0

它是一個numpy的陣列或列表'[]'? – Parfait

+0

@Parfait再次你好!我通過做projectDet = []創建它,所以我想這是一個列表? –

回答

2

考慮使用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"))