Reputation: 105
I have file like below (temp1 file):
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
I have written below code it will search start line and end line and prints between lines including start and end lines.
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__
Output I am getting:
10 Pens I have in Packet1
20 Books I have in Packet1
30 Red pens I have in Packet1
End here
I need help to print lines to file temp2 from above one line from start line to end line . Below is the expected output to file temp2.
Basket1
10 Pens I have in Packet1
20 Books I have in Packet1
30 Red pens I have in Packet1
End here
Upvotes: 1
Views: 572
Reputation: 1947
Since you need Basket1
to be printed, your start_line
has to be Basket1
, and as after first line you need Pens I have
i have used it as '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__
Upvotes: 0
Reputation: 33714
You can use a regex to search your string, to use it for reading from and writing to file, you can do:
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)
Example:
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
Upvotes: 1