搜索字符串并从上面的一行打印到python中的另一个搜索字符串

时间:2017-05-27 04:53:12

标签: python python-2.7

我的文件如下(temp1文件):

Basket1
10 Pens I have in Packet1
20 Books I have in Packet1
30 Red pens I have in Packet1
End here
Basket1
10 apples I have in Packet2
20 Mangos I have in Packet2
30 oranges I have in Packet2.
End here

我写了下面的代码,它将搜索起始行和结束行,并在行之间打印,包括起始行和结束行。

start_line = "Pens I have"
end_line = "End here"
print_lines = False
with open('temp1' , 'r') as f:
    for line in f:
        line = line.strip()
        if (re.search(start_line, line)):
            print_lines = True
        if print_lines:
            temp = open("temp2", 'a')
            sys.stdout = temp
            print line
        if (re.search(end_line, line)):
            print_lines = False
            temp.close()
            sys.stdout = sys.__stdout__

我得到的输出:

10 Pens I have in Packet1
20 Books I have in Packet1
30 Red pens I have in Packet1
End here    

我需要帮助从起始行到结束行从一行以上打印行文件temp2。下面是文件temp2的预期输出。

Basket1
10 Pens I have in Packet1
20 Books I have in Packet1
30 Red pens I have in Packet1
End here

2 个答案:

答案 0 :(得分:0)

您可以使用正则表达式搜索字符串,使用它来读取和写入文件,您可以这样做:

import re

with open('temp1' , 'r') as f1, open('temp2' , 'a') as f2:
    results = re.findall('\w+\n10 Pens I.*?End here', f1.read(), re.DOTALL)
    f2.writelines(results)

示例:

import re

s = '''Basket1
10 Pens I have in Packet1
20 Books I have in Packet1
30 Red pens I have in Packet1
End here
Basket1
10 apples I have in Packet2
20 Mangos I have in Packet2
30 oranges I have in Packet2.
End here'''

# use re.findall if you want to match multiple times
result = re.search('\w+\n10 Pens I.*?End here', s, re.DOTALL)

# only print(result) if using re.findall
print(result.group())

# output:

Basket1
10 Pens I have in Packet1
20 Books I have in Packet1
30 Red pens I have in Packet1
End here

答案 1 :(得分:0)

由于您需要打印Basket1,因此start_line必须为Basket1,并且在第一行之后您需要Pens I have我将其用作'mid_line' ,

import sys
import re

start_line = "Basket1"
mid_line = "Pens I have"
end_line = "End here"
print_lines = False

start_index = None
start_data = None
temp = None

with open('temp1' , 'r') as f:
    for index, line in enumerate(f):
        line = line.strip()

        # Search for start_line, and store it's index and value
        if (re.search(start_line, line)):
            start_data = line
            start_index = index

        # If you find "Pens I have", and it's under start_line then store start_line
        if (re.search(mid_line, line)):
            if start_index + 1 == index:
                temp = open("temp2", 'a')
                sys.stdout = temp
                print start_data
                print_lines = True
        if print_lines:
            temp = open("temp2", 'a')
            sys.stdout = temp
            print line
        if (re.search(end_line, line)):
            print_lines = False
            if temp and hasattr(temp, 'read'):
                temp.close()
            sys.stdout = sys.__stdout__