繁体   English   中英

使用Perl比较两个CSV文件

[英]Compare two CSV Files with Perl

我有两个CSV文件,我想与Perl进行比较。

我有使用Text::CSV::Slurp将文件导入Perl的代码,它为我提供了一个很好的文件散列引用数组。

使用Data::Dumper::Concise正确显示我的所有数据导入。

use strict;
use warnings;

use Text::CSV::Slurp;
use Data::Dumper::Concise;

my $file1_src = "IPB-CSV.csv";

my $file2_src = "SRM-CSV.csv";

my $IPB = Text::CSV::Slurp->load(file => $file1_src);
my $SRM = Text::CSV::Slurp->load(file => $file2_src);

print Dumper($IPB);
print Dumper($SRM);

转储的结果看起来像这样

$ IPB

[
  {
    Drawing => "1001"
  },
  {
    Drawing => "1002"
  },
  {
    Drawing => "1003"
  }
]

$ SRM

[
  {
    Drawing => "1001",
    Figure => "Figure 2-8",
    Index => 2,
    Nomenclature => "Some Part"
  },
  {
    Drawing => "1002",
    Figure => "Figure 2-8",
    Index => 2,
    Nomenclature => "Some Part"
  },
  {
    Drawing => "2001",
    Figure => "Figure 2-8",
    Index => 2,
    Nomenclature => "Some Part"
  },
  {
    Drawing => "2002",
    Figure => "Figure 2-8",
    Index => 2,
    Nomenclature => "Some Part"
  }
]

我想根据每个哈希的Drawing键比较两个数组,并创建两个CSV文件,如下所示

  • 一个包含$IPB但不是$SRM ,仅包含`Drawing列中的数据。

  • 另一个项目在$SRM但不在$IPB ,包含与Drawing列相关的所有字段。

我找到了很多信息来比较文件以查看它们是否匹配,或者比较单个数据片段的哈希值或数组,但我找不到特定于我需要的东西。

由于绘图是一种排序标准,为什么不将数据“索引”到一个更方便的地方,其中绘图索引是关键,相应的数据是相应的值?

my %ipb;
for my $record ( @$IPB ) {
    my $index = $record->{Drawing};
    push @{ $ipb{$index} }, $record;
}

my %srm;
for my $record ( @$SRM ) {
    my $index = $record->{Drawing};
    push @{ $srm{$index} }, $record;
}

现在找出$IPB$SRM独有的索引应该是轻而易举的:

use List::MoreUtils 'uniq';
my @unique_ipb = uniq( grep { $ipb{$_} and not $srm{$_} } keys( %ipb ), keys( %srm ) );
my @unique_srm = uniq( grep { $srm{$_} and not $ipb{$_} } keys( %ipb ), keys( %srm ) );

两者有什么共同之处?

my @intersect = uniq( grep { $srm{$_} and $ipb{$_} } keys( %ipb ), keys( %srm ) );

绘图索引1002的所有图号是什么?

print $_->{Figure}, "\n" for @{ $ipb{1002} // [] }, @{ $srm{1002} // [] };

这个简短的程序使用$ipb$srm示例值,并创建我认为你想要的输出。 (除了包名等全局标识符外, 不要使用大写字母。)

有几个问题

  • 使用Text::CSV::Slurp会为您留下两个散列数组,这些散列对此任务没有用,无需进一步索引。 通过逐行处理文件,从头开始创建适当的数据结构会更好

  • 你说你的第二个文件必须包含与每个Drawing键相关的所有信息,但是,因为Perl哈希本质上是无序的,所以Text::CSV::Slurp已经丢失了字段名称的顺序。 可以做的最好的事情是按照找到的顺序打印数据,但是在显示字段名称的标题行之前。 这是避免Text::CSV::Slurp另一个原因

use strict;
use warnings;
use autodie;

# The original data

my $ipb = [{ Drawing => 1001 }, { Drawing => 1002 }, { Drawing => 1003 }];

my $srm = [
  {
    Drawing => "1001",
    Figure => "Figure 2-8",
    Index => 2,
    Nomenclature => "Some Part"
  },
  {
    Drawing => "1002",
    Figure => "Figure 2-8",
    Index => 2,
    Nomenclature => "Some Part"
  },
  {
    Drawing => "2001",
    Figure => "Figure 2-8",
    Index => 2,
    Nomenclature => "Some Part"
  },
  {
    Drawing => "2002",
    Figure => "Figure 2-8",
    Index => 2,
    Nomenclature => "Some Part"
  }
];

# Index the data

my %srm;
for my $item (@$srm) {
  my $drawing = $item->{Drawing};
  $srm{$drawing} = $item;
}

my %ipb;
for my $item (@$ipb) {
  my $drawing = $item->{Drawing};
  $ipb{$drawing} = 1;
}

# Create the output files

open my $csv1, '>', 'file1.csv';
for my $id (sort keys %ipb) {
  next if $srm{$id};
  print $csv1 $id, "\n";
}
close $csv1;

open my $csv2, '>', 'file2.csv';
my @keys = keys %{ $srm->[0] };
print $csv2 join(',', @keys), "\n";
for my $id (sort keys %srm) {
  next if $ipb{$id};
  print $csv2 join(',', @{$srm{$id}}{@keys}), "\n"; 
}
close $csv2;

产量

file1.csv

1003

file2.csv

Drawing,Nomenclature,Index,Figure
2001,Some Part,2,Figure 2-8
2002,Some Part,2,Figure 2-8

这有点复杂,因为您的数据结构不太适合比较。 您有对散列引用数组的引用,并且您关心hashref的其中一个键中的数据。 我的第一步是将IPB压平为一个数组(因为此下没有数据),并将SRM转换为单个hashref。

my @ipbarray = map { ${$_}{Drawing} } $IPB; # Creates an array from IPB.
my $srmhash = {};
for my $hash ($SRM) {
  ${$srmhash}{${$hash}{Drawing}} = $hash unless defined ${$srmhash}{${$hash}{Drawing}}; # Don't overwrite if it exists
}

现在我们有2个可行的数据结构。

下一步是对比这些值:

my @ipbonly = ();
my @srmonly = ();

for my $ipbitem (@ipbarray) {
  push @ipbonly, ( Drawing => $ipbitem } unless defined ${$srmhash}{$ipbtem};
}

for my $srmitem (keys $srmhash) {
  push @srmonly, ${$srmhash}{$srmitem} unless grep { $_ == $srmitem } @ipbarray;
}

此时,@ ipbonly和@srmonly将包含您想要的数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM