Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract information between two unique words in a large text file

I have about 150 text files filled with character information. Each file contains two unique words ()alpha and bravo and i want to extract the text between these unique words and write it to a different file.

Manually i can CTRL+F for the two words and copy the text between, i just want to know how to do this using a program (preferably Python) for many files.

like image 695
user2760 Avatar asked Feb 10 '12 02:02

user2760


People also ask

How do I extract text between two words in Python?

Given a string and two substrings, write a Python program to extract the string between the found two substrings. In this, we get the indices of both the substrings using index(), then a loop is used to iterate within the index to find the required string between them.


1 Answers

You can use regular expressions for that.

>>> st = "alpha here is my text bravo"
>>> import re
>>> re.findall(r'alpha(.*?)bravo',st)
[' here is my text ']

My test.txt file

alpha here is my line
yipee
bravo

Now using open to read the file and than applying regular expressions.

>>> f = open('test.txt','r')
>>> data = f.read()
>>> x = re.findall(r'alpha(.*?)bravo',data,re.DOTALL)
>>> x
[' here is my line\nyipee\n']
>>> "".join(x).replace('\n',' ')
' here is my line yipee '
>>>
like image 70
RanRag Avatar answered Oct 08 '22 12:10

RanRag