#1.txt为关键词 #2.txt为提取目标 #3.txt储存提取结果 f = open('1.txt','r',encoding='utf-8') q = open('3.txt','a+',encoding='utf-8') line1 = f.readlines() for l in line1: p = open('2.txt', 'r', encoding='utf-8') line2 = p.readlines() for ll in line2: if l.strip() in ll: q.writelines(ll) p.close() f.close() q.close()