i have got 2 files ,one with some keywords and other with plain text i.e myfile.txt , i need to open a myfile.txt and extract the specific text starting with each keyword (mentioned in keyword file) and ends with "!" example:
keyword file :
vrf-a
vrf-b
myfile.txt:
hello
how are you
!
x vrf-a
number 1
!
hi
howa are you
!
x vrf-b
number 2
!
Output should be:
x vrf-a
number 1
!
x vrf-b
number 2
I tried the below code:
import re
crazy = open("keyword.txt","r+")
lines = crazy.readlines()
for word in lines:
#print(word)
with open('mytext.txt', 'r') as fh:
result = re.findall(r'word[^!]+', fh.read(), re.M)
print(result)
fh.close()
crazy.close()
output getting as : [] [] means no match