O'Reilly logo

XML Processing with Python by Sean McGrath

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

7.6. A Pattern-Matching Dry Run

Before we delve into regular expressions, it would be useful to have a functioning xgrep that can search for plain vanilla strings in the files specified on the command line. Doing this much enables us test the end-to-end operation of xgrep. We are nearly there; all we need is to beef up the XMLGrep function a little bit. The modified XMLGrep function is shown below.

 CD-ROM reference=7042.txt import string def XMLGrep(filename,pattern): # for the moment, search the specified file, treating # the pattern argument as a # straight string i.e., not as a regular expression f = open (filename,"r") for l in f.readlines(): pos = string.find (l,pattern) if pos != -1: # print out the match, using square # brackets to show ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required