2016-08-17 4 views
1

私はprojectDet []という配列を持っています。それは約350の項目が含まれています。python:要素ツリーを持つXML項目の配列を処理することが望ましい "merge"

アレイ内のEACHアイテムは、xml情報(以下の単一項目)です。各項目のxmlは同じ書式ですが、IDと値は異なります。 私が望むのは、要素ツリーを使用して要素を取り除くことができる1つの大きなXML変数にこれらをすべて含めることです。今は要素ツリーを使って配列内の300個のアイテムを調べる方法がわかりません。

IDのXMLの別のセットをチェックするコードがあります。そして、XMLデータAのIDがxmlデータBのIDと一致する場合は、「請求可能な時間」を取り出して最終的なCSV行に追加しますidと一致します。これは、配列内にない他のXMLで動作します。だから私は最も簡単な方法は、私はそれが動作しているコードを使用することですが、私は何らかの形で私は既存の関数にパイプできる1つの変数にこれらのエントリを "マージ"する必要があります。

したがって、この配列をループして各項目を1つの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> 
+0

ですか? – Parfait

+0

@Parfaitもう一度!私はprojectDet = []を行うことでそれを作成したので、それはリストだと思いますか? –

答えて

2

リスト全体で繰り返し<root>のすべての子を追加するappend()の使用を検討してください。しかし、最初に<root>の最初の完全な要素をキャプチャし、その後、追加します。それはnumpyの配列またはリスト `[]`

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")) 
関連する問題