python unicodedata模块用法
作者:周小董 发布时间:2021-04-05 20:53:55
UCD介绍
UCD是Unicode字符数据库(Unicode Character DataBase)的缩写。
UCD由一些描述Unicode字符属性和内部关系的纯文本或html文件组成。
UCD中的文本文件大都是适合于程序分析的Unicode相关数据。其中的html文件解释了数据库的组织,数据的格式和含义。
UCD中最庞大的文件无疑就是描述汉字属性的文件Unihan.txt。
在UCD 5.0,0中,Unihan.txt文件大小有28,221K字节。Unihan.txt中包含了很多有参考价值的索引,例如汉字部首、笔划、拼音、使用频度、四角号码排序等。这些索引都是基于一些比较权威的辞典,但大多数索引只能检索部分汉字。
unicodedata.lookup(name)
通过名称来查找一个字符。如果字符存在就返回相应字符,如果不存在抛出异常KeyError。
>>> import unicodedata
>>> print(unicodedata.lookup('LEFT CURLY BRACKET'))
{
>>> print(unicodedata.lookup('LEFT'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: "undefined character name 'LEFT'"
>>>
unicodedata.name(chr[,default])
通过字符来查找它的名称。如果成功返回相应名称,否则抛出异常ValueError。
>>> import unicodedata
>>> print(unicodedata.name('{'))
LEFT CURLY BRACKET
>>> print(unicodedata.name('@'))
COMMERCIAL AT
>>> print(unicodedata.name('{{'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: name() argument 1 must be a unicode character, not str
>>>
unicodedata.decimal(chr[, default])
返回表示数字字符的数值。如果给一个没有数字的值时,会抛出异常ValueError。
>>> import unicodedata
>>> print(unicodedata.decimal('7'))
7
>>> print(unicodedata.decimal('7a'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: decimal() argument 1 must be a unicode character, not str
>>>
unicodedata.digit(chr[, default])
把一个合法的数字字符串转换为数字值,比如0到9的字符串转换为相应的数字值。如果非法的字符串,抛出异常ValueError。
>>> import unicodedata
>>> print(unicodedata.digit('9', None))
9
>>> print(unicodedata.digit('9a', None))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: digit() argument 1 must be a unicode character, not str
>>>
unicodedata.numeric(chr[, default])
把一个表示数字的字符串转换为浮点数返回。比如可以把‘8’,‘四’转换数值输出。与digit()不一样的地方是它可以任意表示数值的字符都可以,不仅仅限于0到9的字符。如果不是合法字符,会抛出异常ValueError。
>>> import unicodedata
>>> print(unicodedata.numeric('四', None))
4.0
>>> print(unicodedata.numeric('8', None))
8.0
>>> print(unicodedata.numeric('8a', None))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: numeric() argument 1 must be a unicode character, not str
>>>
unicodedata.category(chr)
把一个字符返回它在UNICODE里分类的类型。具体类型如下:
Code Description
[Cc] Other, Control
[Cf] Other, Format
[Cn] Other, Not Assigned (no characters in the file have this property)
[Co] Other, Private Use
[Cs] Other, Surrogate
[LC] Letter, Cased
[Ll] Letter, Lowercase
[Lm] Letter, Modifier
[Lo] Letter, Other
[Lt] Letter, Titlecase
[Lu] Letter, Uppercase
[Mc] Mark, Spacing Combining
[Me] Mark, Enclosing
[Mn] Mark, Nonspacing
[Nd] Number, Decimal Digit
[Nl] Number, Letter
[No] Number, Other
[Pc] Punctuation, Connector
[Pd] Punctuation, Dash
[Pe] Punctuation, Close
[Pf] Punctuation, Final quote (may behave like Ps or Pe depending on usage)
[Pi] Punctuation, Initial quote (may behave like Ps or Pe depending on usage)
[Po] Punctuation, Other
[Ps] Punctuation, Open
[Sc] Symbol, Currency
[Sk] Symbol, Modifier
[Sm] Symbol, Math
[So] Symbol, Other
[Zl] Separator, Line
[Zp] Separator, Paragraph
[Zs] Separator, Space
>>> import unicodedata
>>> print(unicodedata.category('四'))
Lo
>>> print(unicodedata.category('8'))
Nd
>>> print(unicodedata.category('a'))
Ll
>>>
unicodedata.bidirectional(chr)
把一个字符给出它的分类,以便进行从左到右,还是从右到左的排列。如果没有定义,返回空字符串。
>>> import unicodedata
>>> print(unicodedata.bidirectional('9'))
EN
>>>
>>> print(unicodedata.bidirectional(u'\u0660'))
AN
>>>
>>> print(unicodedata.bidirectional('中'))
L
>>>
>>> print(unicodedata.bidirectional('a'))
L
>>>
>>> print(unicodedata.category(u'\u0660'))
Nd
>>>
其中EN表示English Number,AN表示Arabic Number,L表示Letter,Nd是表示Number Decimal。
unicodedata.combining(chr)
把字符的权威组合值返回,如果没有定义,默认是返回0。当正规化操作时,可以根据这个值进行排序,大的值排在小的值后面。
>>> import unicodedata
>>> print(unicodedata.combining('9'))
0
>>>
>>> print(unicodedata.combining('A'))
0
>>>
unicodedata.east_asian_width(chr)
把字符显示的宽度返回。具体内容如下:
‘F’(Fullwidth), ‘H’(Halfwidth), ‘W’(Wide), ‘Na’(Narrow), ‘A’(Ambiguous) or ‘N’(Natural).
>>> import unicodedata
>>> print(unicodedata.east_asian_width('9'))
Na
>>>
>>> print(unicodedata.east_asian_width('A'))
Na
>>>
>>> print(unicodedata.east_asian_width('蔡'))
W
>>>
unicodedata.mirrored(chr)
判断一个字符是否支持镜像属性,如果支持返回1,否则返回0.
>>> import unicodedata
>>> print(unicodedata.mirrored('9'))
0
>>>
>>> print(unicodedata.mirrored('A'))
0
>>>
>>> print(unicodedata.mirrored('蔡'))
0
>>>
unicodedata.decomposition(chr)
把一个可分解的字符分成两个16进制的值返回,如果不可分解,返回空。
>>> import unicodedata
>>> print(unicodedata.decomposition('9'))
>>>
>>> print(unicodedata.decomposition('-'))
>>>
>>> print(unicodedata.decomposition('蔡'))
>>>
>>> print(unicodedata.decomposition('ガ'))
30AB 3099
>>>
unicodedata.normalize(form, unistr)
把一串UNICODE字符串转换为普通格式的字符串,具体格式支持NFC、NFKC、NFD和NFKD格式。一些文本元素即可以使用静态的预先组合好的形式,也可使用动态组合的形式。Unicode字符的不同表示序列被认为是等价的。如果两个或多个序列被认为是等价的,Unicode标准不规定哪一种特定的序列是正确的,而认为每一个序列只不过与其它序列等价。
如 果需要一种单一的单一的表示方式,可以使用一种规范化的Unicode文本形式来减少不想要区别。Unicode标准定义了四种规范化形式: Normalization Form D (NFD),Normalization Form KD (NFKD),Normalization Form C (NFC),和Normalization Form KC (NFKC)。大约来说,NFD和NFKD将可能的字符进行分解,而NFC和NFKC将可能的字符进行组合。
>>> import unicodedata
>>> print(unicodedata.normalize('NFKD', u'aあä').encode('ascii', 'ignore'))
b'aa'
>>>
>>> title = u"Klüft skräms inför på fédéral électoral große"
>>> print title.encode(‘ascii','ignore')
Klft skrms infr p fdral lectoral groe
#可以看到丢了许多的字符
>>> import unicodedata
>>> unicodedata.normalize('NFKD', title).encode('ascii','ignore')
'Kluft skrams infor pa federal electoral groe'
unicodedata.unidata_version
返回当前Unicode使用的数据库的版本。
unicodedata.ucd_3_2_0
提供ucd3.2的对象方式访问,以便兼容旧的IDNA的应用程序。
>>> import unicodedata
>>> print(unicodedata.unidata_version)
9.0.0
>>>
>>> print(unicodedata.ucd_3_2_0)
<unicodedata.UCD object at 0x00000215E3EA3B70>
>>>
下面来仔细查看一个字符的UNICODE数据:
U+0062 is the Unicode hex value of the character Latin Small Letter B, which is categorized as “lowercase letter” in the Unicode 6.0 character table.
Unicode Character Information
Unicode Hex U+0062
Character Name LATIN SMALL LETTER B
General Category Lowercase Letter [Code: Ll]
Canonical Combining Class 0
Bidirectional Category L
Mirrored N
Uppercase Version U+0042
Titlecase Version U+0042
Unicode Character Encodings
Latin Small Letter B HTML Entity b (decimal entity), b (hex entity)
Windows Key Code Alt 0098 or Alt +00621
Programming Source Code Encodings Python hex: u”\u0062”, Hex for C++ and Java: “\u0062”
UTF-8 Hexadecimal Encoding 0x62
上面大多的函数都是针对这些数据信息进行查询,并且返回相应的值。
来源:https://cloud.tencent.com/developer/article/1406445


猜你喜欢
- 对于每个类型拥有的值范围以及并且指定日期何时间值的有效格式的描述见7.3.6 日期和时间类型。 这里是一个使用日期函数
- 列表生成式语法:[x*x for x in range(0,10)] //列表生成式,这里是中括号//结果 [0, 1, 4, 9, 16,
- 一、临时表实现分步处理1.概述当需要的结果需要经过多次处理后才能最终得到我们需要的结果时,就可以使用临时表,这里临时表就起到了一个中间处理的
- var _change = { &
- vue element-ui动态面包屑导航,供大家参考,具体内容如下直接上代码一、template代码// 这是单独的组件<templ
- 常用配置以下配置能使用File -> New Projects Settings -> Settings for New Pro
- 数据import numpy as npimport pandas as pddata = [{'Name': '小
- 实际上,在web开发中,cookie仅仅是一个文本文件,当用户访问站点时,它就被存储在用户使用的计算机上,其中,保存了一些信息,当用户日后再
- //截取字符串长度。支持utf-8和gb2312编码。若为gb2312,先将其转为utf-8,在utf-8的基础上截取然后再转换回来 fun
- 这篇文章主要介绍了python tkinter canvas使用实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
- 第一步:下载和安装python-3.4.4amd.msi可以去官方网站下载,也可以从网盘下载: 链接: https://pan.baidu.
- Python列表的append()方法踩坑在这之前,我一直认为append()只是个将一个对象添加到列表尾部的方法,但是今天之后,我对它有了
- Python-apply(lambda x: )使用def instant_order_deal(plat, special_product
- 本文实例为大家分享了js canvas随机粒子特效的具体代码,供大家参考,具体内容如下前言canvas实现前端的特效美术结果展示代码html
- 由于tensorflow版本不同,可能一些函数的调用也有变换,这时候可能需要查看tensorflow版本,可以在终端输入查询命令如下:imp
- 介绍今天有个不正经的需求,就是要快速做一个restful api的性能测试,要求测试在海量作业数据的情况下客户端分页获取所有作业的性能。因为
- 对于字典的操作,本篇介绍的是在其中添加值的方法,下面带来详细的介绍。1、通过键=值的方式进行添加。如果键存在,则会将旧的值进行覆盖,如果不存
- vue-router切换组件时使组件不销毁默认地,使用vue-router切换组件时,离开的组件会被销毁,新进入地组件会创建。那么,有时候,
- 最近重温了一下位图分割的相关内容,发现网络上位图分割原理讲得已经很清楚了,但是代码多为C++实现或者Matlab实现,因为需要Python的
- 我们网站的静态资源(css、js和背景图片)和web应用程序是分开部署的,几乎所有的静态资源都部署在同一个应用下。最开始的网站