I need to filter contents of my text file

2024/10/5 21:17:03

I have a text file that I want to loop through, slice some contents, and store in a separate list. The text file contains:

blu sre
before we start
start the process
blah blah
blah blha
end the process
blah böah
start the process
blah blah
blah blha
end the process
start the process
blah blah
end the process
blah

I want to catch all text between 'start the process' and 'end the process' and store in a new list so that each line of the list will contain every text between start the process and end the process. The desired new list should be like this

list[0] start the process  
blah blah  
blah blha  
end the process   
list[1] start the process  
blah blah  
blah blha  
end the process   
list[2] start the process  
blah blah  
end the process  

Here is the code I prepared. I don't why its not giving me the required result.

list = []
text = 'start the process'
text2 = 'end the process'
for pattern in range (len(file)):if text in file[pattern]:x = patternif text2 in file[pattern]:y = patternlist[i]= file[x:y]i = i+1
Answer

Regex is built for that.

import repart = re.compile(r"start the process(.*?)end the process", flags=re.DOTALL)
wirh open("my_file.text", "r") as file:data = file.read()results = list(part.findall(data))

EDIT update code based on @Xosrov comment

https://en.xdnf.cn/q/119172.html

Related Q&A

How to remove a number from a list that has a range between two numbers? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

How do I solve an attribute error?

So like I said before my code (Or my current project that I am working on) is riddled with errors. So far I have at least solved a dozen errors or more and honestly I just give up. I mean God knows how…

Two variables in Django URL

I want a URL something like this:/(category)/(post-slug)On the link this is what I have:{% url blog.category blog.slug %}and for the url.py:url(r^(I DON"T KNOW WHAT TO PUT ON THIS PART TO GET THE …

Python 3.2 Replace all words in a text document that are a certain length?

I need to replace all words in a text document that are of length 4 with a different word. For example, if a text document contained the phrase "I like to eat very hot soup" the words "l…

Python split list at zeros [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Remove leading and trailing zeros from multidimensional list in Python if I have a list such as:my_list = [[1,2,0,1], [1,0…

Creating Dynamic Text Using the blit method [duplicate]

This question already has answers here:How to display text with font and color using pygame?(7 answers)Closed last year.Im creating a basic game where a black square moves around the screen. There are…

Regex to match special list items

I have weird list of items and lists like this with | as a delimiters and [[ ]] as a parenthesis. It looks like this:| item1 | item2 | item3 | Ulist1[[ | item4 | item5 | Ulist2[[ | item6 | item7 ]] | i…

How to choose the best model dynamically using python

Here is my code im building 6 models and i am getting accuracy in that, how do i choose that dynamically which accuracy is greater and i want to execute only that model which as highest accuracy."…

How do you access specific elements from the nested lists

I am trying to access elements from the nested lists. For example, file = [[“Name”,”Age”,”Medal”,”Location”],[“Jack”,”31”,”Gold”,”China”],[“Jim”,”29”,”Silver”,”US”]]This data c…

Why does BLOCKCHAIN.COM API only return recipient BASE58 addresses and omits BECH32s?

Following this post, I am trying to access all transactions within the #630873 block in the bitcoin blockchain.import requestsr = requests.get(https://blockchain.info/block-height/630873?format=json) …