Extract Values between two strings in a text file using python

后端 未结 7 1413
闹比i
闹比i 2020-11-29 08:27

Lets say I have a Text file with the below content

fdsjhgjhg
fdshkjhk
Start
Good Morning
Hello World
End
dashjkhjk
dsfjkhk

Now I need to wr

7条回答
  •  天命终不由人
    2020-11-29 09:00

    If the text files aren't necessarily large, you can get the whole content of the file then use regular expressions:

    import re
    with open('data.txt') as myfile:
        content = myfile.read()
    
    text = re.search(r'Start\n.*?End', content, re.DOTALL).group()
    with open("result.txt", "w") as myfile2:
        myfile2.write(text)
    

提交回复
热议问题