如何将表情符号的Unicode转换为CLDR短名称

时间:2020-04-30 13:25:59

标签: python unicode emoji extraction data-extraction

我正在使用python提取注释并显示它们。 打印时看起来像这样。

This was heart wrenching \u2764\ufe0f
Amazing compassion \ud83d\udc9c\ud83d\udc9c\ud83d\udc9c #tears
\u2764\ufe0f\u2764\ufe0f\u2764\ufe0f

如何将表情符号的unicode转换为其各自的CLDR简称? 例如,U + 1F44D将打印为竖起大拇指。

1 个答案:

答案 0 :(得分:2)

编辑:我认为我找到了代码\ud83d\udc9c

的解决方案
text = text.encode('utf-16', 'surrogatepass').decode('utf-16')

它将替代值\ud83d\udc9c转换为正确的表情符号值\U0001f49c

来源:How to work with surrogate pairs in Python?

维基百科:Surrogate

其他:Unicode character inspector


使用我找到的Google

print('\U0001F44D'.encode('ascii', 'namereplace').decode())

结果

\N{THUMBS UP SIGN}

还有

import unicodedata

print(unicodedata.name('\U0001F44D'))

结果:

THUMBS UP SIGN

因此,在询问Stackoverflow之前最好使用Google

https://docs.python.org/3/howto/unicode.html


文字相同

text = '''This was heart wrenching \u2764\ufe0f
Amazing compassion \ud83d\udc9c\ud83d\udc9c\ud83d\udc9c #tears
\u2764\ufe0f\u2764\ufe0f\u2764\ufe0f'''

print(text.encode('ascii', 'namereplace').decode())

结果:

This was heart wrenching \N{HEAVY BLACK HEART}\N{VARIATION SELECTOR-16}
Amazing compassion \ud83d\udc9c\ud83d\udc9c\ud83d\udc9c #tears
\N{HEAVY BLACK HEART}\N{VARIATION SELECTOR-16}\N{HEAVY BLACK HEART}\N{VARIATION SELECTOR-16}\N{HEAVY BLACK HEART}\N{VARIATION SELECTOR-16}
\N{THUMBS UP SIGN}

现在您可能必须删除\N{}

但是\ud83d\udc9c\ud83d\udc9c\ud83d\udc9c

有问题

您可以在unicodedata循环中使用for来获取文本中每个字符的名称,但是如果没有名称,则可能会出现问题。 '\n'。而且它还为普通字符指定名称,因此您可能必须使用unicodedata.category()来确定要替换的字符。

\ud83d\udc9c\ud83d\udc9c\ud83d\udc9c

也有问题
import unicodedata

# http://www.unicode.org/reports/tr44/#General_Category_Values

for char in text:
    try:
        print(char, '|', unicodedata.category(char), '|', unicodedata.name(char))
    except ValueError:
        print(repr(char), '| (repr)')

结果:

T | Lu | LATIN CAPITAL LETTER T
h | Ll | LATIN SMALL LETTER H
i | Ll | LATIN SMALL LETTER I
s | Ll | LATIN SMALL LETTER S
  | Zs | SPACE
w | Ll | LATIN SMALL LETTER W
a | Ll | LATIN SMALL LETTER A
s | Ll | LATIN SMALL LETTER S
  | Zs | SPACE
h | Ll | LATIN SMALL LETTER H
e | Ll | LATIN SMALL LETTER E
a | Ll | LATIN SMALL LETTER A
r | Ll | LATIN SMALL LETTER R
t | Ll | LATIN SMALL LETTER T
  | Zs | SPACE
w | Ll | LATIN SMALL LETTER W
r | Ll | LATIN SMALL LETTER R
e | Ll | LATIN SMALL LETTER E
n | Ll | LATIN SMALL LETTER N
c | Ll | LATIN SMALL LETTER C
h | Ll | LATIN SMALL LETTER H
i | Ll | LATIN SMALL LETTER I
n | Ll | LATIN SMALL LETTER N
g | Ll | LATIN SMALL LETTER G
  | Zs | SPACE
❤ | So | HEAVY BLACK HEART
️ | Mn | VARIATION SELECTOR-16
'\n' | (repr)
A | Lu | LATIN CAPITAL LETTER A
m | Ll | LATIN SMALL LETTER M
a | Ll | LATIN SMALL LETTER A
z | Ll | LATIN SMALL LETTER Z
i | Ll | LATIN SMALL LETTER I
n | Ll | LATIN SMALL LETTER N
g | Ll | LATIN SMALL LETTER G
  | Zs | SPACE
c | Ll | LATIN SMALL LETTER C
o | Ll | LATIN SMALL LETTER O
m | Ll | LATIN SMALL LETTER M
p | Ll | LATIN SMALL LETTER P
a | Ll | LATIN SMALL LETTER A
s | Ll | LATIN SMALL LETTER S
s | Ll | LATIN SMALL LETTER S
i | Ll | LATIN SMALL LETTER I
o | Ll | LATIN SMALL LETTER O
n | Ll | LATIN SMALL LETTER N
  | Zs | SPACE
'\ud83d' | (repr)
'\udc9c' | (repr)
'\ud83d' | (repr)
'\udc9c' | (repr)
'\ud83d' | (repr)
'\udc9c' | (repr)
  | Zs | SPACE
# | Po | NUMBER SIGN
t | Ll | LATIN SMALL LETTER T
e | Ll | LATIN SMALL LETTER E
a | Ll | LATIN SMALL LETTER A
r | Ll | LATIN SMALL LETTER R
s | Ll | LATIN SMALL LETTER S
'\n' | (repr)
❤ | So | HEAVY BLACK HEART
️ | Mn | VARIATION SELECTOR-16
❤ | So | HEAVY BLACK HEART
️ | Mn | VARIATION SELECTOR-16
❤ | So | HEAVY BLACK HEART
️ | Mn | VARIATION SELECTOR-16

由于\ud83d\udc9c\ud83d\udc9c\ud83d\udc9c出现问题,因此我将其替换为?

import unicodedata

text = '''This was heart wrenching \u2764\ufe0f
Amazing compassion \ud83d\udc9c\ud83d\udc9c\ud83d\udc9c #tears
\u2764\ufe0f\u2764\ufe0f\u2764\ufe0f'''

result = []

for char in text:
    if unicodedata.category(char) in ('So', 'Mn'):
        result.append(':{}:'.format(unicodedata.name(char)))
    elif unicodedata.category(char) in ('Cs'):
        result.append('?') #char)
    else:
        result.append(char)

print(''.join(result)) 

结果:

This was heart wrenching :HEAVY BLACK HEART::VARIATION SELECTOR-16:
Amazing compassion ?????? #tears
:HEAVY BLACK HEART::VARIATION SELECTOR-16::HEAVY BLACK HEART::VARIATION SELECTOR-16::HEAVY BLACK HEART::VARIATION SELECTOR-16:

编辑:再次使用Google,我发现外部模块emoji可以转换一些名称,但是\ud83d\udc9c也有问题,因此我使用repr来显示它-但它还会将新行打印为\n

text = '''This was heart wrenching \u2764\ufe0f
Amazing compassion \ud83d\udc9c\ud83d\udc9c\ud83d\udc9c #tears
\u2764\ufe0f\u2764\ufe0f\u2764\ufe0f'''

import emoji

#print( repr(emoji.demojize(text, use_aliases=True)) ) 
print( repr(emoji.demojize(text)) ) 

结果:

'This was heart wrenching :heart:\nAmazing compassion \ud83d\udc9c\ud83d\udc9c\ud83d\udc9c #tears\n:heart::heart::heart:'

http://www.unicode.org/emoji/charts/full-emoji-list.html

https://www.webfx.com/tools/emoji-cheat-sheet/

http://unicode.org/Public/emoji/12.0/emoji-test.txt


顺便说一句::我找到了demoji模块,该模块可以找到表情符号并给出名称。但这也与代码\ud83d\udc9c

有关
import demoji

# run only once after installing module
demoji.download_codes()

print(demoji.findall(text))

安装模块后,只需一次demoji.download_codes()

结果:

{'❤️': 'red heart'}

如果您以JSON数据"\ud83d\udc9c"的形式获取它,那么您应该没问题-它应该自动将其转换

import json

# escaped unicode in " "  
data = r'"\ud83d\udc9c"' 
print(json.loads(data))

在其他情况下,您必须将其转换

# convert to escaped unicode and put in " "  
data = '"{}"'.format('\ud83d\udc9c'.encode('unicode-escape').decode())
print(json.loads(data))

How to work with surrogate pairs in Python?