This might be very straightforward but I am having bit of trouble while deleting unwanted line in my text file:
subject name="Biology" value="12th"
topic name="system" value="anything"
topic
subject name="mathematics" value="12th"
topic name="system" value="anything"
other topics ="anything" value="anything"
topic
subject name="chem102" value="12th"
topic name="system" value="anything"
topic
subject name="ComputerSc" value="12th"
topic name="system" value="anything"
other topics ="anything" value="anything"
topic
I want to remove topic line from specific clade by matching subject name from list:
list=['Biology','chem102']
desired output:
subject name="Biology" value="12th"
topic name="system" value="anything"
subject name="mathematics" value="12th"
topic name="system" value="anything"
other topics ="anything" value="anything"
topic
subject name="chem102" value="12th"
topic name="system" value="anything"
subject name="ComputerSc" value="12th"
topic name="system" value="anything"
other topics ="anything" value="anything"
topic
Here's what I am trying
import re
IO=open('fileout.txt','w')
IF=open('filein.txt','r')
for line in IF.readlines():
if re.search('subject name="[^\s]+"',line):
for i in list:
if re.search('subject name="{list[i]}"',line):
line=line.replace("topic","")
IO.write(line)
sorry if this question is already answered. Thank you!