TypeError: name() argument 1 must be a unicode character, not str

TypeError: name() argument 1 must be a unicode character, not str

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

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

我的问题:我使用了unicodedata.name(string),但出现错误TypeError: name() argument 1 must be a unicode character, not 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''))

我相信你可以在 python 3.

中将所有表情符号字符视为普通字符

无法测试代码 atm,但我认为这应该可以。

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)