[BangPypers] Question on Pattern Matching and Regular Expression

Dhruv Baldawa dhruvbaldawa at gmail.com
Mon Jan 7 19:57:25 CET 2013


regex = """#
#(?P<city>[a-zA-Z]+)
#
Population = (?P<population>\d+)cr
Temperature = (?P<temperature>\d+) deg cel
Area = (?P<area>\d+) sqft"""

regex = re.compile(regex, flags=re.MULTILINE)

print regex.findall(rstr)
# >>> [('Delhi', '7', '10', '13000'), ('Mumbai', '10', '30', '132000')]

for x in regex.finditer(rstr):
    group = x.groupdict()
    data[group['city']] = {'area': int(group['area']),
'population': int(group['population']), 'temperature':
int(group['temperature'])}


In [14]: data
Out[14]:
{'Delhi': {'area': 13000, 'population': 7, 'temperature': 10},
 'Mumbai': {'area': 132000, 'population': 10, 'temperature': 30}}

--
Dhruv Baldawa
(http://www.dhruvb.com)


On Tue, Jan 8, 2013 at 12:02 AM, Rahul R <rahul8590 at gmail.com> wrote:

> Hey David,
>
> Assuming , its not a continuous stream of data being written to file all
> the time. you could do something like this
> https://gist.github.com/4477268 .
> you can enhance it and make it more pythonic. :)
>
> Thanks,
> Rahul
>
>
>
>
>
> On Mon, Jan 7, 2013 at 5:02 PM, davidsnt <davidsnt at gmail.com> wrote:
>
> > No this file changes very often, like once in 5 minutes and the values
> are
> > updated, also there is no way I can move it to a database.
> >
> > Regards,
> > Davidsanthosh L
> >
> >
> > On Mon, Jan 7, 2013 at 4:55 PM, Amit Sethi <amit.pureenergy at gmail.com
> > >wrote:
> >
> > > On Mon, Jan 7, 2013 at 3:52 PM, davidsnt <davidsnt at gmail.com> wrote:
> > > > Gora,
> > > >
> > > > Can you help me with few links that you have handy to which I can
> refer
> > > to
> > > > build a parser instead of RE
> > > Can you elaborate on the idea of "build a parser" , in any case you
> > > will have to use regex.
> > >
> > > >  I also need this app to be
> > > >able to do a search in the file based on the title.
> > > What kind of file are you are working with , How often does it change?
> > >  It might be good move the data to a database in case file does not
> > > change very often .
> > >
> > > --
> > > A-M-I-T S|S
> > > _______________________________________________
> > > BangPypers mailing list
> > > BangPypers at python.org
> > > http://mail.python.org/mailman/listinfo/bangpypers
> > >
> > _______________________________________________
> > BangPypers mailing list
> > BangPypers at python.org
> > http://mail.python.org/mailman/listinfo/bangpypers
> >
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list