【问题标题】:How can I print the decimal representation of a unicode string?如何打印 unicode 字符串的十进制表示?
【发布时间】:2015-05-04 22:43:32
【问题描述】:

我正在尝试比较 Python 中的 unicode 字符串。由于很多符号看起来很相似,而且有些可能包含不可打印的字符,所以我在调试比较失败的地方时遇到了麻烦。有没有办法获取一串 unicode 字符并打印它们的 unicode 代码?即:

>>> unicode_print('❄')
'\u2744'

【问题讨论】:

    标签: python string unicode


    【解决方案1】:

    您可以使用其他编码对该字符串进行编码:

    >>> s = '❄'
    >>> s.encode() # "utf8" by default
    b'\xe2\x9d\x84'
    

    对于您指定的输出,我刚刚从here 找到了这个:

    >>> s.encode("unicode_escape")
    b'\\u2744'
    

    【讨论】:

      猜你喜欢
      • 2013-01-16
      • 1970-01-01
      • 2013-12-01
      • 2014-07-06
      • 2011-08-19
      • 2017-05-16
      • 2014-10-07
      • 1970-01-01
      相关资源
      最近更新 更多