Skip to content

Instantly share code, notes, and snippets.

@Prasang-money
Forked from jacobian/elem2dict.py
Created November 16, 2021 12:34
Show Gist options
  • Save Prasang-money/4812c11add25eef7ec995471fec9a3d6 to your computer and use it in GitHub Desktop.
Save Prasang-money/4812c11add25eef7ec995471fec9a3d6 to your computer and use it in GitHub Desktop.
Convert an lxml.etree node tree into a dict.
def elem2dict(node):
"""
Convert an lxml.etree node tree into a dict.
"""
d = {}
for e in node.iterchildren():
key = e.tag.split('}')[1] if '}' in e.tag else e.tag
value = e.text if e.text else elem2dict(e)
d[key] = value
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment