【发布时间】:2017-08-18 03:56:52
【问题描述】:
我正在尝试使用 Elementree 将 XML 文件转换为 Dictionary。 XML 文件中有各种标签,但对于每条记录,ID 标签都是主键。因此,我尝试创建的字典将父标记作为 ID,将所有其他属性作为其子键。但是我收到一个 unboundlocalerror 说'局部变量 x 在赋值之前是引用。代码如下:
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)
【问题讨论】:
-
我想我能弄清楚这个。你能帮我解决这个问题吗? stackoverflow.com/questions/45724345/…