unicodedata --- Unicode 数据库


此模块提供了对 Unicode Character Database (UCD) 的访问,其中定义了所有 Unicode 字符的字符属性。 此数据库中包含的数据编译自 UCD 版本 16.0.0

该模块使用与 Unicode 标准附件 #44 “Unicode 字符数据库” 中所定义的相同名称和符号。 它定义了以下函数:

参见

The Unicode 指南 for more information about Unicode and how to use this module.

unicodedata.lookup(name)

Look up character by name. If a character with the given name is found, return the corresponding character. If not found, KeyError is raised. For example:

>>> unicodedata.lookup('LEFT CURLY BRACKET')
'{'

The characters returned by this function are the same as those produced by \N escape sequence in string literals. For example:

>>> unicodedata.lookup('MIDDLE DOT') == '\N{MIDDLE DOT}'
True

在 3.3 版本发生变更: 已添加对名称别名 [1] 和命名序列 [2] 的支持。

unicodedata.name(chr, default=None, /)

Returns the name assigned to the character chr as a string. If no name is defined, default is returned, or, if not given, ValueError is raised. For example:

>>> unicodedata.name('½')
'VULGAR FRACTION ONE HALF'
>>> unicodedata.name('\uFFFF', 'fallback')
'fallback'
unicodedata.decimal(chr, default=None, /)

Returns the decimal value assigned to the character chr as integer. If no such value is defined, default is returned, or, if not given, ValueError is raised. For example:

>>> unicodedata.decimal('\N{ARABIC-INDIC DIGIT NINE}')
9
>>> unicodedata.decimal('\N{SUPERSCRIPT NINE}', -1)
-1
unicodedata.digit(chr, default=None, /)

Returns the digit value assigned to the character chr as integer. If no such value is defined, default is returned, or, if not given, ValueError is raised:

>>> unicodedata.digit('\N{SUPERSCRIPT NINE}')
9
unicodedata.numeric(chr, default=None, /)

Returns the numeric value assigned to the character chr as float. If no such value is defined, default is returned, or, if not given, ValueError is raised:

>>> unicodedata.numeric('½')
0.5
unicodedata.category(chr)

Returns the general category assigned to the character chr as string. General category names consist of two letters. See the General Category Values section of the Unicode Character Database documentation for a list of category codes. For example:

>>> unicodedata.category('A')  # 'L'etter, 'u'ppercase
'Lu'
unicodedata.bidirectional(chr)

Returns the bidirectional class assigned to the character chr as string. If no such value is defined, an empty string is returned. See the Bidirectional Class Values section of the Unicode Character Database documentation for a list of bidirectional codes. For example:

>>> unicodedata.bidirectional('\N{ARABIC-INDIC DIGIT SEVEN}') # 'A'rabic, 'N'umber
'AN'
unicodedata.combining(chr)

Returns the canonical combining class assigned to the character chr as integer. Returns 0 if no combining class is defined. See the Canonical Combining Class Values section of the Unicode Character Database for more information.

unicodedata.east_asian_width(chr)

Returns the east asian width assigned to the character chr as string. For a list of widths and or more information, see the Unicode Standard Annex #11.

unicodedata.mirrored(chr)

Returns the mirrored property assigned to the character chr as integer. Returns 1 if the character has been identified as a "mirrored" character in bidirectional text, 0 otherwise. For example:

>>> unicodedata.mirrored('>')
1
unicodedata.decomposition(chr)

Returns the character decomposition mapping assigned to the character chr as string. An empty string is returned in case no such mapping is defined. For example:

>>> unicodedata.decomposition('Ã')
'0041 0303'
unicodedata.normalize(form, unistr)

返回 Unicode 字符串 unistr 的正常形式 formform 的有效值为 'NFC' 、 'NFKC' 、 'NFD' 和 'NFKD' 。

Unicode 标准基于规范等价和兼容性等效的定义定义了 Unicode 字符串的各种规范化形式。在 Unicode 中,可以以各种方式表示多个字符。 例如,字符 U+00C7 (带有 CEDILLA 的 LATIN CAPITAL LETTER C )也可以表示为序列 U+0043( LATIN CAPITAL LETTER C )U+0327( COMBINING CEDILLA )。

对于每个字符,有两种正规形式:正规形式 C 和正规形式 D 。正规形式D(NFD)也称为规范分解,并将每个字符转换为其分解形式。 正规形式C(NFC)首先应用规范分解,然后再次组合预组合字符。

In addition to these two forms, there are two additional normal forms based on compatibility equivalence. In Unicode, certain characters are supported which normally would be unified with other characters. For example, U+2160 (ROMAN NUMERAL ONE) is really the same thing as U+0049 (LATIN CAPITAL LETTER I). However, it is supported in Unicode for compatibility with existing character sets (for example, gb2312).

The normal form KD (NFKD) will apply the compatibility decomposition, that is, replace all compatibility characters with their equivalents. The normal form KC (NFKC) first applies the compatibility decomposition, followed by the canonical composition.

即使两个 unicode 字符串被规范化并且人类读者看起来相同,如果一个具有组合字符而另一个没有,则它们可能无法相等。

unicodedata.is_normalized(form, unistr)

判断 Unicode 字符串 unistr 是否为正规形式 formform 的有效值为 'NFC', 'NFKC', 'NFD' 和 'NFKD'。

Added in version 3.8.

此外,该模块暴露了以下常量:

unicodedata.unidata_version

此模块中使用的 Unicode 数据库的版本。

unicodedata.ucd_3_2_0

这是一个与整个模块具有相同方法的对象,但对于需要此特定版本的 Unicode 数据库(如 IDNA )的应用程序,则使用 Unicode 数据库版本 3.2 。

备注