0

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)
StackGuru
  • 471
  • 1
  • 9
  • 25
  • You could do this very easily with Regular-Expression, but this smells a lot like homework to me. Could you provide the code you've tried out yourself already? Where are you stuck? – Hampus Larsson Apr 14 '20 at 20:34
  • @HampusLarsson I could only try for replacing, pasted code above. I'm not that expert in reg-ex or file-handling()by line numbers, seek etc). Could you please help me for rest of the scenarios ? – StackGuru Apr 14 '20 at 21:55
  • @HampusLarsson If i provide regex as ".*<\/fruits>" , it covers all the tags. How do match it for followed by corresponding not of other tag. Since, it's a file how do you apply it for multi-lines ? – StackGuru Apr 15 '20 at 04:01
  • @HampusLarsson any help ? – StackGuru Apr 15 '20 at 19:28

0 Answers0