gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:03:55 25 4
gpt4 key购买 nike

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

Full emoji data

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

u_str = u'\U0001F001'
u_str = u'\U0001F002'

(当然以编程方式生成)

并打印(循环):

print(u'\U0001F001', u_str, ' ', unicodedata.name(u_str))
print(u'\U0001F002', u_str, ' ', unicodedata.name(u_str))

如果能够使用 unicodedata 和某些属性(例如 unicodedata.hex_representation),那么我只会使用它,但如果 unicodedata 中有该属性,我不明白查看它的规范。

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

how-does-one-print-a-unicode-character-code-in-python

我尝试:

>>> print(u_str.encode('raw_unicode_escape'))
b'\\U0001f600'

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

u_str = u'\U0001F600'

这可能吗,还是有其他方法来实现表的构建?

最佳答案

使用 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 🀄

关于python-3.x - 在Python 3中如何将unicode代码点打印为u'\U...',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42757013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com