I
I
Ivan Gorokhov2013-11-25 19:21:34
Python
Ivan Gorokhov, 2013-11-25 19:21:34

Why is there a problem with Russian letters in puthon 3.3?

I just started learning pythona and immediately stumbled upon a problem that I can't solve on my own.
We have code:

#!C:/OpenServer/modules/phyton/python.exe
# -*- coding: utf-8 -*-

print ("Content-Type: text/html; charset=utf-8\n")
print ("<html><head><title>And Now for Something Completely Different…</title></head><body>")
print ("<h1> Python  works!</h1>")
print ("<p>Hello Wordl!</p>")

print('Привет'.encode("utf-8"))

print ("<p>Привет МИР!</p>")
print ("</body></html>")

The interpreter is installed, the Win7 operating system, the Apache web server from the Openserser package, python is connected to the server, the code is being executed.
But the problem is with Russian letters, they are not displayed.
Here is what is displayed in the browser
<html><head><title>And Now for Something Completely Different�</title></head><body>
<h1> Python  works!</h1>
<p>Hello Wordl!</p>
b'\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82'
b'<p>\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82 \xd0\x9c\xd0\x98\xd0\xa0!</p>'
</body></html>

The file is saved in Utf-8 encoding with BOM, Sublime Text 3 editor
Who can tell me how to figure out the encoding?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Vasiliev, 2013-11-26
@Dead_Angel

The conversion happens because the stdout encoding doesn't match what you write there.
Check sys.stdout.encoding. If there is something left or just empty, try setting the encoding by force:

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

Well, tell me later, how did it get so messed up with you that stdout writes like that.

V
Vsevolod, 2013-11-25
@sevka_fedoroff

It seems to me that if you have this at the beginning of the file:
and the file itself is encoded in UTF-8, then it should work simply.
Unless you add this to the HTML code:
PS And try to remove the BOM

Z
zxmd, 2013-11-25
@zxmd

Try like this:

#coding=utf-8
print ("Content-Type: text/html; charset=utf-8\n")
print ("<html><head><title>And Now for Something Completely Different…</title></head><body>")
print (u"<p>Привет МИР!</p>")

L
lightman, 2013-11-26
@lightman

Author, try to output without encode:
print('Hi')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question