PYTHON「連想配列」の作り方

ネイサン30

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}
OneCricketeer

別のループを追加する

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}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事