1.写生成器,从文件中读取内容,再每一行读取的内容前加上 ‘***’之后返回给用户 def func(filename): word = input('输入你想找的内容:') with open(filename,mode='r',encoding='utf-8') as f: for line in f: if word in line: yield line file = input('输入你要查找的文件:') g = func(file) for i in g: print('###'+ i.strip())