Warm tip: This article is reproduced from stackoverflow.com, please click
encoding list python python-2.7 unicode

Cyrillic encoding python 2.7 array

发布于 2020-03-29 21:03:57

My code so far:

data = [(u'Rest', u'русский', u'фввв', u'vc'), (u'Rest', u'русский', u'фввв ', u'vc')]
print(data)

result:

[(u'Rest', u'\u0440\u0443\u0441\u0441\u043a\u0438\u0439', u'\u0444\u0432\u0432\u0432', u'vc'), (u'Rest', u'\u0440\u0443\u0441\u0441\u043a\u0438\u0439', u'\u0444\u0432\u0432\u0432 ', u'vc')]

I want the output to display the cyrillic characters, like so:

[('Rest', 'русский', 'фввв', 'vc'), ('Rest', 'русский', 'фввв ', 'vc')]
Questioner
user357670
Viewed
114
CDJB 2020-01-31 19:05

This is happening because when we print out a list or tuple, the representation of the elements within the list is defined by the element's __repr__ function, rather than its __str__ function. To fix this, you can use the following to encode the strings and then decode the repr() of the list.

Code:

# -*- coding: utf-8 -*-
import sys
data = [(u'Rest', u'русский', u'фввв', u'vc'), (u'Rest', u'русский', u'фввв ', u'vc')]
print repr([tuple(x.encode(sys.stdout.encoding) for x in sl) for sl in data]).decode('string-escape')

Out:

[('Rest', 'русский', 'фввв', 'vc'), ('Rest', 'русский', 'фввв ', 'vc')]