'\u00BD' # ½
'\u00B2' # ²
我想了解isdecimal()和isdigit()更好,因此它對於理解unicode數值屬性是必需的。我將如何看到例如上述兩個unicode的數值屬性。如何顯示Unicodes的數值屬性
'\u00BD' # ½
'\u00B2' # ²
我想了解isdecimal()和isdigit()更好,因此它對於理解unicode數值屬性是必需的。我將如何看到例如上述兩個unicode的數值屬性。如何顯示Unicodes的數值屬性
要獲得包含在字符「數值」,你可以使用unicodedata.numeric()
function:
>>> import unicodedata
>>> unicodedata.numeric('\u00BD')
0.5
使用ord()
function得到整數碼點,可選擇地結合format()
產生一個十六進制值:
>>> ord('\u00BD')
189
>>> format(ord('\u00BD'), '04x')
'00bd'
您可以使用unicodedata.category()
訪問角色屬性,然後您需要檢查文檔類別:
>>> unicodedata('\u00DB')
'No'
其中'No'
stands for Number, Other。
但是,類別Lo
中有一系列.isnumeric() == True
字符; Python unicodedata
數據庫僅允許您訪問常規類別,並依賴於str.isdigit()
,str.isnumeric()
和unicodedata.digit()
,unicodedata.numeric()
等方法來處理其他類別。
如果您想要所有數字Unicode字符的精確列表,則規範來源爲Unicode database;一系列定義整個標準的文本文件。 DerivedNumericTypes.txt
file (v. 6.3.0)爲您提供了一個特定數字屬性的「數據庫視圖」;它會在頂部告訴您文件是如何從標準中的其他數據文件派生的。同上DerivedNumericValues.txt
file,列出每個碼點的確切數值。
the docs明確指定方法和Numeric_Type
屬性之間的關係。
def is_decimal(c):
"""Whether input character is Numeric_Type=decimal."""
return c.isdecimal() # it means General Category=Decimal Number in Python
def is_digit(c):
"""Whether input character is Numeric_Type=digit."""
return c.isdigit() and not c.isdecimal()
def is_numeric(c):
"""Whether input character is Numeric_Type=numeric."""
return c.isnumeric() and not c.isdigit() and not c.isdecimal()
例子:
>>> for c in '\u00BD\u00B2':
... print("{}: Numeric: {}, Digit: {}, Decimal: {}".format(
... c, is_numeric(c), is_digit(c), is_decimal(c)))
...
½: Numeric: True, Digit: False, Decimal: False
²: Numeric: False, Digit: True, Decimal: False
我不知道Decimal Number和Numeric_Type=Decimal將永遠是相同的。
注意:'\u00B2'
不是十進制的,因爲標準明確排除了上標,請參閱4.6 Numerical Value (Unicode 6.2)。
我認爲OP希望0.5和2代碼點,而不是他們的代碼點。 – delnan
@delnan:檢查,也補充說。 –
我的問題可能是錯誤的 - 然後我讀了關於屬性值Numeric_Type = Digit,Numeric_Type = Decimal,Numeric_Type = Numeric我想知道我是否可以從某個unicode點產生這個屬性? – Phoenix