一个python程序,它搜索文本文件并打印出相互的行

时间:2016-06-16 21:15:38

标签: python-3.x

我正在尝试编写一个python程序,它接受n个文本文件,每个文件包含名称,每个名称都在这样一个单独的行上

Steve
Mark
Sarah

程序的作用是只打印出所有输入文件中存在的名称。 我是编程的新手,所以我真的不知道如何实现这个想法,但我认为在递归中,程序似乎仍在无限循环中运行,我不确定是什么问题。是执行错误?如果是这样,你对如何实现它有更好的了解吗?

import sys
arguments = sys.argv[1:]
files = {}

file = iter(arguments)
for number in range(len(sys.argv[1:])):
    files[number] = open(next(file))

def close_files():
    for num in files:
        files[num].close()


def start_next_file(line,files,orderOfFile):
    print('starting next file')
    if orderOfFile < len(files): # to avoid IndexError
        for line_searched in files[orderOfFile]:
            if line_searched.strip():
                line_searched = line_searched[:-1]
                print('searched line = '+line_searched)
                print('searched compared to = ' + line)
                if line_searched == line:
                    #good now see if that name exists in the other files as well
                    start_next_file(line,files,orderOfFile+1) 

    elif orderOfFile >= len(files): # when you finish searching all the files
        print('got ya '+line) #print the name that exists in all the files
        for file in files: 
            # to make sure the cursor is at the beginning of the read files 
            #so we can loop through them again
            files[file].seek(0)



def start_find_match(files):
    orderOfFile = 0
    for line in files[orderOfFile] : 
        # for each name in the file see if it exists in all other files 
        if line.strip():
            line = line[:-1]
            print ('starting line = '+line)
            start_next_file(line,files,orderOfFile+1)



start_find_match(files)
close_files()

1 个答案:

答案 0 :(得分:0)

我不确定如何准确修复代码,但这是考虑它的一种概念性方法。

listdir以列表的形式获取目录中的所有文件。我们将其缩小到只有.txt文件。接下来,打开,读取,拆分换行,然后降低以创建包含名称的更大列表。因此,files将是列表列表。最后,使用一些set逻辑找到所有列表的交集。

import os

folder = [f for f in os.listdir() if f[-4:] == '.txt']
files = []

for i,file in enumerate(folder):
  with open(file) as f:
    files.append([name.lower() for name in f.read().splitlines()])

result = set.intersection(*map(set, files))

示例:

#file1.txt
john
smith
mary
sue
pretesh
ashton
olaf
Elsa

#file2.txt
David
Lorenzo
Cassy
Grant
elsa
Felica
Salvador
Candance
Fidel
olaf
Tammi
Pasquale

#file3.txt
Jaleesa
Domenic
Shala
Berry
Pamelia
Kenneth
Georgina
Olaf
Kenton
Milly
Morgan
elsa

返回:

{'olaf', 'elsa'}