How can I tell when a string is in fact a number?
parkw at better.net
Sun Nov 5 21:40:18 CET 2000
On Sun, Nov 05, 2000 at 02:50:16PM +0000, Gaute B Strokkenes wrote:
> I've been using Python for something slightly more complicated than
> simple 'Hello World' programs for the first time in my life. I find
> it very comfortable, but on the other hand I've also got this snippet
> in my program:
> def isanum(str):
> # FIXME: Surely there must be a sane way of doing this.
> from string import find, digits
> for i in range(len(str)):
> if find(digits, str[i]) == -1:
> return 0
> return 1
> As the comment says, I'm sure there must be a more straightforward way
> to do this. However, I can't find out how, though I'm sure that it is
> really my relative unfamiliarity with Python that is to blame.
if re.search(str, '\D') == None: return 1
else: return 0
'\D' is non-digit (complement to '\d').
---William Park, Open Geometry Consulting
More information about the Python-list