1:mod:`unicodedata` --- Unicode Database 2======================================= 3 4.. module:: unicodedata 5 :synopsis: Access the Unicode Database. 6 7.. moduleauthor:: Marc-André Lemburg <mal@lemburg.com> 8.. sectionauthor:: Marc-André Lemburg <mal@lemburg.com> 9.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de> 10 11.. index:: 12 single: Unicode 13 single: character 14 pair: Unicode; database 15 16-------------- 17 18This module provides access to the Unicode Character Database (UCD) which 19defines character properties for all Unicode characters. The data contained in 20this database is compiled from the `UCD version 13.0.0 21<https://www.unicode.org/Public/13.0.0/ucd>`_. 22 23The module uses the same names and symbols as defined by Unicode 24Standard Annex #44, `"Unicode Character Database" 25<https://www.unicode.org/reports/tr44/>`_. It defines the 26following functions: 27 28 29.. function:: lookup(name) 30 31 Look up character by name. If a character with the given name is found, return 32 the corresponding character. If not found, :exc:`KeyError` is raised. 33 34 .. versionchanged:: 3.3 35 Support for name aliases [#]_ and named sequences [#]_ has been added. 36 37 38.. function:: name(chr[, default]) 39 40 Returns the name assigned to the character *chr* as a string. If no 41 name is defined, *default* is returned, or, if not given, :exc:`ValueError` is 42 raised. 43 44 45.. function:: decimal(chr[, default]) 46 47 Returns the decimal value assigned to the character *chr* as integer. 48 If no such value is defined, *default* is returned, or, if not given, 49 :exc:`ValueError` is raised. 50 51 52.. function:: digit(chr[, default]) 53 54 Returns the digit value assigned to the character *chr* as integer. 55 If no such value is defined, *default* is returned, or, if not given, 56 :exc:`ValueError` is raised. 57 58 59.. function:: numeric(chr[, default]) 60 61 Returns the numeric value assigned to the character *chr* as float. 62 If no such value is defined, *default* is returned, or, if not given, 63 :exc:`ValueError` is raised. 64 65 66.. function:: category(chr) 67 68 Returns the general category assigned to the character *chr* as 69 string. 70 71 72.. function:: bidirectional(chr) 73 74 Returns the bidirectional class assigned to the character *chr* as 75 string. If no such value is defined, an empty string is returned. 76 77 78.. function:: combining(chr) 79 80 Returns the canonical combining class assigned to the character *chr* 81 as integer. Returns ``0`` if no combining class is defined. 82 83 84.. function:: east_asian_width(chr) 85 86 Returns the east asian width assigned to the character *chr* as 87 string. 88 89 90.. function:: mirrored(chr) 91 92 Returns the mirrored property assigned to the character *chr* as 93 integer. Returns ``1`` if the character has been identified as a "mirrored" 94 character in bidirectional text, ``0`` otherwise. 95 96 97.. function:: decomposition(chr) 98 99 Returns the character decomposition mapping assigned to the character 100 *chr* as string. An empty string is returned in case no such mapping is 101 defined. 102 103 104.. function:: normalize(form, unistr) 105 106 Return the normal form *form* for the Unicode string *unistr*. Valid values for 107 *form* are 'NFC', 'NFKC', 'NFD', and 'NFKD'. 108 109 The Unicode standard defines various normalization forms of a Unicode string, 110 based on the definition of canonical equivalence and compatibility equivalence. 111 In Unicode, several characters can be expressed in various way. For example, the 112 character U+00C7 (LATIN CAPITAL LETTER C WITH CEDILLA) can also be expressed as 113 the sequence U+0043 (LATIN CAPITAL LETTER C) U+0327 (COMBINING CEDILLA). 114 115 For each character, there are two normal forms: normal form C and normal form D. 116 Normal form D (NFD) is also known as canonical decomposition, and translates 117 each character into its decomposed form. Normal form C (NFC) first applies a 118 canonical decomposition, then composes pre-combined characters again. 119 120 In addition to these two forms, there are two additional normal forms based on 121 compatibility equivalence. In Unicode, certain characters are supported which 122 normally would be unified with other characters. For example, U+2160 (ROMAN 123 NUMERAL ONE) is really the same thing as U+0049 (LATIN CAPITAL LETTER I). 124 However, it is supported in Unicode for compatibility with existing character 125 sets (e.g. gb2312). 126 127 The normal form KD (NFKD) will apply the compatibility decomposition, i.e. 128 replace all compatibility characters with their equivalents. The normal form KC 129 (NFKC) first applies the compatibility decomposition, followed by the canonical 130 composition. 131 132 Even if two unicode strings are normalized and look the same to 133 a human reader, if one has combining characters and the other 134 doesn't, they may not compare equal. 135 136.. function:: is_normalized(form, unistr) 137 138 Return whether the Unicode string *unistr* is in the normal form *form*. Valid 139 values for *form* are 'NFC', 'NFKC', 'NFD', and 'NFKD'. 140 141 .. versionadded:: 3.8 142 143 144In addition, the module exposes the following constant: 145 146.. data:: unidata_version 147 148 The version of the Unicode database used in this module. 149 150 151.. data:: ucd_3_2_0 152 153 This is an object that has the same methods as the entire module, but uses the 154 Unicode database version 3.2 instead, for applications that require this 155 specific version of the Unicode database (such as IDNA). 156 157Examples: 158 159 >>> import unicodedata 160 >>> unicodedata.lookup('LEFT CURLY BRACKET') 161 '{' 162 >>> unicodedata.name('/') 163 'SOLIDUS' 164 >>> unicodedata.decimal('9') 165 9 166 >>> unicodedata.decimal('a') 167 Traceback (most recent call last): 168 File "<stdin>", line 1, in <module> 169 ValueError: not a decimal 170 >>> unicodedata.category('A') # 'L'etter, 'u'ppercase 171 'Lu' 172 >>> unicodedata.bidirectional('\u0660') # 'A'rabic, 'N'umber 173 'AN' 174 175 176.. rubric:: Footnotes 177 178.. [#] https://www.unicode.org/Public/13.0.0/ucd/NameAliases.txt 179 180.. [#] https://www.unicode.org/Public/13.0.0/ucd/NamedSequences.txt 181