在python中提取文件中的所有字符串

时间:2018-08-05 06:48:09

标签: python python-3.x python-2.7

这些是文件的内容:-

org.mule.api.processor.LoggerMessageProcessor: Process Started MessageID=1AMULE
One
Two
Three
org.mule.api.processor.LoggerMessageProcessor: Process Completd MessageID=1AMULE
org.mule.api.processor.LoggerMessageProcessor: Process Started MessageID=2BMULE
org.mule.api.processor.LoggerMessageProcessor: Process Completd MessageID=2BMULE

我在这里要提取字符串1A,1B,即在python中的org.mule.api.processor.LoggerMessageProcessor: Process Started MessageID=之后和MULE之前。

1 个答案:

答案 0 :(得分:0)

您可以在str.split上使用"="

例如:

with open(filename) as infile:
    for line in infile:
        if "=" in line:
            print(line.split("=")[1].strip())

输出:

1AMULE
1AMULE
2BMULE
2BMULE