Encoding an output stream in Python

Sometimes you may need to encode an output stream. Maybe you're writing to a file and you need it in a certain encoding, or to cStringIO.StringIO, which only takes byte strings. Or basically when you're dealing with the dreaded UnicodeEncodeError. You can avoid this by wrapping your output stream in a class (i.e. a decorator) […]