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

こ雲淡風輕ζ 提交于 2019-12-17 09:44:06

问题


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.


回答1:


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 '
>>>



回答2:


a = 'alpha'
b = 'bravo'
text = 'from alpha all the way to bravo and beyond.'

text.split(a)[-1].split(b)[0]
# ' all the way to '



回答3:


str.find and its sibling rfind have start and end args.

alpha = 'qawsed'
bravo = 'azsxdc'
startpos = text.find(alpha) + len(alpha)
endpos = text.find(bravo, startpos)
do_something_with(text[startpos:endpos]

This is the fastest way if the contained text is short and near the front.

If the contained text is relatively large, use:

startpos = text.find(alpha) + len(alpha)
endpos = text.rfind(bravo)

If the contained text is short and near the end, use:

endpos = text.rfind(bravo)
startpos = text.rfind(alpha, 0, endpos - len(alpha)) + len(alpha)

The first method is in any case better than the naive method of starting the second search from the start of the text; use it if your contained text has no dominant pattern.




回答4:


Instead of using regular expression use Python string.find method.

>>>> unique_word_a = 'alpha'
>>>> unique_word_b = 'bravo'
>>>> s = 'blah blah alpha i am a good boy bravo blah blah'
>>>> your_string = s[s.find(unique_word_a)+len(unique_word_a):s.find(unique_word_b)].strip()
i am a good boy


来源:https://stackoverflow.com/questions/9222106/how-to-extract-information-between-two-unique-words-in-a-large-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!