On Aug 24, 5:33 pm, richie05 bal <richie8...@gmail.com> wrote: > i am starting to learn python and I am stuck with query I want to > generate with python > File looks something like this > TRACE: AddNewBookD {bookId 20, noofBooks 6576, authorId 41, > publishingCompanyId 7} > TRACE: AddNewBookD {bookId 21, noofBooks 6577, authorId 42, > publishingCompanyId 8} > > I want to first search for AddNewBookD > if found > store bookId, noofBooks, authorId and publishingCompanyId > > I know how to search for only AddNewBookD or find the pattern bookId > 20, noofBooks 6576, authorId 41, publishingCompanyId 7 but I don't > know how search one based on another.
Using a regular expression I would perform a match against each line. If the match fails, it will return None. If the match succeeds it returns a match object with which you can extract the values >>> import re >>> pattern = re.compile(r'TRACE: AddNewBookD \{bookId (\d+), noofBooks (\d+), >>> authorId (\d+), publishingCompanyId (\d+)\}\s*') >>> s = '''TRACE: AddNewBookD {bookId 20, noofBooks 6576, authorId 41, >>> publishingCompanyId 7} ''' >>> pattern.match(s) <_sre.SRE_Match object at 0xa362f40> # If the match failed this would be None >>> m = pattern.match(s) >>> m.groups() ('20', '6576', '41', '7') >>> So your code to store the result would be inside an if m: block HTH, Alex -- http://mail.python.org/mailman/listinfo/python-list