Home

crybaby wrote:
> import re
>
> s1 =' 25000 '
> s2 = ' 5.5910 '
>
> mypat = re.compile('[0-9]*(\.[0-9]*|$)')
> rate= mypat.search(s1)
> print rate.group()
>
> rate=mypat.search(s2)
> print rate.group()
> rate = mypat.search(s1)
> price = float(rate.group())
> print price
>
> I get an error when it hits the whole number, that is in this format:
> s1 =' 25000 '
> For whole number s2, mypat catching empty string. I want it to give
> me 25000.
> I am getting this error:
>
> price = float(rate.group())
> ValueError: empty string for float()
>
> Anyone knows, how I can get 25000 out of s2 = ' 5.5910 '
> using regex pattern, mypat = re.compile('[0-9]*(\.[0-9]*|$)'). mypat
> works fine for real numbers, but doesn't work for whole numbers.
>
> thanks
>

Try this:
>>> import re
>>> s1 =' 25000 '
>>> s2 = ' 5.5910 '
>>> num_pat = re.compile(r'([0-9]+(\.[0-9]+)?)')
>>> num_pat.search(s1).group(1)
'25000'
>>> num_pat.search(s2).group(1)
'5.5910'

Ian

previous
next

Re: gui toolkits: the real story? (Tkinter, PyGTK, etc.)
Re: 2D arrays
Re: How to store the data of a large map<string, int>?
How do I get the value out of a DOM Element
Re: Static const non-integrals in-class initialization
Mimo Wszystko
Niechciane i Zapomniane
Dzieci Niczyje
Mam Marzenie
Nasze Dzieci