Created
July 9, 2012 22:42
-
-
Save anonymous/3079509 to your computer and use it in GitHub Desktop.
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
import sys | |
import xml.etree.cElementTree as ET | |
def format_start(elem): | |
attrs = ' '.join('{0}={1}'.format(k, v) | |
for k, v in elem.items()) | |
return ('<{tag} {attrs}>'.format(tag=elem.tag, attrs=attrs) | |
if attrs else '<{tag}>'.format(tag=elem.tag)) | |
def format_end(elem): | |
return '</{0}>'.format(elem.tag) | |
def pretty_print(fn, indent=2): | |
cursor = 0 | |
for event, elem in ET.iterparse(fn, events=('start', 'end')): | |
if event == 'start': | |
print ' ' * cursor, format_start(elem) | |
cursor += indent | |
else: | |
cursor -= indent | |
print ' ' * cursor, format_end(elem) | |
if __name__ == '__main__': | |
pretty_print(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment