在 Python 3 中如何将 unicode 代码点打印为 u'\U...'

mcw*_*ard 4 unicode utf-16 python-3.x

无论出于何种原因,我认为创建一个我感兴趣的表情符号表会很好。第一列是代码点,第二列是表情符号,第三列是名称。与此网页类似的内容,但适合我的使用。

完整的表情数据

假设我弄清楚如何迭代代码点(还有其他问题或者我构建了一个感兴趣的列表)然后我将循环遍历代码点,例如

u_str = u'\U0001F001'
u_str = u'\U0001F002'
Run Code Online (Sandbox Code Playgroud)

(当然以编程方式生成)

并打印(循环):

print(u'\U0001F001', u_str, ' ', unicodedata.name(u_str))
print(u'\U0001F002', u_str, ' ', unicodedata.name(u_str))
Run Code Online (Sandbox Code Playgroud)

如果有能力使用 unicodedata 和一些属性,如 unicodedata.hex_representation 那么我会使用它,但如果 unicodedata 中有该属性,我不明白看到它的规范。

所以在寻找答案时,我发现了这个问题:

怎么做-一个打印-a-unicode-character-code-in-python

我尝试:

>>> print(u_str.encode('raw_unicode_escape'))
b'\\U0001f600'
Run Code Online (Sandbox Code Playgroud)

我正在寻找的是我放入的内容:

u_str = u'\U0001F600'
Run Code Online (Sandbox Code Playgroud)

这是可能的还是有其他方法可以实现表格的构建?

Mar*_*nen 6

使用 Python 3.6+:

>>> for i in range(0x1f001,0x1f005):
>>>     print(f'U+{i:04X} \\U{i:08X} {chr(i)}')
U+1F001 \U0001F001 
U+1F002 \U0001F002 
U+1F003 \U0001F003 
U+1F004 \U0001F004 
Run Code Online (Sandbox Code Playgroud)