我制作了一個python腳本,需要在XML中具有一些參數(shù)。所以這是XML:<ROOT><FOLDERTYPES> <FOLDERTYPE_ID> <NAME>FOURNISSEUR</NAME> <ID>2</ID> <SUBFOLDER> <NAME>Administratif</NAME> <ID>4</ID> </SUBFOLDER> <SUBFOLDER> <NAME>Commandes</NAME> <ID>5</ID> </SUBFOLDER> <SUBFOLDER> <NAME>Factures</NAME> <ID>6</ID> </SUBFOLDER> </FOLDERTYPE_ID> <FOLDERTYPE_ID> <NAME>CLIENT</NAME> <ID>3</ID> <SUBFOLDER> <NAME>Administratif</NAME> <ID>4</ID> </SUBFOLDER> <SUBFOLDER> <NAME>Commandes</NAME> <ID>5</ID> </SUBFOLDER> <SUBFOLDER> <NAME>Factures</NAME> <ID>6</ID> </SUBFOLDER> <SUBFOLDER> <NAME>Logistique</NAME> <ID>7</ID> </SUBFOLDER> </FOLDERTYPE_ID></FOLDERTYPES>現(xiàn)在,我只能夠像下面這樣獲取“名稱”和“ ID”:{'FOURNISSEUR': {'id': '2'}, 'CLIENT': {'id': '3'}}但是我需要像這樣的字典中包含所有子文件夾:{'FOURNISSEUR': {'id': '2', 'subfolders' : {'Administratif':'4','Commandes':'5','Factures':'6'}}, 'CLIENT': {'id': '3', 'subfolders' : {'Administratif':'4','Commandes':'5','Factures':'6','Logistique':'7'}}這是我現(xiàn)在擁有的代碼:def getFolderTypeArray(fileName): result = {} with open(fileName, 'rb') as config_file: content = config_file.read() config = BeautifulSoup(content, "lxml") folderTypesId = config.find_all('foldertype_id') for folderType in folderTypesId: label = folderType.find('name').string folderTypeId = folderType.find('id').string result[label] = {'id' : folderTypeId}
添加回答
舉報
0/150
提交
取消