[Tutor] Text matching
Kent Johnson
kent37 at tds.net
Thu May 3 15:39:17 CEST 2007
Gardner, Dean wrote:
> Here is a test sample of what I have: This currently correctly
> identifies the start and the end of the changelog entries and I can
> identify the requested item records. What I am struggling with at
> present is to how to create records of the full changelog entries for
> each of the found items.
>
> Again any help greatly appreciated.
>
>
> uid = "000028.txt"
>
>
> file = open("C:\projects\SuiteSpecification\Notes\ChangeLog.txt")
>
> output = file.readlines()
> changeLogList = []
> itemList = []
> for strOut in output:
>
> if "----" in strOut:
> changeLogStart = 1
> itemList.append(strOut)
> if "Reviewed:" in strOut:
> changeLogStart=0
> for item in itemList:
> if uid in item:
> print item
I usually solve problems like this by pulling multiple lines from the
file within the loop. Something like this:
f = open('...ChangeLog.txt')
try:
while True:
line = f.next()
if line.startswith('----'):
line1 = line
line2 = f.next()
line3 = f.next()
line4 = f.next()
# Do something to process the lines
line = f.next()
except StopIteration:
pass
The basic idea is that you can collect multiple lines by calling
f.next() explicitly inside the loop.
Kent
More information about the Tutor
mailing list