Python文件匹配和行打印

时间:2017-03-02 15:30:28

标签: python python-2.7

我试图扩展下面的代码(感谢Carles Mitjans),以便如果file1和file2之间匹配,即来自file2的12345和来自file1的12345,则它会打印整行,包括路径但是这两个文件之间存在很多匹配。

但是,我似乎无法将交叉点改为允许这样做的东西。

file1 example                    file2 example
12345 /users/test/Desktop        543252 
54321 /users/test/Downloads      12345  
0000  /users/test/Desktop        11111
                                 0000


with open('Results.txt', 'r') as file1:
    with open('test.txt', 'r') as file2:
        a = set(x.split()[0] for x in file1)
        b = [x.rstrip() for x in file2]
        same = a.intersection(b)
        for line in same:
            print line

same.discard('\n')

目前正在输出 12345 0000

非常感谢任何指导指导。

1 个答案:

答案 0 :(得分:1)

使用a = set() map = {} with open('Results.txt', 'r') as file1: for x in file1: splits = x.split() a.add(splits[0]) map[splits[0]] = x b = [] with open('test.txt', 'r') as file2: b = [x.rstrip() for x in file2] same = a.intersection(b) same.discard('\n') for keyItem in same: print map[keyItem] 将该行映射到匹配的字词。此外,不需要嵌套两个循环。它们可以单独计算,以便加快处理速度。

map = dict()
with open('Results.txt', 'r') as file1:
    for x in file1:
        splits = x.split()
        a.add(splits[0])

        # If the key is not present, initialize it
        if splits[0] not in map:
            map[splits[0]] = []
        # Append the line to map value
        map[splits[0]].append(x)

OP注意到上述解决方案,只会打印最后一场比赛。 更改地图可以解决问题。

   -F, --fixed-strings
          Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.  (-F is specified
          by POSIX.)

其他事情将保持不变。