Skip to content

Instantly share code, notes, and snippets.

@audy
Created October 22, 2018 19:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audy/9e0e087930cdad74a5d7e58649ac136f to your computer and use it in GitHub Desktop.
Save audy/9e0e087930cdad74a5d7e58649ac136f to your computer and use it in GitHub Desktop.
Convert nested XML data into dictionaries
#!/usr/bin/env python3
import xml.etree.ElementTree as ET
from pprint import pprint
import code
tree = ET.parse('biosample_result.xml')
def expand_blob(blob, attributes={}):
'''
convert an XML node (and all of its children) into a nested
dictionary
'''
attributes[blob.tag] = blob.attrib
for child in blob:
expand_blob(child, attributes)
return attributes
for biosample in tree.getroot():
attributes = {}
pprint(expand_blob(biosample, attributes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment