I've been using a library that I found called lxml ( http://lxml.de/ ). It's far easier to work with compared to the built in XML processing in Python. It is also capable of working with unicode which I will need (I will be putting Japanese text in my XML :)).

So it was all going well, I've got a good deal more coding done tonight, but I got to outputting my XML to a file. It was pretty printing it fine for the first element I added and save or if I added more than one before saving. But then if I opened the file again to append another item it was not pretty printing the new item.

I had already come across this FAQ entry on the lxml website: http://lxml.de/FAQ.html#why-doesn-t-the-pretty-print-option-reformat-my-...

But for some reason I didn't read it fully, I spent longer than I should trying different things. It turned out it was the opening that was the issue. I just needed to add this option:

remove_blank_text=True

as explained on that page. It's annoying how something quite small can take up a lot of time.

Tags: Solved | Python | lxml