Python3 gets the content method between two keywords between a large chunk of text

  • 2020-12-09 00:57:00
  • OfStack

Use re or string. find. Here is the re code


import re
# The text is located TXT file 
file = '123.txt'

# The keyword 1,2( Change the content between quotes )
w1 = '123'
w2 = '456'

f = open(file,'r')
buff = f.read()

# Clear the newline character , Please cancel the 1 Comment line 

#buff = buff.replace('\n','')
pat = re.compile(w1+'(.*?)'+w2,re.S)
result = pat.findall(buff)
print(result)

Related articles: