我想将所有字典存储在列表中,但是当我尝试这样做时,它只存储最后一个字典

时间:2015-07-01 02:20:27

标签: python dictionary recursion

#imports
import xml.etree.ElementTree as ET

import pandas as pd

#Loading the Data File

tree = ET.parse('abc.xml')
root = tree.getroot()
list1=[]
finalData=[]
#Function Which Will Progressively Extract Data From the XML File
def xmlPand(list,result=None):
    result = [] if result is None else result
    for item in list:
        if (item.getchildren()):
            ListofChildren=[]
            for child in item:
                ListofChildren.append(child)
            #print ListofChildren
            xmlPand(ListofChildren,result)
        else:
            dicto[item.tag]=item.text
    result.append(dicto)
    print dicto
    #here when i print it takes all different values the way I want but when    I append it to the list and print the list it gives me only the last dictionary that has been created a number of times



#Getting All the Schema Tags in a list  
for child in root:
    list1.append(child)

#Creating a Temp List for each Schema Tag and Sending it to the xmlPand     Function   

for item in list1:
    list2=[]
    data=[]
    list2.append(item)
    dicto={}
    xmlPand(list2,result=data)
    print data

它在这里所做的是继续向字典中添加内容但是将其与最新内容重叠 请注意,XML文件有许多层次结构 我只需要一种方法来存储已在列表中创建的字典。

0 个答案:

没有答案