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

标签 python-3.x unicode utf-16

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

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/

相关文章:

python - PyCharm 认为此 RegEx 具有 Duplicate character is character class。是不是bug?

python - 无法在 google chrome 网页上找到元素

python - 请求响应中的非 'ascii' 字符

utf-8 - 是否存在可以用 UTF-16 编码但不能用 UTF-8 编码的字符?

python - 位置参数和 self 关键字

java - 如何在从数据库检索的页面中显示阿拉伯语 unicode 文本

android - 在输入流中解码\u0000 Unicode 的更优雅方式

Swift 将 UTF16 转换为 UTF8 并返回

python - Python UTF-16 输出和 Windows 行结尾有错误吗?

python - 检查 QColorDialog 是否被取消