简体   繁体   中英

From text file read and if there is a sentences define on the list

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> .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM