从文本文件中读取文本,并在列表中定义是否有句子

时间:2019-01-21 18:56:46

标签: python

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)

从阅读的文本文件中,如果有一个句子由列表定义,则将其替换为<b> the list item that found </b>;例如,如果我在文本文件中有elephants are the biggest animal.,它将替换为<b>elephants are the biggest animal.</b>

1 个答案:

答案 0 :(得分: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)

这应该可以解决问题