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