Python从txt文件中提取单词

时间:2018-01-05 10:56:49

标签: python-2.7

是否可以搜索一系列单词&提取下一个单词。例如,在txt文件中搜索单词' Test' &安培;然后在它之后直接返回这个词?

Test.txt
This is a test to test the function of the python code in the test environ_ment

我希望得到结果: -

to, the, environ_ment

1 个答案:

答案 0 :(得分:0)

您可以使用正则表达式:

Response from export task at 2018-01-05T10:57:42.441844 :\n{'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx', 'HTTPHeaders': {'x-amzn-requestid': 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx', 'date': 'Fri, 05 Jan 2018 10:57:41 GMT', 'content-length': '49', 'content-type': 'application/x-amz-json-1.1'}}, u'taskId': u'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx'}

START RequestId: xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx Version: $LATEST
END RequestId: xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
REPORT RequestId: xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx   Duration: 1418.13 ms    Billed Duration: 1500 ms    Memory Size: 128 MB Max Memory Used: 36 MB

正则表达式与“test”匹配,后面是空格(import re txt = "This is a test to test the function of the python code in the test environ_ment" print re.findall("test\s+(\S+)", txt) # ['to', 'the', 'environ_ment'] )和一系列非空白字符\s+。后者匹配您正在寻找的单词并放入捕获组(带括号)以返回该部分匹配。