我制作了一个python脚本,需要在XML中具有一些参数。所以这是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>
现在,我只能够像下面这样获取“名称”和“ 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'}}
这是我现在拥有的代码:
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}
相关分类