文章目录

  • lookup
  • name
  • decimal
  • digit
  • numeric
  • category
  • combining
  • east_asian_width
  • mirrored
  • decomposition
  • normalize
  • 参考

UCD是Unicode字符数据库(Unicode Character DataBase)的缩写。

UCD由一些描述Unicode字符属性和内部关系的纯文本或html文件组成。

UCD中的文本文件大都是适合于程序分析的Unicode相关数据。其中的html文件解释了数据库的组织,数据的格式和含义。

UCD中最庞大的文件无疑就是描述汉字属性的文件Unihan.txt。

在UCD 5.0,0中,Unihan.txt文件大小有28,221K字节。Unihan.txt中包含了很多有参考价值的索引,例如汉字部首、笔划、拼音、使用频度、四角号码排序等。这些索引都是基于一些比较权威的辞典,但大多数索引只能检索部分汉字。

lookup

语法:unicodedata.lookup(name)

描述:通过一个名称(name)来查找一个字符。如果找到name对应的字符,则返回相应字符;若未找到,则抛出异常KeyError。

import unicodedata
print(unicodedata.lookup('LEFT CURLY BRACKET')) # 左卷曲括号, '{'
print(unicodedata.lookup('LOWER'))
{---------------------------------------------------------------------------KeyError                                  Traceback (most recent call last)<ipython-input-2-f8aa898043e8> in <module>1 import unicodedata2 print(unicodedata.lookup('LEFT CURLY BRACKET')) # 左卷曲括号, '{'
----> 3 print(unicodedata.lookup('LOWER'))KeyError: "undefined character name 'LOWER'"

name

语法:unicodedata.name(chr[, default])

描述:通过字符(chr,只能是单个字符)来查找它的名称。若成功则返回相应名称。给定默认值default后,若未找到对应名称时则返回default值;若未给定默认值,且未找到对应名称则抛出异常ValueError。

print(unicodedata.name('^'))
print(unicodedata.name('{'))
print(unicodedata.name('@'))
print(unicodedata.name('\t', 0)) # 未找到对应名称,返回'0'
print(unicodedata.name('\t')) # 未找到对应名称,且未指定默认值,所以抛出异常ValueError
CIRCUMFLEX ACCENT
LEFT CURLY BRACKET
COMMERCIAL AT
0---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-9-9ab6ba04cd7b> in <module>3 print(unicodedata.name('@'))4 print(unicodedata.name('\t', 0)) # 未找到对应名称,返回'0'
----> 5 print(unicodedata.name('\t')) # 未找到对应名称,且未指定默认值,所以抛出异常ValueErrorValueError: no such name

decimal

语法:unicodedata.decimal(chr[,default])

描述:将unicode字符(chr)转换为其等效的十进制值。以整数形式返回【该字符对应的十进制值】。若未定义这样的值,则返回默认值default;若未指定默认值,则抛出ValueError异常。

print(unicodedata.decimal('7'))
7
print(unicodedata.decimal('7a'))
---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-11-0cd5612c103f> in <module>
----> 1 print(unicodedata.decimal('7a'))TypeError: decimal() argument 1 must be a unicode character, not str
print(unicodedata.decimal('a', 0)) # 未找到,返回默认值0
0

digit

语法:unicodedata.digit(chr[, default])

描述:将unicode字符(chr)转换为其等效的数字值。以整数形式返回【该字符对应的数字值】。若未定义这样的值,则返回默认值default;若未指定默认值,则抛出ValueError异常。

print(unicodedata.digit('9', None))
9
print(unicodedata.digit('9a', None))
---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-16-ecf1b3b1dce3> in <module>
----> 1 print(unicodedata.digit('9a', None))TypeError: digit() argument 1 must be a unicode character, not str
print(unicodedata.digit('\t', 0)) # 未找到,返回默认值0
print(unicodedata.digit('\t'))  # 未找到,且未指定默认值,所以抛出ValueError异常
0---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-18-21b249bc5ecd> in <module>1 print(unicodedata.digit('\t', 0)) # 未找到,返回默认值0
----> 2 print(unicodedata.digit('\t'))  # 未找到,且未指定默认值,所以抛出ValueError异常ValueError: not a digit

numeric

语法:unicodedata.numeric(chr[, default])

描述:将Unicode字符(chr)转换为等效的数值。以浮点形式返回【与chr相对应的数值】。若未定义这样的值,则返回默认值default;若未指定默认值,则抛出ValueError异常。(把一个表示数字的字符转换为浮点数。比如,可将’5’、'五’转换数值输出。与digit()不同是numeric()可以转换任意表示数值的字符,不仅仅限于0到9的字符)

print(unicodedata.numeric('四'))
print(unicodedata.numeric('8', None))
print(unicodedata.numeric('a', 0)) # 'a'不能转换为数字,所以返回默认值0
4.0
8.0
0
print(unicodedata.numeric('8a', None))
---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-20-b9475494db3c> in <module>
----> 1 print(unicodedata.numeric('8a', None))TypeError: numeric() argument 1 must be a unicode character, not str

category

语法:unicodedata.category(chr)

描述:返回字符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

print(unicodedata.category('四')) # Letter, Other
print(unicodedata.category('8')) # Number, Decimal Digit
print(unicodedata.category('a'))
Lo
Nd
Ll

combining

语法:unicodedata.combining(chr)

描述:把字符的权威组合值返回,如果没有定义,默认是返回0。当正规化操作时,可以根据这个值进行排序,大的值排在小的值后面。

print(unicodedata.combining('\t'))
print(unicodedata.combining('A'))
0
0

east_asian_width

语法:unicodedata.east_asian_width(chr)

描述:返回字符显示的宽度。具体内容如下:

F:fullwidth,H:halfwidth,W:wide,Na:narrow,A:ambiguous(不明确),N:natural(正常)

print(unicodedata.east_asian_width('9'))
print(unicodedata.east_asian_width('A'))
print(unicodedata.east_asian_width('蔡'))
Na
Na
W

mirrored

语法:unicodedata.mirrored()

描述:判断字符chr是否支持镜像属性,支持则返回1,不支持则返回0。

print(unicodedata.mirrored('0'))
print(unicodedata.mirrored('蔡'))
0
0

decomposition

语法:unicodedata.decomposition(chr)

描述:将一个可分解的字符chr分成两个16进制的值并返回,如果不可分解,返回空。

print(unicodedata.decomposition('é')) # 可分解
print(unicodedata.decomposition('ガ'))
print(unicodedata.decomposition('e')) # 不可分解,所以返回空值(输出就是一片空白)
0065 0301
30AB 3099

normalize

语法:unicodedata.normalize(form, unistr)

把一串UNICODE字符串转换为普通格式的字符串,form格式支持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将可能的字符进行组合。

s1 = 'café' # 'é'为组合字符,由'e'与重音符组合而成
s2 = 'cafe\u0301' # 将'é'拆为'e'和'\u0301','\u0301'为重音符的unicode编码print(unicodedata.normalize('NFC', s1)) # NFC使用最少的码位,所以'é'被合并为一个字符(事实上s1中本来就是é),因而返回结果为'café'(输出不带引号)
print(unicodedata.normalize('NFC', s2)) # 'e'和'\u0301'被合并为一个字符é,因而返回结果为'café'(输出不带引号)
print(unicodedata.normalize('NFD', s1)) # NFD使组合字符拆开为两个字符,这里'é'被拆为'e'和重音符,即输出结果为:'cafeˋ'
print(unicodedata.normalize('NFD', s2)) # s2最后两个字符为'e'和'\u0301',
café
café
café
café
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')) #可以看到丢了许多的字符
b'Klft skrms infr p fdral lectoral groe'
unicodedata.normalize('NFKD', title).encode('ascii','ignore') # 进行分解
b'Kluft skrams infor pa federal electoral groe'

参考

unicodedata — Unicode Database

https://blog.csdn.net/qq_40061206/article/details/105481909

unicodedata模块相关推荐

  1. Python3.x 标准模块库目录(下篇)

    Python Standard Library 翻译: Python 江湖群 10/06/07 20:10:08 编译 0.1. 关于本书 0.2. 代码约定 0.3. 关于例子 0.4. 如何联系 ...

  2. [转]python各模块的学习

    [-] 01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 ...

  3. Python 模块大全(很详细!)

    转载:.... Python的模块大全,很全,有详细介绍! 另外附Python两个教程 1. Python详细教程(廖雪峰的官方网站,语言简洁!) 2. Python 进阶教程 (Vamei) 3. ...

  4. python 各种模块学习

    from:https://blog.csdn.net/weiwangchao_/article/details/70570508 转载:.... Python的模块大全,很全,有详细介绍! 另外附Py ...

  5. Python2.0 模块大全

    博客 学院 下载 更多 写博客 qq779488143 Python 模块大全(很详细!) 转载 2014年12月12日 20:47:18 标签: Python / 模块 / 教程 3694 转载:. ...

  6. python 模块大全

    Python 模块大全(很详细!) 转载 2014年12月12日 20:47:18 标签: Python / 模块 / 教程 / 4479 编辑 删除 Python 模块大全很详细 01 关于本书 0 ...

  7. Python 模块介绍

    核心模块 1.1. 介绍 1.2. _ _builtin_ _ 模块 1.3. exceptions 模块 1.4. os 模块 1.5. os.path 模块 1.6. stat 模块 1.7. s ...

  8. Python模块(自己整理并不完整)

    PY核心模块方法 ******************** os模块: os.remove() 删除文件  os.unlink() 删除文件  os.rename() 重命名文件  os.listdi ...

  9. micropython常用模块有那个_Python常用模块,不明觉厉先马后看

    这是我在网上找的常用内置模块总结,不是大全.我想,大全对于现在的我也没有什么用处,徒增压力和烦恼. 虽然有很多不明觉厉的东西,不过还是先存起来,以备将来不时之需. 常用的libraries(modul ...

  10. python标准库模块

    06/07 20:10:08 编译 0.1. 关于本书 0.2. 代码约定 0.3. 关于例子 0.4. 如何联系我们 核心模块 1.1. 介绍 1.2. _ _builtin_ _ 模块 1.3. ...

最新文章

  1. Metasploit search命令使用技巧
  2. ECMall如何在后台添加模板编辑页
  3. java文件读写操作指定编码格式[转]
  4. Ant步步为营(4)ant启动tomcat
  5. python文本格式_python处理文本文件并生成指定格式的文件
  6. 目前人工智能的主要研究方向是哪些?
  7. Instagram使用教程
  8. matlab在非线性动力学,基于Matlab的非线性动力学系统分析
  9. Android修改PackageInstaller自动安装指定应用,android开发网易新闻
  10. CUDAnumba | 使用python分别在cpu和gpu全局内存和gpu共享内存进行矩阵乘法运算
  11. SQL每日一题(20210901)如果员工的【入职日期】是当月15号后,则该员工的【社保缴纳月份】为【入职日期】的次月
  12. C语言中快排函数——qsort()
  13. dirname 使用总结
  14. 听声音做钥匙?!慢放开锁音轨,黑客就能破解常用门锁
  15. fastapi的两种运行方式
  16. 苍蓝誓约服务器维护什么时候结束,苍蓝誓约手游2021年7月15日停服维护公告_苍蓝誓约手游2021年7月15日更新了什么_玩游戏网...
  17. npp夜光数据介绍 viirs_npp夜光数据介绍 viirs_DMSP/OLS与NPP/VIIRS两类夜间灯光数据整合...
  18. 正则表达式----座机+手机号
  19. 解决ultraedit-32卸载后文件关联问题
  20. 模糊视频如何修复高清?这个方法教给你

热门文章

  1. 【转】乐观锁和悲观锁的区别
  2. load,initialize方法
  3. Mac上的包管理器Homebrew的介绍及安装和使用实践
  4. Ubuntu安装rpm安装包
  5. php array_diff 用法
  6. open jdk 证书 问题
  7. 20165218 《网络对抗技术》Exp0 Kali安装 Week1
  8. 由脚本创建的新元素事件不触发和用的easyUI插件中的多选框不起作用的解决方法...
  9. 图说单播,组播,广播,选播和地域播
  10. (通用版)salesforce中soql及sosl的伪‘Like’模糊检索