Slightly confused as I'm positive I've had this working before.
I've created the following method...
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
but when I run it on my imported CSV file is produces the error of
AttributeError: 'str' object has no attribute 'isnumeric'
Although I can see that isnumeric is an attribute of str in the documention....
Unless I'm not interpreting the information correctly?
str.isnumeric()
is only available on Python 3. The error indicates you are using Python 2 instead, where onlyunicode.isnumeric()
exists.You should really use
str.isdecimal()
, or better yet, use exception handling:.isnumeric()
matches 430 Unicode codepoints in the BMP thatfloat()
won't accept, and there are codepoints that.isdigit()
returns true for that are also not convertible.You can generate your own table to check with:
which produces output like:
and you'll find that only the
decimal
column has crosses for all non-convertible codepoints.If you want to use
isdecimal()
in Python 2, you'd have to decode your bytestring to Unicode first.