Answer the question
In order to leave comments, you need to log in
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.
Answer the question
In order to leave comments, you need to log in
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?
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
How to solve my problem?
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.
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())
# -*- encoding: utf-8 -*-
from __future__ import print_function
import sys
print(sys.version)
print(u'Здравствуй жопа, новый год!')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question