"plaintext" Java 源代码点如何以编程方式转换为 Python3 中的表情符号?
How can "plaintext" Java source codepoints be programmatically converted to Emoji in Python3?
我写了一个 Python3 脚本来提取 C/C++/Java 源 codepoints/surrogate 对表情符号字符的字符串(\ud83d\ude00
,例如)来自文本文件。
我在此脚本中还有一本字典,将表情符号映射到它们的描述 ("" => "grinning face")。如何将代理对(\ud83d\ude00
,字符串文字)转换为对应的表情符号,以便将它们用作访问字典中相应表情符号描述的键?
对于一些额外的信息,我提取字符串的方式是当我 运行 print(extracted_string)
时,控制台输出是 \ud83d\ude00
。当我尝试将表情符号键处的值分配给变量时,我收到错误消息:
description = dictionary[extracted_string]
KeyError: '\ud83d\ude00'
这需要一些挖掘和一大堆 encoding/decoding,但我发现了一些有用的东西:
extracted_string = '\ud83d\ude00' #String literal as read from file
emoji = extracted_string.encode().decode('unicode-escape').encode('utf-16', 'surrogatepass').decode('utf-16')
print(emoji)
输出:
从 @falestru's answer here:
稍作修改
这也和JSON的编码一样。
>>> import json
>>> json.loads('"\ud83d\ude00"')
''
我写了一个 Python3 脚本来提取 C/C++/Java 源 codepoints/surrogate 对表情符号字符的字符串(\ud83d\ude00
,例如)来自文本文件。
我在此脚本中还有一本字典,将表情符号映射到它们的描述 ("" => "grinning face")。如何将代理对(\ud83d\ude00
,字符串文字)转换为对应的表情符号,以便将它们用作访问字典中相应表情符号描述的键?
对于一些额外的信息,我提取字符串的方式是当我 运行 print(extracted_string)
时,控制台输出是 \ud83d\ude00
。当我尝试将表情符号键处的值分配给变量时,我收到错误消息:
description = dictionary[extracted_string]
KeyError: '\ud83d\ude00'
这需要一些挖掘和一大堆 encoding/decoding,但我发现了一些有用的东西:
extracted_string = '\ud83d\ude00' #String literal as read from file
emoji = extracted_string.encode().decode('unicode-escape').encode('utf-16', 'surrogatepass').decode('utf-16')
print(emoji)
输出:
从 @falestru's answer here:
稍作修改这也和JSON的编码一样。
>>> import json
>>> json.loads('"\ud83d\ude00"')
''