Re: A way of checking if a string contains a number

2007-12-12 Thread MRAB
On Dec 12, 10:59 pm, "Zero Piraeus" <[EMAIL PROTECTED]> wrote: > : > > > [...] 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

Re: A way of checking if a string contains a number

2007-12-12 Thread 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...). -- http://mail.py

Re: A way of checking if a string contains a number

2007-12-12 Thread Hamish
Thanks worked Perfectly On Dec 13, 9:32 am, [EMAIL PROTECTED] wrote: > On Dec 12, 3:10 pm, Hamish <[EMAIL PROTECTED]> 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 re

Re: A way of checking if a string contains a number

2007-12-12 Thread 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: >>> string1 = "ABC 11" >>> int(string1) Traceback (most recent c

Re: A way of checking if a string contains a number

2007-12-12 Thread Larry Bates
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 sp

Re: A way of checking if a string contains a number

2007-12-12 Thread jason . s . trowbridge
On Dec 12, 3:10 pm, Hamish <[EMAIL PROTECTED]> 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

A way of checking if a string contains a number

2007-12-12 Thread 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

Re: checking if a string contains a number

2005-12-20 Thread Steven D'Aprano
On Tue, 20 Dec 2005 15:55:37 +0100, egbert wrote: > On Tue, Dec 20, 2005 at 07:16:46PM +0530, Suresh Jeevanandam wrote: >> s1 = '12e3' >> s2 = 'junk' >> Is there any other function which would return True for s1 and False >> for s2. >> > > isinstance(12e3, (int, float)) That won

Re: checking if a string contains a number

2005-12-20 Thread Steven D'Aprano
On Tue, 20 Dec 2005 19:16:46 +0530, Suresh Jeevanandam wrote: > Hi, > I have a string like, > s1 = '12e3' > s2 = 'junk' > > Now before converting these values to float, I want to check if they > are valid numbers. Just try converting them: float_list = [] for s in strin

Re: checking if a string contains a number

2005-12-20 Thread egbert
On Tue, Dec 20, 2005 at 07:16:46PM +0530, Suresh Jeevanandam wrote: > s1 = '12e3' > s2 = 'junk' > Is there any other function which would return True for s1 and False > for s2. > isinstance(12e3, (int, float)) -- Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020

Re: checking if a string contains a number

2005-12-20 Thread Larry Bates
You should probably work through the tutorials. Use a try block: try: x=float(s) except ValueError: print 'Non-numeric value %s found' % s -Larry Bates Suresh Jeevanandam wrote: > Hi, > I have a string like, > s1 = '12e3' > s2 = 'junk' > > Now before converting these values

Re: checking if a string contains a number

2005-12-20 Thread Tim Williams (gmail)
On 20/12/05, Suresh Jeevanandam <[EMAIL PROTECTED]> wrote: Hi,I have a string like,s1 = '12e3's2 = 'junk'Now before converting these values to float, I want to check if theyare valid numbers.s1.isdigit returns False. Is there any other function which

checking if a string contains a number

2005-12-20 Thread Suresh Jeevanandam
Hi, I have a string like, s1 = '12e3' s2 = 'junk' Now before converting these values to float, I want to check if they are valid numbers. s1.isdigit returns False. Is there any other function which would return True for s1 and False for s2. Than