0
text=open('tt.txt','r')
txt=text.read()
text.close()
print (txt)

text1=open('tt.txt','w')
lis=['elephants are the biggest animal.', 'sunset is the 
     time of day when our siy meets the outer space solar 
      winds.']
For i in range(len(lis)):
         If text.__contains__(lis[i]):
                text2=text.replace(lis[i],txts)
                text1.write(text2)
text1.close()
Print(text2)

From text file read and if there is a sentences define by on list then replace it with <b> the list item that found </b>; for example if I have elephants are the biggest animal. in a text file it will replaced by <b>elephants are the biggest animal.</b>.

Armali
  • 18,255
  • 14
  • 57
  • 171
haptomee
  • 59
  • 10
  • what is the problem? – ilamaaa Jan 21 '19 at 19:00
  • The problem is the above code it didn't do what I want .......and am new for python – haptomee Jan 21 '19 at 19:08
  • I mean did you get an error? if so what was it? also it sounds like you want to replace a bit of text in a file if that bit of text appears in a list, with the text from the list... i am not sure what that accomplishes. – ilamaaa Jan 21 '19 at 19:15
  • for example if I have "elephants are the biggest animal." in a text file it will replaced by "elephants are the biggest animal." both are same?? – Mehrdad Dowlatabadi Jan 21 '19 at 19:17
  • No no no .....the second one is bold because I add a bold tag and it is not showing for you and it just make it bold sorry for that but I mean like I will add a simple html tag and I put that text right there that is what I mean – haptomee Jan 21 '19 at 19:18

1 Answers1

1
f = open("tt.txt","r")
text = f.read()

lis=['elephants are the biggest animal.', 'sunset is the 
     time of day when our siy meets the outer space solar 
      winds.']

for i in lis:
    if i in text:
        text.replace(i, "<b>" + i + "<\b>")

f = open("out.txt","w")
f.write(text)

This should do the trick

ilamaaa
  • 421
  • 2
  • 8
  • Ok nice but it must be done on the original file tt.txt – haptomee Jan 21 '19 at 19:31
  • You should also be putting some effort into solving your issue. Link to thread on read and write to the same file https://stackoverflow.com/questions/14271216/beginner-python-reading-and-writing-to-the-same-file – ilamaaa Jan 21 '19 at 22:53