Python Program to Match Lines in File -


i trying write program in python reads through .txt file , checks

if line[i] == line [i+12]:     print line[i] 

so far, have:

f=open('file.txt', "r")  count=0 line in f:     while count < 1000:         print(count)         if line(count) == line(count+12):             print (line(count))         count+=1 

my output 1000 zeros.

any appreciated.

if you're trying print lines character same 1 12 characters later:

for line in f:     i, c in enumerate(line[:-12]):         if c == line[i+12]:             print(line)             break 

if you're trying print lines same 1 12 lines later, it's simpler in need 1 loop, more complicated in have iterator of lines, not list, can't randomly access this.

one simple fix that, if file small enough, make list:

lines = list(f) i, line in enumerate(lines[:-12]):     if line == lines[i+12]:         print(line) 

a better fix use itertools create shifted copy of iterator (which work either problem):

lines, lines12 = itertools.tee(f, 2) lines12 = islice(lines12, 12, none) line, line12 in zip(lines, lines12):     if line == line12:         print(line) 

(if you're using python 2.x, want use itertools.izip here, not zip. , want take parentheses off prints.)


either way, have no idea you're trying count. if want keep count of matches along way, move count = 0 outside loop, , count += 1 each time print something, don't try use index lines or that.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -