Python程式確定給定索引處的Unicode碼點


Unicode碼點是表示Unicode字元集中一個字元的唯一數字。Unicode是一種字元編碼標準,用於為世界上每個字元分配唯一的程式碼。Unicode支援約13萬個字元,包括字母、符號和表情符號。我們可以使用Python中的ord()函式、codecs模組、unicodedata模組和array模組來確定特定索引處的Unicode碼點。本文將討論如何使用所有這些方法來確定給定索引處的Unicode碼點。

Unicode碼點

根據Unicode碼點,每個字元都由一個唯一的數字表示。碼點以十六進位制表示法表示,由“U+”字首後跟四位或五位十六進位制數字組成。

Python程式確定Unicode碼點

方法1:使用ord()函式。

我們可以使用Python中的ord()函式獲取給定索引處字元的Unicode碼。ord()函式以單個字元作為引數,並返回該字元的Unicode碼點。

語法

code_point = ord(string[index])

這裡,ord()函式以單個字元字串作為引數,並將其字元的Unicode碼點作為整數返回。

示例

在下面的示例中,我們首先獲取字串中特定索引處的字元,然後將其傳遞給Python中的ord()函式以獲取該字元的Unicode碼點。

# Get the Unicode code point at a given index
def get_unicode_code_point(string, index):
   char = string[index]
   code_point = ord(char)
   return code_point

# Test the function
string = "Hello, World!"
index = 1
code_point = get_unicode_code_point(string, index)
print(f"The Unicode code point of the character '{string[index]}' at index {index} is U+{code_point:04X}.")

輸出

The Unicode code point of the character 'e' at index 1 is U+0065.

方法2:使用codecs模組

codecs模組提供了一種名為codecs.encode()的方法,可用於以指定的編碼格式對字串進行編碼。我們可以使用此方法以UTF-8編碼格式對單個字元進行編碼,然後使用bytearray()函式將編碼後的字元轉換為位元組陣列。然後,我們可以使用struct模組從位元組中提取Unicode碼點。

語法

import codecs
byte_string = string.encode('utf-8')
code_point = int(codecs.encode(byte_string[index:index+1], 'hex'), 16)

這裡,我們使用codecs.encode()函式以十六進位制格式對位元組字串進行編碼,它返回一個“XX”形式的字串,其中XX是位元組的兩位十六進位制表示形式。我們使用int()函式(基數為16,因為字串為十六進位制格式)將此字串轉換為整數,以獲取字元的Unicode碼點。

示例

在下面的示例中,我們首先使用UTF-8編碼格式對字串“Hello, World!”中索引1處的字元進行編碼,並將結果位元組字串儲存在byte_string變數中。然後,我們將byte_string傳遞給codecs.decode()方法,指定'unicode_escape'編解碼器以將位元組字串解碼為Unicode轉義序列。這將生成一個Unicode字串,然後我們再次使用UTF-16BE編碼格式對其進行編碼,並將其儲存在code_point變數中。最後,我們使用int.from_bytes()方法將位元組字串轉換為整數,並使用格式化字串字面量列印帶有“U+”字首的十六進位制表示形式的Unicode碼點。

import codecs

string = "Hello, World!"
index = 1
char = string[index]
byte_string = char.encode('utf-8')
code_point = codecs.decode(byte_string, 'unicode_escape').encode('utf-16be')
code_point = int.from_bytes(code_point, byteorder='big')
print(f"The Unicode code point of the character '{string[index]}' at index {index} is U+{code_point:04X}.")

輸出

The Unicode code point of the character 'e' at index 1 is U+0065.

方法3:使用unicodedata模組

unicodedata模組提供了一個名為unicodedata.name()的函式,可用於獲取Unicode字元的名稱。我們可以使用此函式獲取給定索引處字元的名稱,然後使用unicodedata.lookup()函式獲取字元的Unicode碼點。

語法

import unicodedata
code_point = ord(char)
if unicodedata.combining(char):
   prev_char = string[index - 1]
   prev_code_point = ord(prev_char)
   code_point = prev_code_point + (code_point - 0xDC00) + ((prev_code_point - 0xD800) << 10)

這裡,我們首先獲取字串中指定索引處的字元並將其儲存在char變數中。然後,我們使用內建的ord()函式獲取字元的Unicode碼點。如果字元是組合字元(即修改前一個字元外觀的字元,例如重音符號),我們需要使用一些額外的邏輯來獲取完整的Unicode碼點。在這種情況下,我們獲取字串中的前一個字元並使用ord()獲取其Unicode碼點。然後,我們使用一些按位運算來組合這兩個碼點並獲取組合字元的完整Unicode碼點。

示例

在下面的示例中,我們使用unicodedata模組使用unicodedata.name()函式獲取字串“Hello, World!”中索引1處字元'e'的名稱。然後,我們使用int()函式從名稱中提取Unicode碼點,並使用格式化字串字面量(f-字串)以十六進位制表示法列印帶有“U+”字首的碼點。

import unicodedata

string = "Hello, World!"
index = 1
char = string[index]
name = unicodedata.name(char)
code_point = int(name.split(' ')[-1], 16)
print(f"The Unicode code point of the character '{string[index]}' at index {index} is U+{code_point:04X}.")

輸出

The Unicode code point of the character 'e' at index 1 is U+000E.

方法4:使用array模組

array模組提供了一個名為array.array()的類,可用於建立指定型別的陣列。我們可以建立一個無符號整數陣列,並將字串中每個字元的Unicode碼點附加到該陣列。然後,我們可以透過索引陣列來訪問給定索引處字元的Unicode碼點。

語法

import array
byte_array = array.array('b', char.encode('utf-8'))
code_point = int.from_bytes(byte_array, 'big')

這裡,我們首先使用UTF-8編碼格式對字串中指定索引處的字元進行編碼,並將結果位元組字串儲存在byte_array變數中作為有符號位元組陣列。然後,我們使用int.from_bytes()方法(位元組順序為'big')將位元組陣列轉換為整數值並獲取字元的Unicode碼點。

示例

在下面的示例中,我們使用array模組使用array.array()函式建立了一個無符號整數陣列。我們使用列表推導式將字串“Hello, World!”中每個字元的Unicode碼點附加到陣列中。然後,我們索引陣列以獲取索引1處字元的Unicode碼點。我們使用格式化字串字面量(f-字串)以十六進位制表示法列印帶有“U+”字首的碼點。

import array

string = "Hello, World!"
index = 1
code_points = array.array('I', [ord(char) for char in string])
code_point = code_points[index]
print(f"The Unicode code point of the character '{string[index]}' at index {index} is U+{code_point:04X}.")

輸出

The Unicode code point of the character 'e' at index 1 is U+0065.

結論

在本文中,我們討論瞭如何確定給定索引處的Unicode碼點。可以使用Python的ord()函式為每個字元確定Unicode碼點。Unicode碼點是為每個字元表示形式提供的唯一數字。

更新於: 2023年7月11日

3K+瀏覽量

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.