Python 属性错误:';str';对象没有属性';isnumeric';
我有点困惑,因为我肯定我以前做过这个 我创建了以下方法Python 属性错误:';str';对象没有属性';isnumeric';,python,pandas,Python,Pandas,我有点困惑,因为我肯定我以前做过这个 我创建了以下方法 def p2f(x): if x.strip('%').isnumeric(): return float(x.strip('%'))/100 elif x in ['SUPP', 'NEW', 'LOWCOV', 'NA', '']: return 0.0 else: return x 但当我在导入的CSV文件上运行它时,会产生以下错误: AttributeErro
def p2f(x):
if x.strip('%').isnumeric():
return float(x.strip('%'))/100
elif x in ['SUPP', 'NEW', 'LOWCOV', 'NA', '']:
return 0.0
else:
return x
但当我在导入的CSV文件上运行它时,会产生以下错误:
AttributeError: 'str' object has no attribute 'isnumeric'
虽然我可以看到,isnumeric
是文档中str
的一个属性:
除非我没有正确解释信息?str.isnumeric()仅在Python 3上可用。该错误表明您使用的是Python 2,其中只存在
您应该真正使用,或者更好地使用异常处理:
.isnumeric()
匹配BMP中430个float()
无法接受的Unicode代码点,并且有一些代码点.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}')
范围内的i的(2**16):
c=chr(i)
如果c.isnumeric()或c.isdigit()或c.isdecimal():
尝试:
f=浮动(c)
除值错误外:
f=“”
di,de,nu=('\u2705'if test(),else'\u274c'用于测试in(c.isdigit,c.isdecimal,c.isnumeric))
print(f'{c!a:str.isnumeric()
方法仅为Python3.x。您使用的是Python2.x吗?
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>