Python使用来自另一个文件的输入搜索文件以查找文本

我是
python和编程的新手.我需要一些python脚本的帮助.有两个文件,每个文件包含电子邮件地址(超过5000行).输入文件包含我要在数据文件中搜索的电子邮件地址(也包含电子邮件地址).然后我想将输出打印到控制台上的文件或显示.我搜索脚本并能够修改,但我没有得到所需的结果.你能帮我么?

dfile1 (50K lines)
yyy@aaa.com
xxx@aaa.com
zzz@aaa.com


ifile1 (10K lines)
ccc@aaa.com
vvv@aaa.com
xxx@aaa.com
zzz@aaa.com

Output file
xxx@aaa.com
zzz@aaa.com



datafile = 'C:\\Python27\\scripts\\dfile1.txt'
inputfile = 'C:\\Python27\\scripts\\ifile1.txt'

with open(inputfile, 'r') as f:
names = f.readlines()

outputlist = []

with open(datafile, 'r') as fd:
  for line in fd:
    name = fd.readline()
    if name[1:-1] in names:
        outputlist.append(line)
    else:
        print "Nothing found"
 print outputlist

新规范

with open(inputfile, 'r') as f:
    names = f.readlines()
outputlist = []

with open(datafile, 'r') as f:
    for line in f:
        name = f.readlines()
        if name in names:
            outputlist.append(line)
        else:
            print "Nothing found"
    print outputlist

最佳答案 这就是我要做的事情:

names=[]
outputList=[]
with open(inputfile) as f:
    for line in f:
        names.append(line.rstrip("\n")

myEmails=set(names)

with open(outputfile) as fd, open("emails.txt", "w") as output:
    for line in fd:
        for name in names:
            c=line.rstrip("\n")
            if name in myEmails:
                print name #for console
                output.write(name) #for writing to file
点赞