what is wrong in my code?? (python 3.3)

Piet van Oostrum piet at vanoostrum.org
Mon Sep 30 14:00:14 EDT 2013


dream4soul at gmail.com writes:

> I rename file from test.py in test.txt and all works fine. So clearly problem it is not in file coding or browser. ANY IDEAS??

It looks like the encoding of stdout is not utf-8 in the CGI script. Check it with

import sys
print(sys.stdout.encoding)

If it's not utf-8, you must force your output to be utf-8, as that is what the browser expects, because of your Content-type.

You could use: 
sys.stdout.buffer.write('ранее предусматривалась смертная казнь.'.encode('utf-8'))

Or to change stdout into utf-8 encoding:

import codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())

[Note: I haven't tested this]
-- 
Piet van Oostrum <piet at vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list