unicodedata模块是用来做什么的,如何使用
Admin 2022-08-09 群英技术资讯 426 次浏览
UCD由一些描述Unicode字符属性和内部关系的纯文本或html文件组成。
UCD中的文本文件大都是适合于程序分析的Unicode相关数据。其中的html文件解释了数据库的组织,数据的格式和含义。
UCD中最庞大的文件无疑就是描述汉字属性的文件Unihan.txt。
在UCD 5.0,0中,Unihan.txt文件大小有28,221K字节。Unihan.txt中包含了很多有参考价值的索引,例如汉字部首、笔划、拼音、使用频度、四角号码排序等。这些索引都是基于一些比较权威的辞典,但大多数索引只能检索部分汉字。
通过名称来查找一个字符。如果字符存在就返回相应字符,如果不存在抛出异常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'" >>>
通过字符来查找它的名称。如果成功返回相应名称,否则抛出异常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 >>>
返回表示数字字符的数值。如果给一个没有数字的值时,会抛出异常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 >>>
把一个合法的数字字符串转换为数字值,比如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 >>>
把一个表示数字的字符串转换为浮点数返回。比如可以把‘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 >>>
把一个字符返回它在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 >>>
把一个字符给出它的分类,以便进行从左到右,还是从右到左的排列。如果没有定义,返回空字符串。
>>> 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。
把字符的权威组合值返回,如果没有定义,默认是返回0。当正规化操作时,可以根据这个值进行排序,大的值排在小的值后面。
>>> import unicodedata >>> print(unicodedata.combining('9')) 0 >>> >>> print(unicodedata.combining('A')) 0 >>>
把字符显示的宽度返回。具体内容如下:
‘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 >>>
判断一个字符是否支持镜像属性,如果支持返回1,否则返回0.
>>> import unicodedata >>> print(unicodedata.mirrored('9')) 0 >>> >>> print(unicodedata.mirrored('A')) 0 >>> >>> print(unicodedata.mirrored('蔡')) 0 >>>
把一个可分解的字符分成两个16进制的值返回,如果不可分解,返回空。
>>> import unicodedata >>> print(unicodedata.decomposition('9')) >>> >>> print(unicodedata.decomposition('-')) >>> >>> print(unicodedata.decomposition('蔡')) >>> >>> print(unicodedata.decomposition('ガ')) 30AB 3099 >>>
把一串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'
返回当前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
上面大多的函数都是针对这些数据信息进行查询,并且返回相应的值。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
matplotlib内置了众多预定义的颜色映射表,使用这些颜色映射表可以为用户提供更多的颜色建议,为用户节省大量的开发时间。pyplot模块中提供了colormaps()函数用于查看所有可用的颜色映射表,示例代码及运行结果如下。
集合(set)是一个无序的不重复元素序列。因此在每次运行的时候集合的运行结果的内容都是相同的,但元素的排列顺序却不是固定的,所以本章中部分案例的运行结果会出现与给出结果不同的情况(运行结果不唯一)可以使用大括号{}或者set()函数创建集合,注意:创建一个空集合必须用set()而不是{},因为{}是用来创建一个空字典
在工作中难免会遇到需要批量整理文件的情况,当需要从一堆文件中将部分文件批量地转移时,如果手工一一转移难免浪费时间,这篇文章主要给大家介绍了关于如何在python中用os模块实现批量移动文件的相关资料,需要的朋友可以参考下
这篇文章主要为大家详细介绍了python单向循环链表实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
pytest有丰富的命令行选项,以满足不同的需要,下面这篇文章主要给大家介绍了关于Python教程之pytest命令行方式运行的相关资料,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008