多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](stringprep.xhtml "stringprep --- Internet String Preparation") | - [上一页](textwrap.xhtml "textwrap --- Text wrapping and filling") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [文本处理服务](text.xhtml) » - $('.inline-search').show(0); | # [`unicodedata`](#module-unicodedata "unicodedata: Access the Unicode Database.") --- Unicode 数据库 - - - - - - 此模块提供对 Unicode 字符数据库(UCD)的访问,该数据库定义所有 Unicode 字符的字符属性。此数据库中包含的数据是从 [UCD版本11.0.0](http://www.unicode.org/Public/11.0.0/ucd) \[http://www.unicode.org/Public/11.0.0/ucd\] 编译的。 该模块使用与 Unicode 标准附件 #44 , [“Unicode字符数据库”](http://www.unicode.org/reports/tr44/tr44-6.html) \[http://www.unicode.org/reports/tr44/tr44-6.html\] 定义的相同名称和符号。它定义了以下功能: `unicodedata.``lookup`(*name*)按名称查找字符。如果找到具有给定名称的字符,则返回相应的字符。 如果没有找到,则 [`KeyError`](exceptions.xhtml#KeyError "KeyError") 被引发。 在 3.3 版更改: 已添加对名称别名 [1](#id3) 和命名序列 [2](#id4) 的支持。 `unicodedata.``name`(*chr*\[, *default*\])返回分配给字符 *chr* 的名称作为字符串。如果没有定义名称,则返回 *default* ,如果没有给出,则 [`ValueError`](exceptions.xhtml#ValueError "ValueError") 被引发。 `unicodedata.``decimal`(*chr*\[, *default*\])返回分配给字符 *chr* 的十进制值作为整数。 如果没有定义这样的值,则返回 *default* ,如果没有给出,则 [`ValueError`](exceptions.xhtml#ValueError "ValueError") 被引发。 `unicodedata.``digit`(*chr*\[, *default*\])返回分配给字符 *chr* 的数字值作为整数。 如果没有定义这样的值,则返回 *default* ,如果没有给出,则 [`ValueError`](exceptions.xhtml#ValueError "ValueError") 被引发。 `unicodedata.``numeric`(*chr*\[, *default*\])返回分配给字符 *chr* 的数值作为浮点数。 如果没有定义这样的值,则返回 *default* ,如果没有给出,则 [`ValueError`](exceptions.xhtml#ValueError "ValueError") 被引发。 `unicodedata.``category`(*chr*)返回分配给字符 *chr* 的常规类别为字符串。 `unicodedata.``bidirectional`(*chr*)返回分配给字符 *chr* 的双向类作为字符串。如果未定义此类值,则返回空字符串。 `unicodedata.``combining`(*chr*)返回分配给字符 *chr* 的规范组合类作为整数。如果没有定义组合类,则返回 `0` 。 `unicodedata.``east_asian_width`(*chr*)返回分配给字符 *chr* 的东亚宽度作为字符串。 `unicodedata.``mirrored`(*chr*)返回分配给字符 *chr* 的镜像属性为整数。如果字符在双向文本中被识别为“镜像”字符,则返回 `1` ,否则返回 `0` 。 `unicodedata.``decomposition`(*chr*)返回分配给字符 *chr* 的字符分解映射作为字符串。如果未定义此类映射,则返回空字符串。 `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)完全相同。 但是, Unicode 支持它与现有字符集(例如 gb2312 )的兼容性。 正规形式KD(NFKD)将应用兼容性分解,即用其等价项替换所有兼容性字符。 正规形式KC(NFKC)首先应用兼容性分解,然后是规范组合。 即使两个 unicode 字符串被规范化并且人类读者看起来相同,如果一个具有组合字符而另一个没有,则它们可能无法相等。 此外,该模块暴露了以下常量: `unicodedata.``unidata_version`此模块中使用的 Unicode 数据库的版本。 `unicodedata.``ucd_3_2_0`这是一个与整个模块具有相同方法的对象,但对于需要此特定版本的 Unicode 数据库(如 IDNA )的应用程序,则使用 Unicode 数据库版本 3.2 。 示例: ``` >>> import unicodedata >>> unicodedata.lookup('LEFT CURLY BRACKET') '{' >>> unicodedata.name('/') 'SOLIDUS' >>> unicodedata.decimal('9') 9 >>> unicodedata.decimal('a') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: not a decimal >>> unicodedata.category('A') # 'L'etter, 'u'ppercase 'Lu' >>> unicodedata.bidirectional('\u0660') # 'A'rabic, 'N'umber 'AN' ``` 脚注 [1](#id1)<http://www.unicode.org/Public/11.0.0/ucd/NameAliases.txt> [2](#id2)<http://www.unicode.org/Public/11.0.0/ucd/NamedSequences.txt> ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](stringprep.xhtml "stringprep --- Internet String Preparation") | - [上一页](textwrap.xhtml "textwrap --- Text wrapping and filling") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [文本处理服务](text.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。