When reading a file, you get back bytes. Python (at least on version 2.7) does not know reliably what encoding the file uses, so it does not convert them to unicode. By using `.decode('utf-8')` you tell it that the bytes in the file are utf-8 encoded and should be converted to unicode.

The `prettify_xml` function expects unicode objects and then calls `.encode('utf-8')` on them. If it receives a byte object and you run the encode method on it, it first needs to decode it in order to be able to encode it to the desired charset. It uses the default encoding (utf-8). So if you give the function an utf-8 encoded string, it will first get decoded and then encoded again. Stupid, but the code cannot know the encoding for sure without "asking" the user.

Moral der Geschichte: Every time some bytestrings goes into your program, decode it. Internally, always use unicode objects (Python 3 does this by default). When outputting any data (to stdout or to a file), encode them to bytestrings again, with the desired encoding. Doing this workflow consciently avoids many potential encoding bugs.