469,327 Members | 1,219 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

A way of checking if a string contains a number

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?



Dec 12 '07 #1
4 16001
Hamish wrote:
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
Dec 12 '07 #2
:
[...] 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:
>>string1 = "ABC 11"
int(string1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): ABC 11
Or use string methods:

if string1.isdigit():
print "digits found"
else:
print "alphas found"
Again ...
>>string1.isdigit()
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.
Dec 12 '07 #3
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...).
Dec 12 '07 #4
On Dec 12, 10:59 pm, "Zero Piraeus" <sche...@gmail.comwrote:
:
[...] 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:
>>string1 = "ABC 11"
>>int(string1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): ABC 11
Or use string methods:
if string1.isdigit():
print "digits found"
else:
print "alphas found"

Again ...
>>string1.isdigit()
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:
>>string1 = "ABC 11"
any(c.isdigit() for c in string1)
True
Dec 13 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tom Esker | last post: by
4 posts views Thread by Suresh Jeevanandam | last post: by
4 posts views Thread by Patient Guy | last post: by
42 posts views Thread by =?Utf-8?B?UGxheWE=?= | last post: by
13 posts views Thread by nishit.gupta | last post: by
11 posts views Thread by Bryan Crouse | last post: by
7 posts views Thread by =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post: by
21 posts views Thread by ningxin | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.