Situation: there is a console application written in Python (version 2.7). During the work the application should display the (reading not necessary, I do not know if it is important) the data to the console, including the text in Russian. The problem
def __str__(self): return "%s |(%s)|" % (self.name, self.id)
here self.name — a string, which can be Russian characters.
Without any conversions at all — there is a bug:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
If you replace the code to:
def __str__(self): return "%s |(%s)|" % (repr(self.name.encode("UTF-8")), self.id)
the exclusion does not crashes, but the console are not displayed Cyrillic characters and their byte representation.
Example:
'\\xd0\\x95\\xd0\\xbb\\xd0\\xb5\\xd0\\xbd\\xd0\\xb0 S.' |(157927927)|Instead of:
Elena S. How to solve this problem? Latin is no problem, the same code outputs is not a byte representation, and the normal Latin characters.
That way you can read about this?