Python isnumeric 函数仅适用于 unicode

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16863696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:50:58  来源:igfitidea点击:

Python isnumeric function works only on unicode

python

提问by user1050619

I'm trying to check if a string is numeric or not, using the isnumericfunction, but the results are not as expected. The function works only if it's a unicode string.

我正在尝试使用该isnumeric函数检查字符串是否为数字,但结果与预期不符。该函数仅在它是 unicode 字符串时才有效。

>>> a=u'1'
>>> a.isnumeric()
True
>>> a='1'
>>> a.isnumeric()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'isnumeric'

isnumericworks only if its unicode. Any reason why?

isnumeric只有在它的 unicode 时才有效。有什么理由吗?

采纳答案by maazza

Often you will want to check if a string in Python is a number. This happens all the time, for example with user input, fetching data from a database (which may return a string), or reading a file containing numbers. Depending on what type of number you are expecting, you can use several methods. Such as parsing the string, using regex, or simply attempting to cast (convert) it to a number and see what happens. Often you will also encounter non-ASCII numbers, encoded in Unicode. These may or may not be numbers. For example ?, which is 2 in Thai.However ? is simply the copyright symbol, and is obviously not a number.

通常你会想要检查 Python 中的字符串是否是数字。这种情况一直发生,例如用户输入、从数据库中获取数据(可能返回一个字符串)或读取包含数字的文件。根据您期望的数字类型,您可以使用多种方法。例如解析字符串,使用正则表达式,或者只是尝试将它转换(转换)为一个数字,然后看看会发生什么。通常,您还会遇到以 Unicode 编码的非 ASCII 数字。这些可能是也可能不是数字。例如?,在泰语中是 2。然而 ?只是版权符号,显然不是数字。

link : http://pythoncentral.io/how-to-check-if-a-string-is-a-number-in-python-including-unicode/

链接:http: //pythoncentral.io/how-to-check-if-a-string-is-a-number-in-python-include-unicode/

回答by Artem Volkhin

Just different name.

只是名字不同。

'1'.isdigit() True

'1'.isdigit() 真

回答by Justin Ethier

According to the Python documentation, isnumericis only present for unicode objects:

根据Python 文档isnumeric仅适用于 unicode 对象:

The following methods are present only on unicode objects:

unicode.isnumeric()

Return True if there are only numeric characters in S, 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.

以下方法仅存在于 unicode 对象上:

unicode.isnumeric()

如果 S 中只有数字字符,则返回 True,否则返回 False。数字字符包括数字字符,以及所有具有Unicode 数值属性的字符,例如U+2155、庸俗分数五分之一。

回答by razvan.paul.blaga

isnumeric()has extended support for different numeral systemsin Unicode strings.

isnumeric()扩展了对Unicode 字符串中不同数字系统的支持。

In Americas and Europe the Hindu-Arabic numeral systemis used which consists of 0123456789 digits.

在美洲和欧洲,使用由 0123456789 数字组成的印度-阿拉伯数字系统

The Hindu-Arabic numerals are also called European digits by the Unicode.

印度-阿拉伯数字也被 Unicode 称为欧洲数字。

The are other numeral systems available such as:

还有其他可用的数字系统,例如:

  • Roman numerals
  • Ancient Greek numerals
  • Tamil numerals
  • Japaneese numerals
  • Chineese numerals
  • Korean numerals
  • 罗马数字
  • 古希腊数字
  • 泰米尔数字
  • 日文数字
  • 中文数字
  • 韩文数字

More information about numeral systems can be found here: wikiwand.com/en/Numerals_in_Unicode#/Numerals_by_script

有关数字系统的更多信息,请访问:wikiwand.com/en/Numerals_in_Unicode#/Numerals_by_script

Unicode subscript, superscriptand fractionsare also considered valid numerals by the isnumeric()function.

Unicode的subscriptsuperscript并且fractions也被认为是有效的数字isnumeric()功能。



You can use the isnumeric() function below to check if a string is a non-unicode number.

您可以使用下面的 isnumeric() 函数来检查字符串是否为非 unicode 数字。

l = ['abc' + chr(255), 'abc', '123', '45a6', '78b', u"\u2155", '123.4', u'\u2161', u'\u2168']

def isnumeric(s):
    '''Returns True for all non-unicode numbers'''
    try:
        s = s.decode('utf-8')
    except:
        return False

    try:
        float(s)
        return True
    except:
        return False


for i in l:
    print i, 'isnumeric:', isnumeric(i)

print '--------------------'
print u'\u2169', 'isnumeric', u'\u2169'.isnumeric()
print u'\u2165', 'isnumeric', u'\u2165'.isnumeric()

Edit: I'll update this post as soon as I have enough reputation to add more than 2 links to this answer.

编辑:一旦我有足够的声誉来为这个答案添加 2 个以上的链接,我就会更新这篇文章。