Python - problems with utf-8 encoding

How do I get python to work with utf-8 encoding?

The file.pyc file is generated (or not generated, I didn't understand it myself) with win1251 headers from utf file.py

The actual source file. Maybe it will help. The file is encoded in utf-8 without BOM

#!/usr/bin/python
# -*- coding: utf-8 -*-
import string
import sys

print ('''Content-type: text/html''')
print ('''
<html>
<head>
<title>Проверка Python</title>
</head>
<body>
Русский текст - Nerusskii text<br>
<ul>''')

And in the browser I get a page with headers

Content-Type:text/html; charset=UTF-8

And the content. (The browser looks in utf-8, and the file is win1251)

<html>

<head>
 <title>�������� Python</title>
</head>
<body>
������� ����� - Nerusskii text<br>
<ul>

UP (After many years) The problem still turned out to be in Apache.

The solution was found tut

AddDefaultCharset UTF-8    
SetEnv PYTHONIOENCODING utf8
Author: Дух сообщества, 2013-09-23

2 answers

Try adding py{[2] to the header]}

## -*- coding: utf-8 -*-

And save the file in utf-8.

 5
Author: deterok, 2013-09-23 05:01:32
  1. Actually, in what program is the code itself written?
  2. You tried experimenting, for example, specifying the encoding as win1251 in the headers. Russian Russian letters in UTF (I remember earlier in Windows XP because of such gestures, by the way, as in Whist, it was necessary to specify the font in the system rigidly, since many applications not only did not know about Russian but also about many other languages). It is also possible that the font used in the browser does not know anything about Russian letters in UTF (I remember earlier in Windows XP because of such gestures, by the way, as in Whist, I had to specify the font in the system rigidly, since many applications not only did not know about Russian, but also about many other languages.)
  3. It's also worth a try. specify the print function about the encoding used for the string.
  4. It is also not entirely clear where the print function outputs your messages, it is possible that the output does not know about Russian letters. Russian Russian letters (when the "system" does not know about Russian letters and receives at least a letter in the form of a set of bytes, it splits it into two (remember SMS, you can write English characters more than Russian) and we get 2 completely independent characters at the output, and since they exist, then naturally in the future they are both used as 2 completely different characters and not our Russian letter. although in your case it seems that this is not quite the case, since the number of characters here is not 2 times more, but I think the principle is clear to you.)
 3
Author: Xokyopo, 2013-12-27 22:31:22