How to get cyrillic in output, Python?

how to get cyrillic instead u'...

the code is like this

def openfile(filename):
    with codecs.open(filename, encoding="utf-8") as F:
        raw = F.read()
do stuff...
print some_text

prints

>>>[u'.', u',', u':', u'\u0432', u'<', u'>', u'(', u')', u'\u0437', u'\u0456']

+3
source share
3 answers

It looks like this some_textis a list of unicode objects. When you print such a list, it prints reprsitems inside the list. So try instead:

print(u''.join(some_text))

The join method combines elements some_textwith white space u''between elements. The result is a single unicode object.

+5
source

I don’t understand where it comes from some_text(you cut this bit of your code), so I have no idea why it prints as a list of characters, not a string.

, Python ASCII, . , - , :

>>> text = u'\u0410\u0430\u0411\u0431'
>>> print text
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3:
  ordinal not in range(128)
>>> print text.encode('utf8')

+2

u'\uNNNN' - ASCII- u'':

>>> print u'\u0437'

, , . Windows :

>>> print u'\u0437'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u0437' in position 0: character maps to <undefined>

Windows Unicode , Python 2 repr ASCII-safe.

print repr , . print , , u'...' .

0

Source: https://habr.com/ru/post/1773689/


All Articles