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

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://blog.csdn.net/xc_zhou/article/details/82079753

python unicodedata用法相关推荐

  1. [383]python unicodedata用法

    UCD是Unicode字符数据库(Unicode Character DataBase)的缩写. UCD由一些描述Unicode字符属性和内部关系的纯文本或html文件组成. UCD中的文本文件大都是 ...

  2. Python pandas用法

    Python pandas用法 无味之味关注 12019.01.10 15:43:25字数 2,877阅读 91,914 介绍 在Python中,pandas是基于NumPy数组构建的,使数据预处理. ...

  3. python goto 用法

    python goto 用法 pip install goto-statement from goto import with_goto @with_goto def range(start, sto ...

  4. Python SQLite 用法

    Python SQLite 用法 具体可以参考网址 代码: #导入 import sqlite3 #连接库,如果数据库不存在,那么它就会被创建,最后将返回一个数据库对象. # test.db:数据路路 ...

  5. import的用法python_Python导入模块,Python import用法(超级详细)

    Python导入模块,Python import用法(超级详细) 使用 Python 进行编程时,有些功能没必须自己实现,可以借助 Python 现有的标准库或者其他人提供的第三方库.比如说,在前面章 ...

  6. python with用法

    @python with用法 python中with可以明显改进代码友好度,比如: [python] view plaincopyprint? with open('a.txt') as f: pri ...

  7. Python高级用法总结

    Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensi ...

  8. 数据库和python的结合_redis数据库及与python交互用法简单示例

    本文实例讲述了redis数据库及与python交互用法.分享给大家供大家参考,具体如下: redis数据操作 1.string类型:主要存储字符串 操作 命令 设置键值 set key value 设 ...

  9. python输出函数是什么_Python中打印函数使用概述,python,print,用法,总结

    输出的 print 函数总结: 1. 字符串和数值类型 可以直接输出 print(1) 1 print("Hello World") Hello World 2.变量 无论什么类型 ...

最新文章

  1. 数论基础--洛谷P1072 Hankson 的趣味题
  2. 马自达新车全面取消触摸屏,意外引得叫好声一片
  3. (转载)彻底理解浏览器的缓存机制
  4. 【Leetcode】62. 不同路径
  5. C++Wiggle Sort摆动排序的实现算法(附完整源码)
  6. 疫情之下的科技普惠:阿里云科技驱动中小企业数字化
  7. Nacos配置中心介绍
  8. Xcode5 使用gitHub上库的SSH地址建立Repository,以及如何通过Xcode把代码传到GitHub
  9. CentOS下安装SecureCRT的sz/rz工具包
  10. 升序堆和降序堆(优先队列) 洛谷1801
  11. 使用ViewModel模式来简化WPF的TreeView
  12. van-cell 取消点击_支付宝平安好医保怎么样?怎么报销?靠谱吗?怎么取消?_保险测评...
  13. SpringMvc @PathVariable 工作原理
  14. PLC通讯实现-C#实现欧姆龙以太网通讯FINS UDP(三)
  15. 阿里云常见热门问题解答汇总
  16. python重复import_Python 中循环 import 造成的问题如何解决?
  17. 解决windows电脑蓝屏的方法
  18. C语言|博客作业05
  19. 【GPGPU编程模型与架构原理】第一章 1.1 GPGPU 与并行计算机
  20. 电平是啥,大白话理解行吗?

热门文章

  1. c语言整数除法转换成浮点型,C语言中的类型转换
  2. Pyqt5的tableWidget的单元格控件居中
  3. PyQt5笔记之表格(Table Widget)
  4. 如何使用 FFmpeg 进行图片压缩与制作视频酷游连结
  5. shardingjdbc多数据源配置
  6. 《第一行代码》封面诞生记
  7. Python-Flask开发微电影网站(五)
  8. 最全英语单词下载地址
  9. telnet 正在连接127.0.0.1:8888...无法打开到主机的连接。 在端口 23: 连接失败
  10. VMware ubuntu虚拟机的安装过程记录