栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

AttributeError:'str'对象没有属性'isnumeric'

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AttributeError:'str'对象没有属性'isnumeric'

str.isnumeric()
仅在Python 3上可用。该错误表明您使用的是Python 2,而只有Python
2
unipre.isnumeric()
存在。

您应该真正使用

str.isdecimal()
或更好地使用 异常处理

def p2f(x):    try:        return float(x.strip('%'))/100    except ValueError:        return 0.0 if x in ('SUPP', 'NEW', 'LOWCOV', 'NA', '') else x

.isnumeric()
匹配
float()
不接受的BMP中的430个Unipre代码点,并且有些代码点
.isdigit()
返回true也不能转换。

您可以生成自己的表进行检查:

for i in range(2 ** 16):    c = chr(i)    if c.isnumeric() or c.isdigit() or c.isdecimal():        try: f = float(c)        except ValueError: f = '<not convertible>'        di, de, nu = ('u2705' if test() else 'u274c' for test in (c.isdigit, c.isdecimal, c.isnumeric))        print(f'{c!a:<6} {c}tdigit: {di}   decimal: {de}   numeric: {nu}  float: {f}')

产生如下输出:

'0'    0    digit: ✅   decimal: ✅   numeric: ✅  float: 0.0'1'    1    digit: ✅   decimal: ✅   numeric: ✅  float: 1.0'2'    2    digit: ✅   decimal: ✅   numeric: ✅  float: 2.0'3'    3    digit: ✅   decimal: ✅   numeric: ✅  float: 3.0'4'    4    digit: ✅   decimal: ✅   numeric: ✅  float: 4.0'5'    5    digit: ✅   decimal: ✅   numeric: ✅  float: 5.0'6'    6    digit: ✅   decimal: ✅   numeric: ✅  float: 6.0'7'    7    digit: ✅   decimal: ✅   numeric: ✅  float: 7.0'8'    8    digit: ✅   decimal: ✅   numeric: ✅  float: 8.0'9'    9    digit: ✅   decimal: ✅   numeric: ✅  float: 9.0'xb2' ²    digit: ✅   decimal: ❌   numeric: ✅  float: <not convertible>'xb3' ³    digit: ✅   decimal: ❌   numeric: ✅  float: <not convertible>'xb9' ¹    digit: ✅   decimal: ❌   numeric: ✅  float: <not convertible>'xbc' ¼    digit: ❌   decimal: ❌   numeric: ✅  float: <not convertible>'xbd' ½    digit: ❌   decimal: ❌   numeric: ✅  float: <not convertible>'xbe' ¾    digit: ❌   decimal: ❌   numeric: ✅  float: <not convertible>

并且您会发现只有该

decimal
列的所有不可转换代码点都有叉号。

如果要

isdecimal()
在Python 2中使用,则必须先将字节字符串解码为Unipre。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/660227.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号