need help extracting data from a text file
Kent Johnson
kent37 at tds.net
Mon Nov 7 14:37:50 EST 2005
nephish at xit.net wrote:
> Hey there,
> i have a text file with a bunch of values scattered throughout it.
> i am needing to pull out a value that is in parenthesis right after a
> certain word,
> like the first time the word 'foo' is found, retrieve the values in the
> next set of parenthesis (bar) and it would return 'bar'
>
> i think i can use re to do this, but is there some easier way?
It's pretty easy with an re:
>>> import re
>>> fooRe = re.compile(r'foo.*?\((.*?)\)')
>>> fooRe.search('foo(bar)').group(1)
'bar'
>>> fooRe.search('This is a foo bar baz blah blah (bar)').group(1)
'bar'
Kent
More information about the Python-list
mailing list