sample_file.txt
line1
line2
Apple
line4
line5
line6
line7
line8
line9
Orange
line11
line12
line13
line14
line15
some_text
line17
<fruits name="Pineapple">
line19
line20
line21
</fruits>
line23
<fruits name="Banana">
line25
line26
line27
</fruits>
line29
Apple
line31
Mango
till
line100
Above is sample file of 100 lines
1) line3 to line 10, Apple followed by Orange should be removed
2) line 16, some_text
should be replaced by Found data
3) Tag should be commented.
4) Tag should be removed.
5) Apple followed by Mango shouldn't be altered.
how can we achieve this in python?
using reg-ex
or seek
?
output_file.txt
line1
line2
line11
line12
line13
line14
line15
Found data
line17
<!--<fruits name="Pineapple">
line19
line20
line21
</fruits>-->
line23
line29
Apple
line31
Mango
till
line100
Code:
import re
with open('sample_file.txt', 'r') as file:
filedata = file.read()
filedata = filedata.replace("some_text", "Found data")
with open('output_file.txt', 'w') as file:
file.write(filedata)