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 –