file.write() of non-ASCII characters differs in Interpreted Python than in script run

Chris Angelico rosuav at gmail.com
Wed Aug 26 01:16:10 CEST 2015


On Wed, Aug 26, 2015 at 7:19 AM, RAH <rene.heymans at gmail.com> wrote:
>     rb = request_body.decode()                                      # string

I'd recommend avoiding this operation in Python 2. As of Python 3,
omitting the encoding means "UTF-8", but in Python 2 it means "use the
default encoding", and that often causes problems in scripts that run
in various background environments. Instead, explicitly say what
encoding you're using:

rb = request_body.decode("UTF-8")

Conversely, if you're using Python 3 for this, the default encoding is
coming from this line:

> h = open('logdict', 'a')

Again, if you want this to be a text file with a specific encoding, say so:

h = open('logdict', 'a', encoding='UTF-8')

Give that a try and see if your problems disappear. If not, this
should at least poke them with a pointy stick.

ChrisA


More information about the Python-list mailing list