[Tutor] Reformatting a one (long) line xml file

Negroup - negroup at gmail.com
Thu Sep 29 08:43:19 CEST 2005


2005/9/28, Christopher Arndt <chris.arndt at web.de>:
[cut]
>
> See http://www.python.org/doc/current/lib/module-xml.dom.minidom.html for more
> info.

Hi, this seems to be exactly what I was looking for!

import sys
from xml.dom import minidom

INDENT = ' ' * 4

file = sys.argv[1]

content = open(file).read()

pretty = minidom.parseString(content).toprettyxml(indent=INDENT).encode('utf-8')
beautiful = open('%s.pretty' % file, 'w')
beautiful.write(pretty)
beautiful.close()

I looked at BeautifulSoup, as Adam suggested. However it seems very
powerful and probably *too* powerful for my actual simple needings.
Anyway I will remember for the future.

> HTH, Chris

Thanks both of you!


More information about the Tutor mailing list