unicodedata模块是用来做什么的,如何使用
Admin 2022-08-09 群英技术资讯 770 次浏览
在这篇文章中,我们来学习一下“unicodedata模块是用来做什么的,如何使用”的相关知识,下文有详细的讲解,易于大家学习和理解,有需要的朋友可以借鉴参考,下面就请大家跟着小编的思路一起来学习一下吧。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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
Pandas是一个python数据分析库,它提供了许多函数和方法来加快数据分析过程,下面这篇文章主要给大家介绍了关于python基础篇之pandas常用基本函数的相关资料,需要的朋友可以参考下
这篇文章主要分享的是 Python 编程语言的详细介绍,Python 由 Guido van Rossum 设计,作为“ABC”编程语言的继承者,于 1991 年首次发布。它是一种高级通用语言,其设计理念是通过使用缩进来强调代码的可读性。文章具有一定的参考价值,需要的朋友可以参考一下
这篇文章主要介绍了Python 入门学习之函数式编程, Python 中的函数式编程技术进行了简单的入门介绍,下文详细内容需要的小伙伴可以参考一下
最近在整理我磁盘上的照片,发现不少猫照,突然觉得若能把这些猫照都挑出来,观察它们的成长轨迹也是一件不错的事情。一张一张的找实在是太费劲了,能不能自动化地找出来呢?本文将详细为大家讲讲,需要的可以参考一下
内容介绍Canvas控件基本属性Canvas控件绘图常用方法Canvas控件具有两个功能,首先它可以用来绘制各种图形,比如弧形、线条、椭圆形、多边形和矩形等,其次Canvas控件还可以用来展示图
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008