2017-05-27 165 views
0

我有文件類似下面(temp1目錄文件):搜索字符串,並打印從一個線以上在Python另一個搜索字符串

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 
+0

請註明你所面對 – JkShaw

+0

喜JkShaw問題/問題,我需要打印到文件,從上面開始行到結束行之一。現在我只能打印從開始行到結束行 – kitty

+0

你已經打印行到'temp2' –

回答

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 
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__ 
+0

我得到了temp.close() NameError:名稱'temp'沒有定義 – kitty

+0

實際上它會發生,當模式將不會被發現。不過,更新解決方案以處理這種情況,請參閱更新的解決方案。 – JkShaw

相關問題