最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python?unicodedata模塊用法

 更新時間:2022年06月23日 11:00:18   作者:周小董  
這篇文章主要為大家介紹了python?unicodedata模塊用法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

UCD介紹

UCD是Unicode字符數(shù)據(jù)庫(Unicode Character DataBase)的縮寫。

UCD由一些描述Unicode字符屬性和內(nèi)部關(guān)系的純文本或html文件組成。

UCD中的文本文件大都是適合于程序分析的Unicode相關(guān)數(shù)據(jù)。其中的html文件解釋了數(shù)據(jù)庫的組織,數(shù)據(jù)的格式和含義。

UCD中最龐大的文件無疑就是描述漢字屬性的文件Unihan.txt。

在UCD 5.0,0中,Unihan.txt文件大小有28,221K字節(jié)。Unihan.txt中包含了很多有參考價值的索引,例如漢字部首、筆劃、拼音、使用頻度、四角號碼排序等。這些索引都是基于一些比較權(quán)威的辭典,但大多數(shù)索引只能檢索部分漢字。

unicodedata.lookup(name)

通過名稱來查找一個字符。如果字符存在就返回相應(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])

通過字符來查找它的名稱。如果成功返回相應(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ù)值。如果給一個沒有數(shù)字的值時,會拋出異常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])

把一個合法的數(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])

把一個表示數(shù)字的字符串轉(zhuǎn)換為浮點數(shù)返回。比如可以把‘8’,‘四’轉(zhuǎn)換數(shù)值輸出。與digit()不一樣的地方是它可以任意表示數(shù)值的字符都可以,不僅僅限于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)

把字符的權(quán)威組合值返回,如果沒有定義,默認(rèn)是返回0。當(dāng)正規(guī)化操作時,可以根據(jù)這個值進行排序,大的值排在小的值后面。

>>> 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)

判斷一個字符是否支持鏡像屬性,如果支持返回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字符串轉(zhuǎn)換為普通格式的字符串,具體格式支持NFC、NFKC、NFD和NFKD格式。一些文本元素即可以使用靜態(tài)的預(yù)先組合好的形式,也可使用動態(tài)組合的形式。Unicode字符的不同表示序列被認(rèn)為是等價的。如果兩個或多個序列被認(rèn)為是等價的,Unicode標(biāo)準(zhǔn)不規(guī)定哪一種特定的序列是正確的,而認(rèn)為每一個序列只不過與其它序列等價。

如 果需要一種單一的單一的表示方式,可以使用一種規(guī)范化的Unicode文本形式來減少不想要區(qū)別。Unicode標(biāo)準(zhǔn)定義了四種規(guī)范化形式: 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(‘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ù)庫的版本。

unicodedata.ucd_3_2_0

提供ucd3.2的對象方式訪問,以便兼容舊的IDNA的應(yīng)用程序。

>>> import unicodedata
>>> print(unicodedata.unidata_version)
9.0.0
>>>
>>> print(unicodedata.ucd_3_2_0)
<unicodedata.UCD object at 0x00000215E3EA3B70>
>>>

下面來仔細(xì)查看一個字符的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ù)都是針對這些數(shù)據(jù)信息進行查詢,并且返回相應(yīng)的值。

以上就是python unicodedata模塊用法的詳細(xì)內(nèi)容,更多關(guān)于python unicodedata模塊的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python編程中用close()方法關(guān)閉文件的教程

    Python編程中用close()方法關(guān)閉文件的教程

    這篇文章主要介紹了Python編程中用close()方法關(guān)閉文件的教程,是Python編程入門中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05
  • python實現(xiàn)移動木板小游戲

    python實現(xiàn)移動木板小游戲

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)移動木板小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Django中URL的參數(shù)傳遞的實現(xiàn)

    Django中URL的參數(shù)傳遞的實現(xiàn)

    這篇文章主要介紹了Django中URL的參數(shù)傳遞的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 使用python 進行區(qū)間取值的方法

    使用python 進行區(qū)間取值的方法

    這篇文章主要介紹了使用python 進行區(qū)間取值的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-07-07
  • 詳解Python中Pytest和Unittest的區(qū)別

    詳解Python中Pytest和Unittest的區(qū)別

    Pytest?和?Unittest是Python中屬于最常用的兩個測試框架。那么他們有些什么區(qū)別呢??Playwright?為什么只給了Pytest的深度支持,而不是Unittest呢?本文就來和大家詳細(xì)聊聊
    2023-03-03
  • python和shell變量互相傳遞的幾種方法

    python和shell變量互相傳遞的幾種方法

    這篇文章主要介紹了python和shell變量互相傳遞方法,使用了環(huán)境變量、管道等方法
    2013-11-11
  • keras分類模型中的輸入數(shù)據(jù)與標(biāo)簽的維度實例

    keras分類模型中的輸入數(shù)據(jù)與標(biāo)簽的維度實例

    這篇文章主要介紹了keras分類模型中的輸入數(shù)據(jù)與標(biāo)簽的維度實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Python序列化模塊JSON與Pickle

    Python序列化模塊JSON與Pickle

    這篇文章介紹了Python的序列化模塊JSON與Pickle,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • Django中的Signal代碼詳解

    Django中的Signal代碼詳解

    這篇文章主要介紹了Django中的Signal代碼詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • Python循環(huán)語句For?Range用法示例詳解

    Python循環(huán)語句For?Range用法示例詳解

    這篇文章主要為大家介紹了Python循環(huán)語句For?Range用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09

最新評論

海兴县| 什邡市| 古蔺县| 贡嘎县| 习水县| 济阳县| 碌曲县| 九龙县| 许昌市| 沧州市| 云安县| 凉城县| 阜新| 巴楚县| 乐山市| 和顺县| 南通市| 贵定县| 前郭尔| 金塔县| 舒城县| 分宜县| 合江县| 尉氏县| 图木舒克市| 鹤壁市| 平遥县| 攀枝花市| 乌兰浩特市| 益阳市| 都匀市| 通化县| 区。| 双柏县| 岳阳市| 蒙阴县| 青川县| 唐山市| 香河县| 靖安县| 慈溪市|