Skip to content
Advertisement

Create a dictionary from an XML using xpath

I would like to create a dictionary from an XML file unsing xpath. Here’s an example of the XML:

JavaScript

What I would like it’s having the contract ID as key and the unwanted patterns as value. Here’s my code:

JavaScript

I get the error “unhashable type: ‘list'”. Thank you for your help, the output should look like that:

{1: 0,1

2: 0,1}

Advertisement

Answer

xpath returns list, so instead of

JavaScript

try

JavaScript

As for output, as dictionary cannot have multiple values with the same key DictUP[key] = UnwantedPatterns.append(patterns.text) will overwrite value on each iteration.

Try

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement