A way of checking if a string contains a number

Hamish hamishmacg at gmail.com
Wed Dec 12 17:56:39 EST 2007


Thanks worked Perfectly



On Dec 13, 9:32 am, jason.s.trowbri... at gmail.com wrote:
> On Dec 12, 3:10 pm, Hamish <hamishm... at gmail.com> 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?
>
> 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 -




More information about the Python-list mailing list