1 :mod:`unicodedata` --- Unicode Database 2 ======================================= 3 4 .. module:: unicodedata 5 :synopsis: Access the Unicode Database. 6 7 .. moduleauthor:: Marc-Andr Lemburg <mal (a] lemburg.com> 8 .. sectionauthor:: Marc-Andr Lemburg <mal (a] lemburg.com> 9 .. sectionauthor:: Martin v. Lwis <martin (a] v.loewis.de> 10 11 .. index:: 12 single: Unicode 13 single: character 14 pair: Unicode; database 15 16 -------------- 17 18 This module provides access to the Unicode Character Database (UCD) which 19 defines character properties for all Unicode characters. The data contained in 20 this database is compiled from the `UCD version 11.0.0 21 <http://www.unicode.org/Public/11.0.0/ucd>`_. 22 23 The module uses the same names and symbols as defined by Unicode 24 Standard Annex #44, `"Unicode Character Database" 25 <http://www.unicode.org/reports/tr44/tr44-6.html>`_. It defines the 26 following 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 137 In addition, the module exposes the following constant: 138 139 .. data:: unidata_version 140 141 The version of the Unicode database used in this module. 142 143 144 .. data:: ucd_3_2_0 145 146 This is an object that has the same methods as the entire module, but uses the 147 Unicode database version 3.2 instead, for applications that require this 148 specific version of the Unicode database (such as IDNA). 149 150 Examples: 151 152 >>> import unicodedata 153 >>> unicodedata.lookup('LEFT CURLY BRACKET') 154 '{' 155 >>> unicodedata.name('/') 156 'SOLIDUS' 157 >>> unicodedata.decimal('9') 158 9 159 >>> unicodedata.decimal('a') 160 Traceback (most recent call last): 161 File "<stdin>", line 1, in <module> 162 ValueError: not a decimal 163 >>> unicodedata.category('A') # 'L'etter, 'u'ppercase 164 'Lu' 165 >>> unicodedata.bidirectional('\u0660') # 'A'rabic, 'N'umber 166 'AN' 167 168 169 .. rubric:: Footnotes 170 171 .. [#] http://www.unicode.org/Public/11.0.0/ucd/NameAliases.txt 172 173 .. [#] http://www.unicode.org/Public/11.0.0/ucd/NamedSequences.txt 174