Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting an xml to dictionary

I have an XML file like this:

<?xml version="1.0" encoding="UTF-8"?>
<items>
  <item type="dict">
    <job_id type="str">id-1</job_id >
    <title type="str">title-1</title>
    <desc type="str">desc-1</desc>
  </item>
  <item type="dict">
    <job_id type="str">id-2</job_id>
    <title type="str">title-2</title>
    <desc type="str">desc-2</desc>
  </item>
</items>

I want to parse this into a dictionary, in such a way that I can access the job using it’s id. So the dictionary key will be job_id and the whole job definition will the corresponding value.

Here is what I have tried:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Job:
    def __init__(self):
        self.path = "path-to-xml-file"
        self.job_dict = {}

    def load_jobs(self, env, path):
        file = read_from_s3(env, full_path) # reads the job file from S3 bucket
        dict = xmltodict.parse(file)

        for item in dict['items']['item']:
            key = item['job_id']
            self.job_dict[key] = item # <-- I get exception on this line

I get the following exception when I try to add element to the dictionary:

[Failure instance: Traceback: <class ‘TypeError’>: unhashable type:
‘collections.OrderedDict’

Also in the watch window, this is what I see for item:

enter image description here

and this is what I see for key:

enter image description here

>Solution :

item['job_id'] is a dict. You cant use that as a key in your self.job_dict = {}.

Change it to key = item['job_id']['#text'] instead


Just to better understand the error, the object implementing a dictionary key must implement magic method __hash__().
This means a key must be hashable for the dictionary structure to be optimized.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading