A way of checking if a string contains a number

H

Hamish

Hey

I'm new to python, but I have used a fair bit of C and Perl

I found Perls regex's to be very easy to use however I don't find
Pythons regexes as good.

All I am trying to do is detect if there is a number in a string.

I am reading the string from an excel spread sheet using the xlrd
module

then I would like to test if this string has a number in it

ie.
import xlrd
import re

doesHaveNumber = re.compile('[0-9]')
string1 = ABC 11

regularExpressionCheck = doesHaveNumber.search(string1)

This will get the right result but then I would like to use the result
in an IF statement and have not had much luck with it.

if regularExpressionCheck != "None"
print "Something"

the result is that it prints every line from the spreadsheet to output
but I only want the lines from the spreadsheet to output.

Is there a way I can drop the regular expression module and just use
built in string processing?

Why si the output from checks in teh re module either "None" or some
crazy memory address? Couldn't it be just true or false?
 
J

jason.s.trowbridge

Hey

I'm new to python, but I have used a fair bit of C and Perl

I found Perls regex's to be very easy to use however I don't find
Pythons regexes as good.

All I am trying to do is detect if there is a number in a string.

I am reading the string from an excel spread sheet using the xlrd
module

then I would like to test if this string has a number in it

ie.
import xlrd
import re

doesHaveNumber = re.compile('[0-9]')
string1 = ABC 11

regularExpressionCheck = doesHaveNumber.search(string1)

This will get the right result but then I would like to use the result
in an IF statement and have not had much luck with it.

if regularExpressionCheck != "None"
print "Something"

the result is that it prints every line from the spreadsheet to output
but I only want the lines from the spreadsheet to output.

Is there a way I can drop the regular expression module and just use
built in string processing?

Why si the output from checks in teh re module either "None" or some
crazy memory address? Couldn't it be just true or false?

None isn't a string, None is a special object under Python. Functions
that don't return anything actually return the None value. In a
boolean context, None is false.

Python objects can be used in a boolean context, such as an if-
statement. Empty sequences (such as tuples, strings, and lists),
None, 0, and 0.0 are treated as False. Everything else is usually
treated as "True". If a class defines a __len__ or __nonzero__
method, those methods are called to determine if an instance is True
or False. Otherwise, all objects are True.

There's also only one None object, so identity testing is the pythonic
idiom for testing against None.

So, your check needs to be:
if regularExpressionCheck is not None:
print "Something"

or, even more simply:
if regularExpressionCheck:
print "Something"

I recommend that you peruse a Python tutorial or two. These are
fairly basic attributes of the Python language. If you're using
regular expressions, you'll want to be aware of how to make raw
strings to help you out.

--Jason
 
L

Larry Bates

Hamish said:
Hey

I'm new to python, but I have used a fair bit of C and Perl

I found Perls regex's to be very easy to use however I don't find
Pythons regexes as good.

All I am trying to do is detect if there is a number in a string.

I am reading the string from an excel spread sheet using the xlrd
module

then I would like to test if this string has a number in it

ie.
import xlrd
import re

doesHaveNumber = re.compile('[0-9]')
string1 = ABC 11

regularExpressionCheck = doesHaveNumber.search(string1)

This will get the right result but then I would like to use the result
in an IF statement and have not had much luck with it.

if regularExpressionCheck != "None"
print "Something"

the result is that it prints every line from the spreadsheet to output
but I only want the lines from the spreadsheet to output.

Is there a way I can drop the regular expression module and just use
built in string processing?

Why si the output from checks in teh re module either "None" or some
crazy memory address? Couldn't it be just true or false?
regularExpressionCheck won't ever contain the characters "None". The result
if doesHaveNumber.search(string1) is a match object not a string. IMHO regular
expressions are overkill for the task you describe. You may be better served to
just try to convert it to whatever number you want.

try:
value=int(string1)

except ValueError:
# do whatever you want for non integers here

else:
# do whatever you want for integers here

Or use string methods:

if string1.isdigit():
print "digits found"
else:
print "alphas found"

-Larry
 
H

Hamish

Thanks worked Perfectly



I'm new to python, but I have used a fair bit of C and Perl
I found Perls regex's to be very easy to use however I don't find
Pythons regexes as good.
All I am trying to do is detect if there is a number in a string.
I am reading the string from an excel spread sheet using the xlrd
module
then I would like to test if this string has a number in it
ie.
import xlrd
import re
doesHaveNumber = re.compile('[0-9]')
string1 = ABC 11
regularExpressionCheck = doesHaveNumber.search(string1)
This will get the right result but then I would like to use the result
in an IF statement and have not had much luck with it.
if regularExpressionCheck != "None"
print "Something"
the result is that it prints every line from the spreadsheet to output
but I only want the lines from the spreadsheet to output.
Is there a way I can drop the regular expression module and just use
built in string processing?
Why si the output from checks in teh re module either "None" or some
crazy memory address? Couldn't it be just true or false?

None isn't a string, None is a special object under Python. Functions
that don't return anything actually return the None value. In a
boolean context, None is false.

Python objects can be used in a boolean context, such as an if-
statement. Empty sequences (such as tuples, strings, and lists),
None, 0, and 0.0 are treated as False. Everything else is usually
treated as "True". If a class defines a __len__ or __nonzero__
method, those methods are called to determine if an instance is True
or False. Otherwise, all objects are True.

There's also only one None object, so identity testing is the pythonic
idiom for testing against None.

So, your check needs to be:
if regularExpressionCheck is not None:
print "Something"

or, even more simply:
if regularExpressionCheck:
print "Something"

I recommend that you peruse a Python tutorial or two. These are
fairly basic attributes of the Python language. If you're using
regular expressions, you'll want to be aware of how to make raw
strings to help you out.

--Jason- Hide quoted text -

- Show quoted text -
 
Z

Zero Piraeus

:
[...] IMHO regular
expressions are overkill for the task you describe. You may be better served to
just try to convert it to whatever number you want.

try:
value=int(string1)

Fails for the OP's example:
Traceback (most recent call last):
Or use string methods:

if string1.isdigit():
print "digits found"
else:
print "alphas found"

Again ...
False

If you were desperate to avoid regexes, this works:
>>> max([(d in string1) for d in "0123456789"])
True

.... but it's not exactly legible.

-[]z.
 
B

Bruno Desthuilliers

Larry Bates a écrit :
IMHO
regular expressions are overkill for the task you describe.

There are cases where regexps are the right tool, and according to the
exemple given, this may be one (now it's of course hard to tell without
seeing a decent sample of real data...).
 
M

MRAB

:
[...] IMHO regular
expressions are overkill for the task you describe. You may be better served to
just try to convert it to whatever number you want.
try:
value=int(string1)

Fails for the OP's example:
Traceback (most recent call last):
Or use string methods:
if string1.isdigit():
print "digits found"
else:
print "alphas found"

Again ...
False

If you were desperate to avoid regexes, this works:
max([(d in string1) for d in "0123456789"])
True

... but it's not exactly legible.

How about:
True
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top