1

I want to write information from xml to dict in Python. Below is xml file:

<data>
  <files>
    <links>
      <item>
        <file_name>file1</file_name>
        <id>100</id>
      </item>
      <item>
        <file_name>file2</file_name>
        <id>200</id>
      </item>
      <item>
        <file_name>file3</file_name>
        <id>300</id>
      </item>
    </links>
  </files>
</data>

To Python dict like a:

xml_content = { 'file1' = 100, 'file2' = 200, 'file3' = 300 }

Thanks for your help

2 Answers 2

1

Using xmltodict this simple code can be used to extract your dictionary:

install xmltodict with pip install xmltodict

import xmltodict

doc = xmltodict.parse("""
<data>
  <files>
    <links>
      <item>
        <file_name>file1</file_name>
        <id>100</id>
      </item>
      <item>
        <file_name>file2</file_name>
        <id>200</id>
      </item>
      <item>
        <file_name>file3</file_name>
        <id>300</id>
      </item>
    </links>
  </files>
</data>
""")

d = {}

for item in doc["data"]["files"]["links"]["item"]:
    d[item["file_name"]] = int(item["id"])

print(d)

d will be:

{u'file3': 300, u'file2': 200, u'file1': 100}

Alternatively you can load the xml from a file like this:

with open('path/to/file.xml') as fd:
    doc = xmltodict.parse(fd.read())
Sign up to request clarification or add additional context in comments.

4 Comments

I recieve that message: >>> for item in doc["data"]["files"]["links"]["item"]: ... d[item["file_name"]] = int(item["id"]) ... Traceback (most recent call last): File "<stdin>", line 2, in <module> KeyError: 'file_name'
@dracons2 This happens because in your xml file the <file_name> either does not exist or has another name. But my answer is correct for your question and if your file is built as you specified the example will work.
TRUE, The example works properly. Thank for your help.
You're welcome. Please consider accepting the answer.
0

Beautiful Soup should help you

Link - https://www.crummy.com/software/BeautifulSoup/

Something like this should work

from bs4 import BeautifulSoup

soup = BeautifulSoup("""
<data>
  <files>
    <links>
      <item>
        <file_name>file1</file_name>
        <id>100</id>
      </item>
      <item>
        <file_name>file2</file_name>
        <id>200</id>
      </item>
      <item>
        <file_name>file3</file_name>
        <id>300</id>
      </item>
    </links>
  </files>
</data>
""")


xml_content = { item.find('file_name').string: item.find('id').string for item in soup.find_all('item') }

Output:

{'file2': '200', 'file3': '300', 'file1': '100'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.