python unicodedata模块用法

目录
  • UCD介绍
    • unicodedata.lookup(name)
    • unicodedata.name(chr[,default])
    • unicodedata.decimal(chr[, default])
    • unicodedata.digit(chr[, default])
    • unicodedata.numeric(chr[, default])
    • unicodedata.category(chr)
    • unicodedata.bidirectional(chr)
    • unicodedata.combining(chr)
    • unicodedata.east_asian_width(chr)
    • unicodedata.mirrored(chr)
    • unicodedata.decomposition(chr)
    • unicodedata.normalize(form, unistr)
    • unicodedata.unidata_version

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

上面大多的函数都是针对这些数据信息进行查询,并且返回相应的值。

以上就是python unicodedata模块用法的详细内容,更多关于python unicodedata模块的资料请关注我们其它相关文章!

(0)

相关推荐

  • python中argparse模块基础及使用步骤

    目录 argparse模块用法 一. 概念 二. 基础 1. 使用步骤 1.1 总步骤 1.2 创建对象 1.3 添加参数 1.4 解析参数 三. 使用案例 argparse模块用法 一. 概念 argsparse是python的命令行解析的标准模块,内置于python,不需要安装.这个库可以让我们直接在命令行中就可以向程序中传入参数并让程序运行. 官方文档的位置:[https://docs.python.org/zh-cn/3/library/argparse.html] 在这里我们利用git

  • Python中模块的使用--binascii模块用法

    目录 binascii模块用法 binascii模块和进制转换笔记 Python内置函数 binascii模块用法 binascii模块用于在二进制和ASCII之间转换 >> import binascii # 将binary 转ascii并用十六进制表示 >> str1 = b"hello world"   >> binascii.b2a_hex(b"hello world") # 输出 b'68656c6c6f20776f72

  • python处理excel文件之xlsxwriter 模块

    目录 模块基本使用 写入更多样式数据 其余样式扩展 xlsxwriter 中的 write 方法 xlsxwriter 关闭文件 其它需要了解的方法 xlsxwriter 模块的优缺点 优点 缺点 模块安装: pip install xlsxwriter 安装完毕,直接在文件中进行模块导入,测试是够存在BUG. import xlsxwriter 该模块看名字就能知道其用法,它是用来向 Excel 中写入数据的模块,其中限制 Excel 版本为2007+. 模块基本使用 接下来完成一个基本的流程

  • Python实现以主程序的形式执行模块

    前言: 这个先来创建一个模块,名称为christmastree,在该模块中,首先定义一个全局变量,然后创建一个名称为fun_christmastree()的函数,最后再通过print()函数输出一写内容. 代码如下: printree = "我是小菜鸡" # 定义全局变量"小菜鸡" def fun_christmastree(): # 定义函数 """功能: 一个梦 :return 无返回值 """ pri

  • Python使用apscheduler模块设置定时任务的实现

    目录 一.安装 二.ApScheduler 简介 1 APScheduler的组件 2 调度器的种类 3 内置的触发器类型 三.使用举例 1 使用date类型的触发器 2 使用interval类型的触发器 3 使用cron类型的触发器 四.定时器使用装饰器的方法 一.安装 pip install apscheduler 二.ApScheduler 简介 1 APScheduler的组件 triggers:触发器triggers包含任务执行的调度逻辑,决定任务按照什么逻辑进行定时执行 job st

  • python unicodedata模块用法

    目录 UCD介绍 unicodedata.lookup(name) unicodedata.name(chr[,default]) unicodedata.decimal(chr[, default]) unicodedata.digit(chr[, default]) unicodedata.numeric(chr[, default]) unicodedata.category(chr) unicodedata.bidirectional(chr) unicodedata.combining

  • Python pickle模块用法实例分析

    本文实例讲述了Python pickle模块用法.分享给大家供大家参考.具体分析如下: pickle提供了一个简单的持久化功能.可以将对象以文件的形式存放在磁盘上. pickle.dump(obj, file[, protocol]) 序列化对象,并将结果数据流写入到文件对象中.参数protocol是序列化模式,默认值为0,表示以文本的形式序列化.protocol的值还可以是1或2,表示以二进制的形式序列化. pickle.load(file) 反序列化对象.将文件中的数据解析为一个Python

  • Python random模块用法解析及简单示例

    用法示例: import random # 1)随机小数 print(random.random()) # 获取大于0且小于1 之间的小数 random.random() print(random.uniform(1, 4)) # 获取大于1小于3的小数 # 2)随机整数 print(random.randint(1, 9)) # 获取大于等于1且小于等于9之间的整数 print(random.randrange(1, 9)) # 获取大于等于1且小于9之间的整数 print(random.ra

  • Python pymongo模块用法示例

    本文实例讲述了Python pymongo模块用法.分享给大家供大家参考,具体如下: MongoDB优点 MongoDB是一个为当代web应用而生的noSQL数据库,它有如下优点: 1.文档型存储.可以把关系型数据库的表理解为一个电子表格,列表示字段,每行的记录其实是按照列的字段顺序排列的值得元组.而存储在MongoDB中的文档被存储为键-值对的形式,值却可以是任意类型且可以嵌套.之前在用关系型数据库的时候,我们把产品信息打散到不同的表中,要通过关系表或者使用join拼接成复杂的SQL语句的方式

  • Python shutil模块用法实例分析

    本文实例讲述了Python shutil模块用法.分享给大家供大家参考,具体如下: shutil模块 主要作用与拷贝文件用的. 1.shutil.copyfileobj(文件1,文件2):将文件1的数据覆盖copy给文件2. import shutil f1 = open("1.txt",encoding="utf-8") f2 = open("2.txt","w",encoding="utf-8") sh

  • Python minidom模块用法示例【DOM写入和解析XML】

    本文实例讲述了Python minidom模块用法.分享给大家供大家参考,具体如下: 一.DOM写XML文件 # -*- coding:utf-8 -*- #!python3 #导入minidom from xml.dom import minidom # 1.创建DOM树对象 dom=minidom.Document() # 2.创建根节点.每次都要用DOM对象来创建任何节点. root_node=dom.createElement('root') # 3.用DOM对象添加根节点 dom.ap

  • Python unittest模块用法实例分析

    本文实例讲述了Python unittest模块用法.分享给大家供大家参考,具体如下: python的unittest模块提供了一个测试框架,只要我们写一个继承unittest.TestCase的类,类中用setUp做初始化,用tearDown做清理. 主要用到的函数有: failedinfo表示不成立打印信息failedinfo,为可选参数 self.fail([msg])会无条件的导致测试失败,不推荐使用. self.assertEqual(value1, value2, failedinf

  • Python logging模块用法示例

    本文实例讲述了Python logging模块用法.分享给大家供大家参考,具体如下: logging模块 函数式简单配置 import logging logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message') logging.bas

  • Python pygorithm模块用法示例【常见算法测试】

    本文实例讲述了Python pygorithm模块用法.分享给大家供大家参考,具体如下: pygorithm:一个用纯粹python编写的Python模块,用于纯粹的教育目的.只需导入所需的算法即可获取代码,时间复杂度等等.开始学习Python编程的好方法.了解Python中所有主要算法的实现.不需要上网就可以获得所需的代码. 安装 pip3 install pygorithm 常见函数 斐波那契数列 from pygorithm.fibonacci import recursion resul

  • Python hashlib模块用法实例分析

    本文实例讲述了Python hashlib模块用法.分享给大家供大家参考,具体如下: 一.hashlib基本使用 python中的hashlib模块用来进行hash或者md5加密,而且这种加密是不可逆的,所以这种算法又被称为摘要算法.其支持Openssl库提供的所有算法,包括md5.sha1.sha224.sha256.sha512等. 常用的属性和方法: algorithms:列出所有加密算法('md5','sha1','sha224','sha256','sha384','sha512')

随机推荐