Skip to content Skip to sidebar Skip to footer

Searching A File

This is in reference to a question I posted yesterday Searching a file in 3 different ways I just require help now on two things, searching a file and and printing the line a searc

Solution 1:

for the first part

for line in open("file"):
    line=line.rstrip()
    if"search" in line:
        f=1if f: print line

for the second part

context=3
search="myword"
f=open("file")
d={}
for n,line inenumerate(f):
    d[n%context]=line.rstrip()
    if search in line:
        for i inrange(n+1,n+1+context):
            print d[i%context]
        for i inrange(1,context):
            print f.next().rstrip()
f.close()

Post a Comment for "Searching A File"