Skip to content

Instantly share code, notes, and snippets.

@joac
Created June 4, 2013 16:50
Show Gist options
  • Save joac/5707530 to your computer and use it in GitHub Desktop.
Save joac/5707530 to your computer and use it in GitHub Desktop.
indent for element tree
def indent(elem, level=0):
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment