Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python使用来自另一个文件的输入来搜索文件中的文本_Python_Python 2.7 - Fatal编程技术网

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

Python使用来自另一个文件的输入来搜索文件中的文本,python,python-2.7,Python,Python 2.7,我是python和编程新手。我需要一些关于python脚本的帮助。有两个文件,每个文件包含电子邮件地址(超过5000行)。输入文件包含我要在数据文件中搜索的电子邮件地址(还包含电子邮件地址)。然后我想将输出打印到一个文件或显示在控制台上。我搜索脚本,并能够修改,但我没有得到想要的结果。你能帮帮我吗 dfile1 (50K lines) yyy@aaa.com xxx@aaa.com zzz@aaa.com ifile1 (10K lines) ccc@aaa.com vvv@aaa.com

我是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

我认为您可以删除
name=fd.readline()
,因为您已经在for循环中获得了该行。它将读取除for循环之外的另一行,for循环每次读取一行。另外,我认为
name[1:-1]
应该是
name
,因为在搜索时不想去掉第一个和最后一个字符
with
自动关闭打开的文件

附言:我会怎么做:

with open("dfile1") as dfile, open("ifile") as ifile:
    lines = "\n".join(set(dfile.read().splitlines()) & set(ifile.read().splitlines())
print(lines)
with open("ofile", "w") as ofile:
    ofile.write(lines)

在上面的解决方案中,基本上我采用两个文件行的并集(两个集合的元素部分)来查找公共行。

我认为您的问题来自以下方面:

name = fd.readline()
if name[1:-1] in names:
name[1:-1]
对每个电子邮件地址进行切片,以便跳过第一个和最后一个字符。虽然通常最好跳过最后一个字符(换行符
'\n'
),但在“dfile”中加载名称数据库时

您正在添加新行。所以,根本不要在“ifile”中分割名称,即

if name in names:

下面是我要做的:

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

mitan8解决了您的问题,但我会这样做:

with open(inputfile, "r") as f:
    names = set(i.strip() for i in f)

output = []

with open(datafile, "r") as f:
    for name in f:
        if name.strip() in names:
            print name
这样可以避免将较大的数据文件读入内存

如果要写入输出文件,可以使用语句对第二个
执行此操作:

with open(datafile, "r") as i, open(outputfile, "w") as o:
    for name in i:
        if name.strip() in names:
            o.write(name)

也许我遗漏了什么,但为什么不用一副呢

#!/usr/local/cpython-3.3/bin/python

data_filename = 'dfile1.txt'
input_filename = 'ifile1.txt'

with open(input_filename, 'r') as input_file:
    input_addresses = set(email_address.rstrip() for email_address in input_file.readlines())

with open(data_filename, 'r') as data_file:
    data_addresses = set(email_address.rstrip() for email_address in data_file.readlines())

print(input_addresses.intersection(data_addresses))

很好的解决方案,但是如果输入为~10k行,是否适合使用
readlines()
?老实说,我对这种内存使用规模没有太多经验。@kevinsa5每行大约需要60个字节(Python的字符串类型有相当大的开销),那么集合的开销应该是600k+左右(在任何情况下都不到一兆字节)。我不熟悉使用集合,但这会得到他在输入文件中寻找的电子邮件地址吗?还是这就解决了所有问题?@BraydonKains我的答案只是找到了共同的行,这与他的问题是一样的,或者我认为是这样。
.readlines()
在字符串中留下了一个新行;您可以改为使用
file.read().splitlines()
。我将其更改为if name in names。但它给了我这个错误。文件“C:\Python27\scripts\test.py”,第12行,in name=f.readlines()value错误:混合使用迭代和读取方法会丢失数据您能确保代码中的缩进正确吗?这将帮助我理解您的错误。以下是我的代码:将open(inputfile,'r')作为f:names=f.readlines()#打印names outputlist=[],将open(datafile,'r')作为f:for-in-in-f:name=f.readlines()如果name-in-in-name:outputlist.append(line)否则:打印“未找到任何内容”印刷品outputlist@HarryD你能把问题编辑成那个代码吗?@HarryD:你的“新代码”中有一个输入错误。它应该是
name=f.readline()
(而不是
readlines()
)。如果文件末尾没有换行符,则可能会失败,因为文件是Python中行的迭代器。如果name:o.write(name)
->
如果name:o.writeline(name)
中有name.strip(),则无需调用.readlines()
。您可以使用
-语句(只需添加逗号)在一个
中打开多个文件。@J.F.Sebastian Edited!我之所以有
readlines
,是因为我在添加生成器表达式以调用
strip
之后没有删除它。
(…对于数据文件中的电子邮件地址)
就足够了。在Python中,文件是行的迭代器。无需调用
.readlines()
对于末尾没有换行符的文件,可能会失败;您可以调用
.rstrip(“\n”)
来修复它。这里的列表也是无效的,您可以像在其他答案中一样使用
set()
#!/usr/local/cpython-3.3/bin/python

data_filename = 'dfile1.txt'
input_filename = 'ifile1.txt'

with open(input_filename, 'r') as input_file:
    input_addresses = set(email_address.rstrip() for email_address in input_file.readlines())

with open(data_filename, 'r') as data_file:
    data_addresses = set(email_address.rstrip() for email_address in data_file.readlines())

print(input_addresses.intersection(data_addresses))