在python

时间:2016-12-09 19:03:52

标签: python dictionary nested

编辑:显然dic.update在Python中仍然运行良好,所以我已经解决了我的问题。如果人们将来再看这件事,会不会这样做。

def read_prices_file(filename):
    tree = ET.parse(filename)
    root = tree.getroot()
    items = root.find('Items')
    outer_dict = {}
    middle_dict = {}
    inner_dict = {}
    #outer_dict[inner_dict] = {}
    item_code = ""
    counter = 0
    item = ""
    for element in items.findall('Item'):
        item = element
        for element_2 in item:
            item_code = item.find('ItemCode')
            inner_dict[str(element_2.tag)] = str(element_2.text)
        outer_dict[str(item_code.text)] = inner_dict
        break
    print(outer_dict)

read_prices_file("prices.xml")

我希望我的字典看起来像:

{ItemCode1 : {Element 1_2 : Element text 1_2, Element 1_3 : Element_text 1,3}, ItemCode2: Element 2_1 : Element text 2_1, ...}

但相反它会给出最后一个。如何定义要添加到嵌套字典的位置? (提示更可取,不寻找完整的解决方案/代码)

0 个答案:

没有答案