2つの方法は、特定のUnicode文字クラスをテストします。文字列内のすべて文字が指定された文字クラス(特定のUnicodeプロパティを持つ)である場合、テストはtrueです。
isdecimal()
文字列が10進数であるかどうかはテストされません。 documentationを参照してください:
Return true if all characters in the string are decimal characters and there is at least one character, false otherwise. Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”.
.
ピリオド文字がNd
カテゴリのメンバーではありません。小文字ではありません。
str.isdecimal()
の文字はstr.isnumeric()
のサブセットです。これはすべての数字をテストします。ここでも、documentationから:
Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.
Nd
はここNumeric_Type=Digit
です。
あなたは文字列が有効な進数であるかどうかをテストしたい場合は、単にフロートに変換してみてください。
def is_valid_decimal(s):
try:
float(s)
except ValueError:
return False
else:
return True
'isnumeric'と' isdecimal'テスト*文字のUnicodeプロパティ*。 10進数はテストしません。 –
詳細:[unicode.isdigit()とunicode.isnumeric()の違い(// stackoverflow.com/a/24384917) –