Reading (writing) unicode text from (to) files
You want to write some special characters to a file (e.g.
f.write("voilá")) but you get immediately some unicode error in your face.
Instead of messing with the encode, decode methods, use the codecs module.
import codecs # read with codecs.open(fname, "r", "utf-8") as f: text = f.read() # write with codecs.open(tmp, "w", "utf-8") as to: to.write(text)
As can be seen, its usage is very similar to the well-known
This tip is from here.