PYTHON如何制作“关联数组”

时间:2018-06-20 12:29:44

标签: python xml beautifulsoup

我制作了一个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}

1 个答案:

答案 0 :(得分:1)

添加另一个循环

folderTypesId = config.find_all('foldertype_id')
for folderType in folderTypesId:
    label               = folderType.find('name').string
    folderTypeId        = folderType.find('id').string
    subfolders = dict() 
    for s in folderType.find_all('subfolder'):
        subfolders[s.find('name').string] = s.find('id').string
    result[label]       = {'id' : folderTypeId, 'subfolders': subfolders}
相关问题