如何在大型文本文件中的两个唯一单词之间提取信息 [英] How to extract information between two unique words in a large text file

查看:114
本文介绍了如何在大型文本文件中的两个唯一单词之间提取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大约有150个文本文件,其中填充了字符信息.每个文件包含两个唯一词()alpha和bravo,我想提取这些唯一词之间的文本并将其写入不同的文件.

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.

手动地,我可以按住CTRL + F键输入两个单词,然后在两个单词之间复制文本,我只是想知道如何使用程序(最好是Python)处理许多文件.

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.

推荐答案

您可以为此使用正则表达式

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

我的test.txt文件

My test.txt file

alpha here is my line
yipee
bravo

现在使用打开来读取文件,然后应用regular expressions

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

这篇关于如何在大型文本文件中的两个唯一单词之间提取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆