Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode output in ipython notebook

I have to work with Unicode (cyrillic) characters in IPython Notebook. Are there any way to output strings in Unicode, not their unicode or utf8 codes? I'd like to have ["АБ","ВГ"] as output in the last two examples below.

In [62]: "АБВ"

Out[62]: '\xd0\x90\xd0\x91\xd0\x92'

In [63]: u"АБВ"

Out[63]: u'\u0410\u0411\u0412'

In [64]: print "АБВ"

АБВ

In [65]: print u"АБВ"

АБВ

In [66]: print ["АБ","ВГ"]

['\xd0\x90\xd0\x91', '\xd0\x92\xd0\x93']

In [67]: print [u"АБ",u"ВГ"]

[u'\u0410\u0411', u'\u0412\u0413']
like image 569
Ilya V. Schurov Avatar asked Feb 03 '14 23:02

Ilya V. Schurov


2 Answers

In Python 2 (with or without IPython), you can use the unicode_escape string codec to mimic proper Unicode reprs:

In [1]: print repr([u"АБ",u"ВГ"]).decode('unicode_escape')

[u'АБ', u'ВГ']

https://docs.python.org/2/library/codecs.html#python-specific-encodings

Unlike Mark Ransom's solution, this works for most common data types (including e.g. dictionaries). Note that this prevents IPython's nice formatting of large data structures, though.

like image 113
futurulus Avatar answered Oct 15 '22 03:10

futurulus


You have to switch to Python 3 and get nice repr's of Unicode strings.

like image 20
Ilya V. Schurov Avatar answered Oct 15 '22 05:10

Ilya V. Schurov