두 가지 방법은 특정 유니 코드 문자 클래스를 테스트합니다. 문자열의 모든 문자가 지정된 문자 클래스 (특정 유니 코드 속성 보유)의 문자이면 테스트가 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
입니다.
당신은 문자열이 유효한 진수 인 경우 단지 float로 변환하려고 테스트하려면 :
def is_valid_decimal(s):
try:
float(s)
except ValueError:
return False
else:
return True
은 'isnumeric'과'isdecimal' 테스트 * 캐릭터의 유니 코드 속성 *. 10 진수는 테스트하지 않습니다. –
밀접하게 관련 : [차이점은 unicode.isdigit()와 unicode.isnumeric()] (// stackoverflow.com/a/24384917) –