Hi,
i need a script, that deletes all the lines between two certain words in a text file. the two words appear several times in the text file.
i have a code, that works so far. the problem is, once it looped for the first time through the text file, the end_word appears before the new start_word. so i have to tell the program, to start searching for the end_word, after the start_word.
here is the code. i hope that makes it more understandable:
i need a script, that deletes all the lines between two certain words in a text file. the two words appear several times in the text file.
i have a code, that works so far. the problem is, once it looped for the first time through the text file, the end_word appears before the new start_word. so i have to tell the program, to start searching for the end_word, after the start_word.
here is the code. i hope that makes it more understandable:
Code:
orig_file = open ("*Strata1CombinedAll.txt", "r")
lines = orig_file.readlines()
import fnmatch
def find(seq, pattern):
pattern = pattern.lower()
for i, n in enumerate(seq):
if fnmatch.fnmatch(n.lower(), pattern):
return i
return -1
def index(seq, pattern):
result = find(seq, pattern)
if result == -1:
raise ValueError
return result
try:
for item in lines:
begin_line = index(lines, "*start_word*")
end_line = index(lines, "*end_word*")
del lines[begin_line:end_line]
except:
new_text_file = open ("*test604", "w")
new_text_file.writelines(lines)
new_text_file.close()
Comment