0
我想将XML文件转换为使用Elementree的字典。 XML文件中有各种标签,但对于每个记录,ID标签都是主键。所以我试图创建的字典将父标记作为ID,将所有其他属性作为其子键。但我得到一个unboundlocalerror,说'局部变量x是赋值前的引用。下面是代码:Python:分配前的局部变量引用
tree = ET.parse(xml_file)
root = tree.getroot()
temp_dict={}
def create_dict():
test_dict = {}
for child in root.iter():
if subchild.tag=='ID':
x=(child.text)
else:
test_dict[subchild.tag]= subchild.text
temp_dict[x]=test_dict
return (temp_dict)
我想我是能够找出这一个。你能帮我解决这个问题吗? https://stackoverflow.com/questions/45724345/python-unable-to-extract-attribute-using-xmltodict –