pythonxmltodict

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:

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.