this is my first meeting with Python :) I have question -> my code and question bellow:
I am trying to divide input file to 3 files (Program info/Program core/Toollist)
how can i tell I want to "continue with looping from this found string" and write to second list/file or how can I mark all lines between two strings to append it in list and write do file after. Thanks a lot guy. I wish you merry christmas and will be happy from your help
import os
filename = "D327971_fc1.i" # current program name
file = open(filename, 'r') # read current program
if os.stat(filename).st_size == 0: # check if size of file is null
print('File is empty')
file.close()
else:
read = file.readlines()
programdef = []
toollist = []
core = []
line_num = -1
for line in read:
start_line_point = "Zacatek" in line
end_line_point = "Konec" in line
toollist_point = "nastroj" in line
programdef.append(line.strip())
if start_line_point: break
core.append(line.strip())
if end_line_point:
toollist.append(line.strip())
with open('0progdef.txt', 'w') as f:
f.write(str(programdef))
with open('1core.txt', 'w') as f:
f.write(str(core))
with open('2toollist.txt', 'w') as f:
f.write(str(toollist))
Divide input file to 3 lists with marking lines by find string and esport this lists to 3 files after it.
If I understood correctly, what you want is to split the file into 3 different files: the first one includes all lines before "Zacatek", the second one includes all lines between "Zacatek" and "Konec" and the third one includes all line between "Konec" and "nastroj".
You could change your for
loop to something like:
keywords = {0:'Zacatek', 1:'Konec', 2:'nastroj'}
index = 0
for line in read:
if index == 3:
break
if keywords[index] in line:
index += 1
continue
if index == 0:
programdef.append(line.strip())
elif index == 1:
core.append(line.strip())
elif index == 2 :
toollist.append(line.strip())
This will create the three expected files containing lists
of the lines in the original file.