python?unicodedata模塊用法
UCD介紹
UCD是Unicode字符數(shù)據(jù)庫(kù)(Unicode Character DataBase)的縮寫。
UCD由一些描述Unicode字符屬性和內(nèi)部關(guān)系的純文本或html文件組成。
UCD中的文本文件大都是適合于程序分析的Unicode相關(guān)數(shù)據(jù)。其中的html文件解釋了數(shù)據(jù)庫(kù)的組織,數(shù)據(jù)的格式和含義。
UCD中最龐大的文件無(wú)疑就是描述漢字屬性的文件Unihan.txt。
在UCD 5.0,0中,Unihan.txt文件大小有28,221K字節(jié)。Unihan.txt中包含了很多有參考價(jià)值的索引,例如漢字部首、筆劃、拼音、使用頻度、四角號(hào)碼排序等。這些索引都是基于一些比較權(quán)威的辭典,但大多數(shù)索引只能檢索部分漢字。
unicodedata.lookup(name)
通過(guò)名稱來(lái)查找一個(gè)字符。如果字符存在就返回相應(yīng)字符,如果不存在拋出異常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])
通過(guò)字符來(lái)查找它的名稱。如果成功返回相應(yīng)名稱,否則拋出異常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])
返回表示數(shù)字字符的數(shù)值。如果給一個(gè)沒(méi)有數(shù)字的值時(shí),會(huì)拋出異常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])
把一個(gè)合法的數(shù)字字符串轉(zhuǎn)換為數(shù)字值,比如0到9的字符串轉(zhuǎn)換為相應(yīng)的數(shù)字值。如果非法的字符串,拋出異常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])
把一個(gè)表示數(shù)字的字符串轉(zhuǎn)換為浮點(diǎn)數(shù)返回。比如可以把‘8’,‘四’轉(zhuǎn)換數(shù)值輸出。與digit()不一樣的地方是它可以任意表示數(shù)值的字符都可以,不僅僅限于0到9的字符。如果不是合法字符,會(huì)拋出異常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)
把一個(gè)字符返回它在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)
把一個(gè)字符給出它的分類,以便進(jìn)行從左到右,還是從右到左的排列。如果沒(méi)有定義,返回空字符串。
>>> 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)
把字符的權(quán)威組合值返回,如果沒(méi)有定義,默認(rèn)是返回0。當(dāng)正規(guī)化操作時(shí),可以根據(jù)這個(gè)值進(jìn)行排序,大的值排在小的值后面。
>>> import unicodedata >>> print(unicodedata.combining('9')) 0 >>> >>> print(unicodedata.combining('A')) 0 >>>
unicodedata.east_asian_width(chr)
把字符顯示的寬度返回。具體內(nèi)容如下:
‘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)
判斷一個(gè)字符是否支持鏡像屬性,如果支持返回1,否則返回0.
>>> import unicodedata >>> print(unicodedata.mirrored('9')) 0 >>> >>> print(unicodedata.mirrored('A')) 0 >>> >>> print(unicodedata.mirrored('蔡')) 0 >>>
unicodedata.decomposition(chr)
把一個(gè)可分解的字符分成兩個(gè)16進(jìn)制的值返回,如果不可分解,返回空。
>>> import unicodedata >>> print(unicodedata.decomposition('9')) >>> >>> print(unicodedata.decomposition('-')) >>> >>> print(unicodedata.decomposition('蔡')) >>> >>> print(unicodedata.decomposition('ガ')) 30AB 3099 >>>
unicodedata.normalize(form, unistr)
把一串UNICODE字符串轉(zhuǎn)換為普通格式的字符串,具體格式支持NFC、NFKC、NFD和NFKD格式。一些文本元素即可以使用靜態(tài)的預(yù)先組合好的形式,也可使用動(dòng)態(tài)組合的形式。Unicode字符的不同表示序列被認(rèn)為是等價(jià)的。如果兩個(gè)或多個(gè)序列被認(rèn)為是等價(jià)的,Unicode標(biāo)準(zhǔn)不規(guī)定哪一種特定的序列是正確的,而認(rèn)為每一個(gè)序列只不過(guò)與其它序列等價(jià)。
如 果需要一種單一的單一的表示方式,可以使用一種規(guī)范化的Unicode文本形式來(lái)減少不想要區(qū)別。Unicode標(biāo)準(zhǔn)定義了四種規(guī)范化形式: Normalization Form D (NFD),Normalization Form KD (NFKD),Normalization Form C (NFC),和Normalization Form KC (NFKC)。大約來(lái)說(shuō),NFD和NFKD將可能的字符進(jìn)行分解,而NFC和NFKC將可能的字符進(jìn)行組合。
>>> 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(‘a(chǎn)scii','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
返回當(dāng)前Unicode使用的數(shù)據(jù)庫(kù)的版本。
unicodedata.ucd_3_2_0
提供ucd3.2的對(duì)象方式訪問(wèn),以便兼容舊的IDNA的應(yīng)用程序。
>>> import unicodedata >>> print(unicodedata.unidata_version) 9.0.0 >>> >>> print(unicodedata.ucd_3_2_0) <unicodedata.UCD object at 0x00000215E3EA3B70> >>>
下面來(lái)仔細(xì)查看一個(gè)字符的UNICODE數(shù)據(jù):
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
上面大多的函數(shù)都是針對(duì)這些數(shù)據(jù)信息進(jìn)行查詢,并且返回相應(yīng)的值。
以上就是python unicodedata模塊用法的詳細(xì)內(nèi)容,更多關(guān)于python unicodedata模塊的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python 使用百度AI接口進(jìn)行人臉對(duì)比的步驟
這篇文章主要介紹了python 使用百度AI接口進(jìn)行人臉對(duì)比的步驟,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03pandas中DataFrame新增行及global變量的使用方式
這篇文章主要介紹了pandas中DataFrame新增行及global變量的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02從零學(xué)python系列之?dāng)?shù)據(jù)處理編程實(shí)例(二)
這篇文章主要介紹了python數(shù)據(jù)處理編程實(shí)例,需要的朋友可以參考下2014-05-05Python批量實(shí)現(xiàn)word中查找關(guān)鍵字的示例代碼
本文主要介紹了Python批量實(shí)現(xiàn)word中查找關(guān)鍵字的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07django admin實(shí)現(xiàn)動(dòng)態(tài)多選框表單的示例代碼
借助django-admin,可以快速得到CRUD界面,但若需要?jiǎng)?chuàng)建多選標(biāo)簽字段時(shí),需要對(duì)表單進(jìn)行調(diào)整,本文通過(guò)示例代碼給大家介紹django admin多選框表單的實(shí)現(xiàn)方法,感興趣的朋友跟隨小編一起看看吧2021-05-05Python操作Elasticsearch處理timeout超時(shí)
這篇文章主要介紹了Python操作Elasticsearch處理timeout超時(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Python中關(guān)于matplotlib圖片的灰度處理方式
這篇文章主要介紹了Python中關(guān)于matplotlib圖片的灰度處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08