python - Evaluating the next line in a For Loop while in the current iteration -
here trying do: trying solve issue has wrapping in text file.
i want open txt file, read line , if line contains want contain, check next line see if not contain in first line. if not, add line first line.
import re stuff = open("my file") line in stuff: if re.search("from ", line): first = line print first if re.search('from ', handle.next()): continue else: first = first + handle.next() else: continue
i have looked quite few things , cannot seem find answer. please help!
i try this, invalid triples of "from " , not elegant @ all.
lines = open("file", 'r').readlines() lines2 = open("file2", 'w') counter_list=[] last_from = 0 counter, line in enumerate(lines): if "from " in line , counter != last_from +1: last_from = counter current_count = counter if current_count+1 == counter: if "from " in line: counter_list.append(current_count+1) counter, line in enumerate(lines): if counter in counter_list: lines2.write(line) else: lines2.write(line, '\n')
than can check lines2 if helped.
you revert order of lines, check in next line not in previous. solve problem in 1 loop.
Comments
Post a Comment