S
S
Sergey Aganezov2010-12-11 09:34:25
Python
Sergey Aganezov, 2010-12-11 09:34:25

Python + Cyrillic?

Situation: there is a console application in Python (version 2.7). During operation, the application should output (no need to read, I don’t know if this is important) data to the console, including text in Russian. Problem

def __str__(self):
        return "%s |(%s)|"  % (self.name, self.id)

here self.name is a string that can contain Russian characters.
Without any conversions at all, an error occurs:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

If we replace the code with:
def __str__(self):
        return "%s |(%s)|"  % (repr(self.name.encode("UTF-8")), self.id)

then the exception is not thrown, but not Cyrillic characters are output to the console, but their byte representation.
Example:
'\xd0\x95\xd0\xbb\xd0\xb5\xd0\xbd\xd0\xb0 S.' |(157927927)|
Instead:
Elena S.
How to solve such a problem? There are no problems with Latin, the same code does not output a byte representation, but normal Latin characters characters.
By the way, what can you read about this?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
shsmad, 2010-12-11
@shsmad

#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Foo(object):
    def __init__(self, name):
  self.name = name
  
    def __str__(self):
  return 'str: %s' % self.name
  
    def __unicode__(self):
        return 'uni: %s' % self.name.decode('utf-8')

    def __repr__(self):
  return 'repr: %s' % self.name

a = 'Елена S'
b = Foo(a)

print(str(b))
print(unicode(b))
print(repr(b))

Outputs:
str: Elena S
uni: Elena S
repr: Elena S

D
Denis, 2015-11-30
@Norkotik

what if a = u'elena s'?

V
Vasily Kozlov, 2016-05-19
@Quis_sum_ego

Dear author!
I thought long and hard about what's going on!
until I read your article
versin python 2
-*- coding: utf-8 -*-
word="words"
print 'word'
alas it didn't support language support modules !
and my problem was that when working with the eclipse mars program, he convulsively showed all the words output by the program as an error and gave out the number of the memory cell code in which this obscure object was stored.
that's what helped!
sudo apt-get install python 3
and of course changing the python 2 compiler to a newer version!

T
toga, 2016-10-17
@toga

Suddenly it will come in handy for someone - in order for python2 not to swear, you need to write at the beginning of the class

@python_2_unicode_compatible
class AuthorModel(models.Model):

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question