[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Can I specify regex group to return float or int instead of string?

Jeremy

2/25/2010 3:49:00 PM

I have a regular expression that searches for some numbers and puts
them into a dictionary, i.e.

'(?P<integer>\d+)\s+(?P<float>\d+\.\d+)'

Is it possible to have the results of the matches returned as int or
float objects instead of strings?

Thanks,
Jeremy
4 Answers

Steven D'Aprano

2/25/2010 4:41:00 PM

0

On Thu, 25 Feb 2010 07:48:44 -0800, Jeremy wrote:

> I have a regular expression that searches for some numbers and puts them
> into a dictionary, i.e.
>
> '(?P<integer>\d+)\s+(?P<float>\d+\.\d+)'
>
> Is it possible to have the results of the matches returned as int or
> float objects instead of strings?

No. Just convert the match with int() or float() before storing it in the
dictionary. That is, instead of:

d[key] = match

use

d[key] = float(match)

or similar.

--
Steven

Jeremy

2/25/2010 5:00:00 PM

0

On Feb 25, 9:41 am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
> On Thu, 25 Feb 2010 07:48:44 -0800, Jeremy wrote:
> > I have a regular expression that searches for some numbers and puts them
> > into a dictionary, i.e.
>
> > '(?P<integer>\d+)\s+(?P<float>\d+\.\d+)'
>
> > Is it possible to have the results of the matches returned as int or
> > float objects instead of strings?
>
> No. Just convert the match with int() or float() before storing it in the
> dictionary. That is, instead of:
>
> d[key] = match
>
> use
>
> d[key] = float(match)
>
> or similar.

I was afraid that was the only option. Oh well, thought I'd ask
anyway. Thanks for your help.
Jeremy

Steven D'Aprano

2/25/2010 5:21:00 PM

0

On Thu, 25 Feb 2010 09:00:07 -0800, Jeremy wrote:

> On Feb 25, 9:41 am, Steven D'Aprano <st...@REMOVE-THIS-
> cybersource.com.au> wrote:
>> On Thu, 25 Feb 2010 07:48:44 -0800, Jeremy wrote:
>> > I have a regular expression that searches for some numbers and puts
>> > them into a dictionary, i.e.
>>
>> > '(?P<integer>\d+)\s+(?P<float>\d+\.\d+)'
>>
>> > Is it possible to have the results of the matches returned as int or
>> > float objects instead of strings?
>>
>> No. Just convert the match with int() or float() before storing it in
>> the dictionary. That is, instead of:
>>
>> d[key] = match
>>
>> use
>>
>> d[key] = float(match)
>>
>> or similar.
>
> I was afraid that was the only option. Oh well, thought I'd ask anyway.


Why "afraid"? What's the problem with calling int or float on the match?




--
Steven

nn

2/25/2010 8:09:00 PM

0

On Feb 25, 12:20 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
> On Thu, 25 Feb 2010 09:00:07 -0800, Jeremy wrote:
> > On Feb 25, 9:41 am, Steven D'Aprano <st...@REMOVE-THIS-
> > cybersource.com.au> wrote:
> >> On Thu, 25 Feb 2010 07:48:44 -0800, Jeremy wrote:
> >> > I have a regular expression that searches for some numbers and puts
> >> > them into a dictionary, i.e.
>
> >> > '(?P<integer>\d+)\s+(?P<float>\d+\.\d+)'
>
> >> > Is it possible to have the results of the matches returned as int or
> >> > float objects instead of strings?
>
> >> No. Just convert the match with int() or float() before storing it in
> >> the dictionary. That is, instead of:
>
> >> d[key] = match
>
> >> use
>
> >> d[key] = float(match)
>
> >> or similar.
>
> > I was afraid that was the only option.  Oh well, thought I'd ask anyway.
>
> Why "afraid"? What's the problem with calling int or float on the match?
>
> --
> Steven

He must not be Dutch

:-) SCNR