Python - Parsing multiple lines of text from file -
i stuck on problem working on.
it involves opening file reading line , comparing next 2 lines in file it. move down 1 line, ie second line in file , compare next 2 lines again.
i having hard time creating loop go through these steps.
so far have done
write_file = input_file[:-3] + "bak" read_file = input_file open(write_file, "w") write: open(read_file,"r") read: line in read: line1 = line
thank you.
something like:
tri_lines = [lines[i:i+3] in range(0,len(lines),3)] #then can iterate through: triplet in tri_lines: # code here, compare tri_lines[0] tri_lines[1] etc.
i'll leave task of getting lines of file list.
note you'll need simple validation handle cases file not have number of lines divisible 3.
you can
for triplet in [lines[i:i+3] in range(0,len(lines),3)]: if triplet[0] == triplet[1]: #do if triplet[0] == triplet[2]: #do
though might not clear
Comments
Post a Comment