简体   繁体   English

ipython笔记本中的Unicode输出

[英]Unicode output in ipython notebook

I have to work with Unicode (cyrillic) characters in IPython Notebook. 我必须在IPython Notebook中使用Unicode(西里尔文)字符。 Are there any way to output strings in Unicode, not their unicode or utf8 codes? 有没有办法用Unicode输出字符串,而不是unicode或utf8代码? 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']

In Python 2 (with or without IPython), you can use the unicode_escape string codec to mimic proper Unicode repr s: 在Python 2(使用或不使用IPython)中,您可以使用unicode_escape字符串编解码器来模拟正确的Unicode repr

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

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

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

Unlike Mark Ransom's solution, this works for most common data types (including eg dictionaries). 与Mark Ransom的解决方案不同,这适用于大多数常见数据类型(包括例如字典)。 Note that this prevents IPython's nice formatting of large data structures, though. 请注意,这可以防止IPython很好地格式化大型数据结构。

你必须切换到Python 3并获得很好的Unicode字符串repr

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']'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM