Python Unicode 資料庫
unicodedata 模組用於透過 Unicode 字元資料庫訪問所有 Unicode 字元。在此資料庫中,包含所有字元的字元屬性。
要使用此模組,我們需要在程式碼中匯入 **unicodedata** 模組。
import unicodedata
Unicode 資料庫方法
這裡描述了 unicodedata 模組的一些方法。
方法 (unicodedata.lookup(name)) −
此方法用於按名稱查詢字元。當名稱有效時,它應該返回該字元。否則,它將引發 KeyError。
方法 (unicodedata.name(chr[, default]))−
此方法用於將給定字元的名稱作為字串返回。如果給出了預設值,則當字元不在資料庫中時,它可能會返回預設值,否則它將引發 ValueError。
方法 (unicodedata.digit(chr[, default])) −
此方法用於返回給定字元的整數數字。如果給出了預設值,則當字元不存在或在資料庫中格式不正確時,它可能會返回預設值,否則它將引發 ValueError。
方法 (unicodedata.category(chr)) −
此方法用於返回分配給字元的通用類別。例如,對於字母,它將返回“L”,對於大寫字母,它將返回“u”,對於左括號,它將返回 Ps(標點開始)等。
方法 (unicodedata.mirrored(chr))−
此方法用於檢查字元是否有映象字元。一些字元有映象字元,例如“(”和“)”等。當它與映象字元匹配時,它將返回 1,否則返回 0。
示例程式碼
import unicodedata as ud
print(ud.lookup('ASTERISK'))
print(ud.lookup('Latin Capital letter G'))
#The Unicode name from the characters
print(ud.name(u'x'))
print(ud.name(u'°'))
#The Unicode character to decimal and numerics
print(ud.decimal(u'6'))
print(ud.numeric(u'9'))
#The Unicode character categoty
print(ud.category(u'A'))
print(ud.category(u'9'))
print(ud.category(u'[')) #Punctuation Start
#Unicode character to check whether mirrored or not
print(ud.mirrored(u'A'))
print(ud.mirrored(u'<'))
輸出
* G LATIN SMALL LETTER X DEGREE SIGN 6 9.0 Lu Nd Ps 0 1
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP