为什么 isnumeric 不起作用? [英] Why isn't isnumeric working?

查看:65
本文介绍了为什么 isnumeric 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一个非常简单的 python3 使用字符串操作指南,然后我遇到了这个奇怪的错误:

In [4]: # 创建字符串string = '让我们测试一下.'# 测试它是否是数字string_isnumeric = string.isnumeric()输出 [4]:AttributeError Traceback(最近一次调用最后一次)<ipython-input-4-859c9cefa0f0>在 <module>()34 # 测试是否是数字---->5 string_isnumeric = string.isnumeric()AttributeError: 'str' 对象没有属性 'isnumeric'

问题是,据我所知,str DOES 有一个属性,isnumeric.

解决方案

不,str 对象没有 isnumeric 方法.isnumeric 仅适用于 unicode 对象.换句话说:

<预><代码>>>>d = unicode('一些字符串', 'utf-8')>>>d.isnumeric()错误的>>>d = unicode('42', 'utf-8')>>>d.isnumeric()真的

I was going through a very simple python3 guide to using string operations and then I ran into this weird error:

In [4]: # create string
        string = 'Let\'s test this.'

        # test to see if it is numeric
        string_isnumeric = string.isnumeric()

Out [4]: AttributeError                            Traceback (most recent call last)
         <ipython-input-4-859c9cefa0f0> in <module>()
                    3 
                    4 # test to see if it is numeric
              ----> 5 string_isnumeric = string.isnumeric()

         AttributeError: 'str' object has no attribute 'isnumeric'

The problem is that, as far as I can tell, str DOES have an attribute, isnumeric.

解决方案

No, str objects do not have an isnumeric method. isnumeric is only available for unicode objects. In other words:

>>> d = unicode('some string', 'utf-8')
>>> d.isnumeric()
False
>>> d = unicode('42', 'utf-8')
>>> d.isnumeric()
True

这篇关于为什么 isnumeric 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆