Answer the question
In order to leave comments, you need to log in
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>")
<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>
Answer the question
In order to leave comments, you need to log in
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())
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
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>")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question