Created
October 22, 2018 19:34
-
-
Save audy/9e0e087930cdad74a5d7e58649ac136f to your computer and use it in GitHub Desktop.
Convert nested XML data into dictionaries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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