Do you know about the codecs module? reading http://pymotw.com/2/codecs/ may be useful if this is new to you. Have you read https://www.python.org/dev/peps/pep-0293/ ? Will backslashreplace do what you want? Laura