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 16.0.0.

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

参考

Unicode とこのモジュールの使い方については Unicode HOWTO を参照してください。

unicodedata.lookup(name)

名前で文字を検索します。 指定された名前の文字が見つかれば、その文字を返します。 見つからなければ KeyError を送出します。 例えば次のようになります。

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

この関数が返す文字は、文字列リテラルの \N エスケープシーケンスが生成する文字と同じです。 例えば次のようになります。

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

バージョン 3.3 で変更: name aliases [1] と named sequences [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 を返し、default を指定していなければ ValueError を送出します。 例えば次のようになります。

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

文字 chr に割り当てられた数字の値を整数で返します。 値が定義されていなければ default を返し、default を指定していなければ ValueError を送出します。

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

文字 chr に割り当てられた数値を浮動小数点数で返します。 値が定義されていなければ default を返し、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.mirrored(chr)

文字 chr に割り当てられた鏡像プロパティを整数で返します。 双方向テキストで「鏡像」になる文字とされていれば 1、それ以外は 0 を返します。 例えば次のようになります。

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

文字 chr に割り当てられた文字分解マッピングを文字列で返します。 そのようなマッピングが定義されていなければ空文字列を返します。 例えば次のようになります。

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

Unicode 文字列 unistr の正規形 form を返します。 form の有効な値は、'NFC'、'NFKC'、'NFD'、'NFKD' です。

Unicode 規格は標準等価性 (canonical equivalence) と互換等価性 (compatibility equivalence) に基づいて、様々な Unicode文字列の正規形を定義します。Unicode では、複数の方法で表現できる文字があります。たとえば、文字 U+00C7 (LATIN CAPITAL LETTER C WITH CEDILLA) は、U+0043 (LATIN CAPITAL LETTER C) U+0327 (COMBINING CEDILLA) というシーケンスとしても表現できます。

各文字には2つの正規形があり、それぞれ正規形 C と正規形 D といいます。正規形 D (NFD) は標準分解 (canonical decomposition) としても知られており、各文字を分解された形に変換します。正規形 C (NFC) は標準分解を適用した後、結合済文字を再構成します。

これら 2 つの形式に加え、互換等価性に基づく正規形が 2 つあります。 Unicode は、本来なら他の文字に統一されるような文字もサポートしています。 例えば U+2160 (ROMAN NUMERAL ONE) は、実質的には U+0049 (LATIN CAPITAL LETTER I) と同じものです。 ただし、既存の文字集合 (例えば gb2312) との互換性のために、Unicode でもサポートされています。

正規形 KD (NFKD) は互換分解を適用し、すべての互換文字を等価な文字に置き換えます。 正規形 KC (NFKC) は、互換分解を適用してから標準合成を適用します。

2 つの Unicode 文字列が正規化されていて、人間の目には同じに見えても、一方に結合文字があり他方にはない場合は、比較して等しいと判定されないことがあります。

unicodedata.is_normalized(form, unistr)

Unicode 文字列 unistr が正規形 form かどうかを返します。 form の有効な値は、'NFC'、'NFKC'、'NFD'、'NFKD' です。

Added in version 3.8.

In addition, the module exposes the following constant:

unicodedata.unidata_version

このモジュールで使われている Unicode データベースのバージョン。

unicodedata.ucd_3_2_0

This is an object that has the same methods as 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).

脚注