M
M
Musgem2016-02-12 13:04:36
Python
Musgem, 2016-02-12 13:04:36

VS + Python + Cyrillic, error in console, how to solve?

Hello!
For the sake of interest, I installed Python 3.5.1 - (I decided to see what kind of language it was, I heard a lot about it)
So, when you output the program in the console, I get an error, as I understand it, because of the Russian language and because of the encoding ... When you write in English everything is fine - of course you can write everything in it! and hit the problem. But, after all, we are Russian) and I want to write messages in my native language.
I googled this topic, many people in many programming languages ​​​​have a problem precisely because of Russian letters, everyone solves this problem in different ways ...
Gentlemen! question how to solve my problem?
Thank you.
ccfa8746d7a742d1bf77066a09875e07.jpg

Answer the question

In order to leave comments, you need to log in

6 answer(s)
M
Musgem, 2016-02-12
@Musgem

What I managed to dig up...
A.
if you put the following construction at the beginning of the code:
# -*- coding: cp1251 -*-
Then everything starts to work, as I understand it, this is a forced recoding of the file - (I will be grateful if you explain gentlemen more precisely)
B.
( as I understand it)
The file must be created in the desired utf8 encoding
By default, Visual Studio creates files in a different encoding, you need to understand where to change / check / test it ...
Yes, that's right! in Visual Studio, go to File - Additional save options
and set it like this (screen) everything starts working ..
But this does not save from such a record ... (screen) the question remains what to do?
For the sake of the test, I directly launched the python console - and entered the necessary command there, all the rules were displayed!
What's wrong with this damn VS?
34d1f4ac38fd47888e93d3d57f4cd19f.jpg

D
Dimonchik, 2016-02-12
@dimonchik2013

in the general case, the problem has no solution for a
long time, you can read here: Strange behavior of pycharm?
briefly: writing to the database - please, to a file (I don’t remember, you have to look), to the console - alas
, this, of course, if you just don’t have something messed up there that is decided by encode / decode
and, of course, in bytes - takes you anywhere

N
Nikita Konin, 2016-02-12
@jkjkjirf

How to solve my problem?

Use Linux as an option in a virtual machine and PyCharm.

R
Roman_Kh, 2016-02-12
@Roman_Kh

The construction # -*- coding: cp1251 -*-specifies the encoding of the file itself with the program code. Therefore, all lines in the text will be saved in 1251 encoding, and if you output to the console with 1251 encoding, then everything will be printed normally. However, if the console encoding is 866 or Unicode, then instead of Cyrillic you will get krakozyabry.
The problem with python is that different commands/functions encode/decode strings differently for some reason.
To deal with this, it is better to encode all strings in Unicode and output to the Unicode console. And use only commands with understandable behavior, such as print. Otherwise, sooner or later you will again encounter krakozyabry.

A
Andy_U, 2016-02-12
@Andy_U

Here is an excerpt from the documentation for Python 3.5.1:
Python » 3.5.1 Documentation » The Python Standard Library » 29. Python Runtime Services » sys
The character encoding is platform-dependent.
Under Windows, if the stream is interactive (that is, if
its isatty() method returns True), the console codepage is
used, otherwise the ANSI code page.
Under other platforms, the locale encoding is used (see
locale.getpreferredencoding()).
Under all platforms though, you can override this value
by setting the PYTHONIOENCODING environment variable
before starting Python.
When interactive, standard streams are line-buffered. Otherwise,
they are block-buffered like regular text files. You can override
this value with the -u command-line option.
Further, the current console encoding in Windows (for Russia it is cp866), well, the string for ANSI is obtained like this:

import ctypes

    def ansi_encoding():
        return 'cp'+str(ctypes.windll.kernel32.GetACP())

    def console_output_encoding():
        return 'cp'+str(ctypes.windll.kernel32.GetConsoleOutputCP())

The value of sys.stdout.encoding and other similar ones can also help ...
You can make the Windows console unicode with the chcp 65001 command.
Now about Pycharm: it sets the environment variable
PYTHONIOENCODING=utf-8
and its console is unicode. The font for the console I recommend is DejaVu Sans Mono - the only one that correctly displays formulas from sympy.
We already wrote about the fact that the text of the program should be in utf-8. The line about encoding is not necessary, but in order for both python 2 and python 3 to correctly display text even on the console, even in pycharm, the program should look like this:
# -*- encoding: utf-8 -*-

from __future__ import print_function
import sys

print(sys.version)
print(u'Здравствуй жопа, новый год!')

P
Plamea, 2021-12-02
@Plamea

ATTENTION the last working solution is to put the version vs 2019, the problem with Cyrillic will pass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question