Unicode output in ipython notebook
Solution 1
In Python 2 (with or without IPython), you can use the unicode_escape
string codec to mimic proper Unicode repr
s:
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.
Solution 2
You have to switch to Python 3 and get nice repr
's of Unicode strings.
Solution 3
Don't print out a whole list, print out each element of the list separately. Or convert the list to a string:
print u'[' + u','.join(string_list) + u']'
Ilya V. Schurov
Updated on July 01, 2022Comments
-
Ilya V. Schurov almost 2 years
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']
-
Ilya V. Schurov about 10 yearsThanks, I believe it will work, but I'd be happy to find a nicer solution — maybe, some option in ipython's preferences, i don't know… For example, if I need to print more complicated data structure, it will be trouble to use this method. It seems to be generic problem — there should be possibility to output unicode strings in a nice way.
-
Mark Ransom about 10 years@ilyavschurov, sorry Python offers no such option. You're seeing the difference between
str
andrepr
. -
Mark Ransom over 8 yearsYou're right, I forgot (or didn't yet know) about that advantage to Python 3.
-
matanster almost 7 yearsWhat does it mean if this does not work in a python 2 Jupiter notebook?
-
futurulus almost 7 years@matanster Can you give more details about the problem? It still works for me in Jupyter 4.3.0, Python 2.7.12.