Searching a window of text in Python
Revision as of 19:23, 11 March 2014 by Michael Murtaugh (talk | contribs) (Created page with "<source lang="python"> s = "state" f = open("machiavelli_pg1232.txt") lines = f.readlines() numlines = len(lines) # PASS 1: Search the text in "windows" of 5 lines # If the k...")
s = "state"
f = open("machiavelli_pg1232.txt")
lines = f.readlines()
numlines = len(lines)
# PASS 1: Search the text in "windows" of 5 lines
# If the keywords are both there (this condition you can adapt)
# MARK the line numbers of the window (by adding the line numbers to the "hits" set)
hits = set()
for n in range(0, numlines-5):
block = ""
for line in lines[n:n+5]:
block += line
if "state" in block and "work" in block:
for n in range(n, n+5):
hits.add(n)
# PASS 2: Display the linenumbers that are in hits
lastline = 0
for n in range(0, numlines):
if n in hits:
line = lines[n].strip()
if n > lastline + 1:
print "========================="
# print n, line
print line.replace("state", "STATE").replace("work", "WORK")
lastline = n
# for line in f:
# if "state" in line and "work" in line:
# print line