# `unicodedata` --- Unicode 数据库

This module provides access to the Unicode Character Database (UCD) which defines character properties for all Unicode characters. The data contained in this database is compiled from the UCD version 17.0.0.

The module uses the same names and symbols as defined by Unicode Standard Annex #44, "Unicode Character Database".

参见

有关 Unicode 以及如何使用此模块的更多信息，请参阅 Unicode 指南。

Lookup
`lookup(name)` | Look up character by name
`name(chr)` | Return the name assigned to a character
Numeric values
`decimal(chr)` | Decimal value of a character
`digit(chr)` | Digit value of a character
`numeric(chr)` | Numeric value of a character
Properties
`bidirectional(chr)` | Bidirectional class of a character
`block(chr)` | Unicode block of a character
`category(chr)` | General category of a character
`combining(chr)` | Canonical combining class of a character
`decomposition(chr)` | Character decomposition mapping
`east_asian_width(chr)` | East Asian width of a character
`extended_pictographic(chr)` | Check if a character has the Extended_Pictographic property
`grapheme_cluster_break(chr)` | Grapheme_Cluster_Break property of a character
`indic_conjunct_break(chr)` | Indic_Conjunct_Break property of a character
`isxidcontinue(chr)` | Check if a character is a valid identifier continuation
`isxidstart(chr)` | Check if a character is a valid identifier start
`mirrored(chr)` | Mirrored property of a character
Normalization
`normalize(form, unistr)` | Return the normalized form of a string
`is_normalized(form, unistr)` | Check if a Unicode string is normalized
Text segmentation
`iter_graphemes(unistr)` | Iterate over grapheme clusters in a string

unicodedata.lookup(name, /)

  按名称查找字符。如果找到具有给定名称的字符，则返回相应的字符。 如果没有找到，则引发 `KeyError`。例如:

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

  此函数返回的字符与字符串字面值中 `\N` 转义序列生成的字符相同。例如:

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

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

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

  返回分配给字符 chr 的名称（字符串形式）。如果未定义名称，则返回 default；如果未指定 default，则会引发 `ValueError`。例如:

      >>> unicodedata.name('½')
      'VULGAR FRACTION ONE HALF'
      >>> unicodedata.name('\uFFFF', 'fallback')
      'fallback'

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

  返回分配给字符 chr 的十进制值作为整数。 如果没有定义这样的值，则返回 default ，如果没有给出，则 `ValueError` 被引发。例如:

      >>> unicodedata.decimal('\N{ARABIC-INDIC DIGIT NINE}')
      9
      >>> unicodedata.decimal('\N{SUPERSCRIPT NINE}', -1)
      -1

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

  返回分配给字符 chr 的数字值作为整数。 如果没有定义这样的值，则返回 default ，如果没有给出，则引发 `ValueError`:

      >>> unicodedata.digit('\N{SUPERSCRIPT NINE}')
      9

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

  返回分配给字符 chr 的数值作为浮点数。 如果没有定义这样的值，则返回 default ，如果没有给出，则引发 `ValueError`

      >>> 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.block(chr, /)

  Returns the block assigned to the character chr. For example:

      >>> unicodedata.block('S')
      'Basic Latin'

  Added in version 3.15.

unicodedata.mirrored(chr, /)

  返回分配给字符 chr 的镜像属性为整数。如果字符在双向文本中被识别为“镜像”字符，则返回 `1` ，否则返回 `0` 。例如:

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

unicodedata.isxidstart(chr, /)

  Return `True` if chr is a valid identifier start per the Unicode Standard Annex #31, that is, it has the `XID_Start` property. Return `False` otherwise. For example:

      >>> unicodedata.isxidstart('S')
      True
      >>> unicodedata.isxidstart('0')
      False

  Added in version 3.15.

unicodedata.isxidcontinue(chr, /)

  Return `True` if chr is a valid identifier character per the Unicode Standard Annex #31, that is, it has the `XID_Continue` property. Return `False` otherwise. For example:

      >>> unicodedata.isxidcontinue('S')
      True
      >>> unicodedata.isxidcontinue(' ')
      False

  Added in version 3.15.

unicodedata.decomposition(chr, /)

  返回分配给字符 chr 的字符分解映射作为字符串。如果未定义此类映射，则返回空字符串。例如:

      >>> unicodedata.decomposition('Ã')
      '0041 0303'

unicodedata.grapheme_cluster_break(chr, /)

  Returns the Grapheme_Cluster_Break property assigned to the character.

  Added in version 3.15.

unicodedata.indic_conjunct_break(chr, /)

  Returns the Indic_Conjunct_Break property assigned to the character.

  Added in version 3.15.

unicodedata.extended_pictographic(chr, /)

  Returns `True` if the character has the Extended_Pictographic property, `False` otherwise.

  Added in version 3.15.

unicodedata.normalize(form, unistr, /)

  返回 Unicode 字符串 unistr 的正规形式 form 。 form 的有效值为 '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）首先应用规范分解，然后再次组合预组合字符。

  除了这两种形式之外，还有两种基于兼容性等效的其他正规形式。 在 Unicode 中，支持某些通常会与其他字符统一的字符。 例如， U+2160（ROMAN NUMERAL ONE）与 U+0049（LATIN CAPITAL LETTER I）实际上是相同的。 但是，为了与现有字符集（例如 gb2312 ）兼容，Unicode 仍然支持它。

  正规形式KD（NFKD）将应用兼容性分解，也就是用其等价项替换所有兼容性字符。 正规形式KC（NFKC）首先应用兼容性分解，然后是规范组合。

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

unicodedata.is_normalized(form, unistr, /)

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

  Added in version 3.8.

unicodedata.iter_graphemes(unistr, start=0, end=sys.maxsize, /)

  Returns an iterator to iterate over grapheme clusters. With optional start, iteration begins at that position. With optional end, iteration stops at that position.

  Converting an emitted item to string returns a substring corresponding to the grapheme cluster. Its `start` and `end` attributes denote the start and end of the grapheme cluster.

  It uses extended grapheme cluster rules defined by Unicode Standard Annex #29, "Unicode Text Segmentation".

  Added in version 3.15.

In addition, the module exposes the following constants:

unicodedata.unidata_version

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

unicodedata.ucd_3_2_0

  This is an object that has most of the methods of the entire module, but uses the Unicode database version 3.2 instead, for applications that require this specific version of the Unicode database (such as IDNA).

备注

[1]

https://www.unicode.org/Public/17.0.0/ucd/NameAliases.txt

[2]

https://www.unicode.org/Public/17.0.0/ucd/NamedSequences.txt