如何打印 unicode 字符串的十进制表示形式?

How can I print the decimal representation of a unicode string?

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

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

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

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

对于您指定的输出,我刚刚从 here:

中找到了这个
>>> s.encode("unicode_escape")
b'\u2744'