Python 如何比较两个文件并仅打印与第一个文件匹配的第二个文件

Python 如何比较两个文件并仅打印与第一个文件匹配的第二个文件,python,perl,comparison,Python,Perl,Comparison,我有两个文件。其中一个有两列,ref.txt。另一个有三列,file.txt 在ref.txt中 1 2 2 3 3 5 在file.txt中 1 2 4 <---here matching 3 4 5 6 9 4 2 3 10 <---here matching 4 7 9 3 5 7 <---here matching 我想两本字典比较起来 mydict = {} mydict1 = {} with open('ref.txt'

我有两个文件。其中一个有两列,ref.txt。另一个有三列,file.txt

在ref.txt中

1  2
2  3
3  5
在file.txt中

1  2  4   <---here matching
3  4  5
6  9  4
2  3  10  <---here matching
4  7  9
3  5  7   <---here matching
我想两本字典比较起来

mydict = {}
mydict1 = {}

with open('ref.txt') as f1:
     for line in f1:
         key, key1 = line.split()
         sp1 = mydict[key, key1]

with open('file.txt') as f2:
      for lines in f2:
          item1, item2, value = lines.split()
          sp2 = mydict1[item1, item2]
          if sp1 == sp2:
             print value
如何将两个文件与字典或其他文件进行适当比较

我发现一些perl和python代码可以解决两个文件中相同数量的列

在我的例子中,一个文件有两列,另一个有三列


如何比较两个文件并只打印匹配的值?

这里有一个修订版和注释版,适用于更大的数据集:

#read in your reference and the file
reference = open("ref.txt").read()
filetext = open("file.txt").read()

#split the reference file into a list of strings, splitting each time you encounter a new line
splitReference = reference.split("\n")

#do the same for the file
splitFile = filetext.split("\n")

#then, for each line in the reference,
for referenceLine in splitReference:

  #split that line into a list of strings, splitting each time you encouter a stretch of whitespace
  referenceCells = referenceLine.split()

  #then, for each line in your 'file',  
  for fileLine in splitFile:

    #split that line into a list of strings, splitting each time you encouter a stretch of whitespace
    lineCells = fileLine.split()

    #now, for each line in 'reference' check to see if the first value is equal to the first value of the current line in 'file'
    if referenceCells[0] == lineCells[0]:

      #if those are equal, then check to see if the current rows of the reference and the file both have a length of more than one
      if len(referenceCells) > 1:
        if len(lineCells) > 1:

          #if both have a length of more than one, compare the values in their second columns. If they are equal, print the file line
          if referenceCells[1] == lineCells[1]:
            print fileLine
输出:

如果两个文件中字符之间的空白量相同,就足够了。如果不是,你可以这样做

awk '{print "^" $1 "[[:space:]]+" $2}' | xargs -I {} grep -E {} file.txt
结合了我最喜欢的三个实用程序:
awk
grep
xargs
。。。后一种方法还确保匹配只发生在行的开头(将列1与列1进行比较,将列2与列2进行比较)。

还有另一个选项:

use strict;
use warnings;

my $file = pop;
my %hash = map { chomp; $_ => 1 } <>;

push @ARGV, $file;

while (<>) {
    print if /^(\d+\s+\d+)/ and $hash{$1};
}

希望这有帮助

(ref.txt)do grep“^$i”file.txt中i的某些ike
;完成
将很接近。是否将
file.txt
中的条目
0 1 2
视为匹配?基本上,
ref.txt
中的条目必须在
file.txt
行的任何位置找到,还是必须以
值开头?这里有一个类似的问题:-它希望在某些列上匹配(但在这种情况下,列是混合的-比您的问题要难一点)。答案非常有趣,值得一读。感谢您的所有评论。我对这段代码有一个错误,例如,回溯(最近一次调用):文件“test.py”,第13行,在if referenceCells[0]==lineCells[0]:indexer:list index超出范围您是如何获得此输出的?谢谢。我猜生成此错误消息时使用的输入文件包含的行数比此处提供的示例数据多,对吗?较大文件中必须有一行或多行包含空单元格。我将编辑我的答案,向您展示如何处理这些行…您是如何获得此输出的?这段代码在运行时也有一个错误。在ref.txt第1行的“12”附近找到运算符的位置(2之前缺少运算符?)在ref.txt第2行的“2”附近找到运算符的位置(上一行缺少分号?)在ref.txt第2行的“2 3”附近找到运算符的位置(3之前缺少运算符?)在ref.txt第3行“3”附近找到运算符所需的数字(上一行缺少分号?)ref.txt第1行“12”附近出现语法错误。由于编译错误,ref.txt的执行被中止。@ChangWoonJang-我很抱歉。我最初的用法是
perl ref.txt file.txt[>outFile]
,但它应该是
perl script.pl ref.txt file.txt[>outFile]
,现在已经修复了。是的!仅仅将这两个文本文件直接发送到Perl是行不通的。我们如何使用Python实现这一点?我不懂Perl,第一个grep很简单,很好用。非常感谢。“让事情尽可能简单,但不要简单。”-通常归于阿尔伯特·爱因斯坦。“grep命令”有两个小问题。如果file.txt包含111 2 4,这也与ref.txt中的1 2匹配,因为111中的最后1和2与ref.txt中的“1 2”匹配。此外,如果两个数字之间的双空格与单个空格的模式不同。这是一种简单而好的方法,但需要改进。非常感谢。这就是为什么我给出了第二个例子——就是为了解决这些问题。它将只匹配“从一开始”(使用
^
)和可变的空间量(使用
[[:space:]
)。不幸的是,它使它有点复杂-但仍然适合在一条线上。
grep -Ff ref.txt file.txt
awk '{print "^" $1 "[[:space:]]+" $2}' | xargs -I {} grep -E {} file.txt
use strict;
use warnings;

my $file = pop;
my %hash = map { chomp; $_ => 1 } <>;

push @ARGV, $file;

while (<>) {
    print if /^(\d+\s+\d+)/ and $hash{$1};
}
1  2  4
2  3  10
3  5  7