I am a python beginner and have the following problem:
I have a text file ('demofile.txt') and want to cut out everything between two specific elements ({start} and {end}
) multiple times.
As en example imagine the text file contains:
'AAAA {start} BBBB {end} CCCC {start} DDDD {end} EEEE {start} FFFF {end} GGGG'
The outcome should be:
'AAAA CCCC EEEE GGGG'
First I defined the two elements which work as the cutter
start = '{start}'
end = '{end}'
The I tried to cut out the first part and used this code:
text_start = text.find(start)
text_new = text[0:text_start]
print(text_new)
The outcome is : 'AAAA ', which is what I wanted
For the next part I tried this:
text_start = text.find(end)
text_end = text.find(start, text_start)
text_new = text[text_start+len(end):text_end]
print(text_new)
The outcome is: 'CCCC ' which is again what I was looking for
Now I tried to put everything together and build a loop and failed :-)
text_start = text.find(start)
text_new = text[0:text_start]
text_end = 0
for parts in text.split("{"):
text_start = text.find(end, text_end)
text_end = text.find(start, text_start)
text_new = text_new + text[text_start+len(end):text_end]
print(text_new)
The outcome is: 'AAAA CCCC EEEE GGG {start} BBBB {end} CCCC {start} DDDD {end}...' and lot more of that. Thus the Outcome was okay until "GGG", but one G is missing. And all the stuff afterwards should be deleted. I guess the loop continued somehow and the start of the loop with the split statement is crap. What is the solution here? I would like to understand what went wrong and change the code. Of course I am also interested in a shorter and more elegant way. I am sure what I did is quite terrible ;-) I found something with "regular expressions" but I was not able to get this going as well. Thanks for any idea.
(PS: any idea how I could save everything I cut out in a seperate file?)