Python TypeError:name()参数1必须是unicode字符,而不是str

Python TypeError:name()参数1必须是unicode字符,而不是str,python,unicode,utf-8,Python,Unicode,Utf 8,您好,我正试图在Linux上的python 3.7中使用unicodedata,但不幸的是,它失败了。非常感谢您的帮助 我在网上寻找同样的问题,但我找不到任何提示,指出我在正确的方向 我的问题:我使用了unicodedata.name(string),在那里我得到一个错误TypeError:name()参数1必须是unicode字符,而不是str 最小工作示例 #!/usr/bin/env python3 import re import emoji import unicodedata

您好,我正试图在Linux上的python 3.7中使用
unicodedata
,但不幸的是,它失败了。非常感谢您的帮助

我在网上寻找同样的问题,但我找不到任何提示,指出我在正确的方向

我的问题:我使用了
unicodedata.name(string)
,在那里我得到一个错误
TypeError:name()参数1必须是unicode字符,而不是str

最小工作示例

#!/usr/bin/env python3

import re
import emoji
import unicodedata


def replace_emoji(document):
    emoji_all = emoji.EMOJI_ALIAS_UNICODE.items()
    emoji_items = []

    emoji_pattern = re.compile(u'|'.join(
        re.escape(u[1]) for u in emoji_all), flags=re.UNICODE)
    emoji_items = re.findall(emoji_pattern, document)

    for item in emoji_items:
        unicodes = []
        unicode_values = []

        for char in range(len(item)):
            if not len(item) > 1:
                unicodes.append(r'{:x}'.format(ord(item[char])).upper())

            unicode_values.append([hex(ord(x)) for x in item[char]][0])

        char_length = len(unicode_values)

        chars = [chr(int(u, 16)) for u in unicode_values]

        if char_length == 2:
            print(chars)

            value = u'\\U{:x}\\U{:x}'.format(
                ord(chars[0]), ord(chars[1])).upper()

            unicodedata.name(value)

    return document
我的测试运行

print(replace_emoji(u'I believe you can treat all emoji chars as normal characters in python 3.

Can't test the code atm, but I think this should do it.

import emoji
import unicodedata


def replace_emojis(document):
    emoji_chars = emoji.EMOJI_ALIAS_UNICODE.values()

    def _emoji(char):
        if char in emoji_chars:
            return unicodedata.name(char)

    return ''.join(_emoji(char) or char for char in document)

print(replace_-emoji(u'我相信您可以将所有表情符号视为python 3中的普通字符

无法测试atm的代码,但我认为应该这样做


我相信在Python3中,您可以将所有表情符号字符视为普通字符

无法测试atm的代码,但我认为应该这样做


你期望的输出是什么?为了它的价值(它没有回答你的问题),在Python 3中,使用
u'|'is'|'
并使用
flags=re.UNICODE
是多余的,因为字符串是UNICODE开头的。@Johan Schiff我期望的输出是使用
print(replace\emoji)时的表情符号的名称,比如
HAMBURGER
(u'Thank@Booboo我会改变这一点,老实说,我对python非常陌生。您的预期输出是什么?它的价值是什么(它没有回答您的问题),在Python 3中,
u'|'是“|”
并且使用
flags=re。UNICODE是多余的,因为字符串的开头是UNICODE的。@Johan Schiff当我使用
print(替换\u emoji)时,我的预期输出是类似于
汉堡包的表情符号的名称(u'Thank@Booboo我会改变这一点,老实说,我对python非常陌生。非常感谢这正是返回的预期输出,如
区域指标符号字母J
区域指标符号字母P
。我想知道是否有一个名称,一个名称,用于表示感谢。非常感谢,这正是返回的预期输出,如
区域指标符号字母J
区域指标符号字母P
。我想知道是否有一个名称