【问题标题】:perl to merge csv files removing the headingsperl 合并 csv 文件删除标题
【发布时间】:2014-10-15 03:47:32
【问题描述】:

我在一个文件夹中有几份 csv 格式的月度报告。 csv 文件都有 8 个公共列(带标题)。使用 perl,我想将这些文件逐行合并在一起。

文件 1:

1,2,3,4,5,6,7,8,
a1,b1,c1,d1,e1,f1,g1,h1,
a1,b1,c1,d1,e1,f1,g1,h1,
a1,b1,c1,d1,e1,f1,g1,h1,

文件 2:

1,2,3,4,5,6,7,8,
a2,b2,c2,d2,e2,f2,g2,h2,
a2,b2,c2,d2,e2,f2,g2,h2,
a2,b2,c2,d2,e2,f2,g2,h2,

我希望输出看起来像这样(加入行并删除标题)

输出:

1,2,3,4,5,6,7,8,
a1,b1,c1,d1,e1,f1,g1,h1,
a1,b1,c1,d1,e1,f1,g1,h1,
a1,b1,c1,d1,e1,f1,g1,h1,
a2,b2,c2,d2,e2,f2,g2,h2,
a2,b2,c2,d2,e2,f2,g2,h2,
a2,b2,c2,d2,e2,f2,g2,h2,

我已设法将文件名保存在一个数组中。但由于某种原因,我无法加入他们。 你能帮我弄清楚我的代码有什么问题吗?我对 perl 很陌生。

#! C:Strawberry/perl/bin;
use feature ':5.12';
use strict;
use warnings;

my $data_directory = 'R:/testing_data/';

opendir( DIR, $data_directory ) or die "Could not open $data_directory $!\n";
my @files = grep {/_monthlyreport\.csv$/} readdir(DIR);    #to get on the monthly reports csv files

foreach my $file (@files) {
    open( HANR, "<", '$data_directory' . my $files ) or die "cannot open $files: $!";    #read handler
    open( HANW, ">>", "G:/outputfile_script.csv" ) or die "error $! \n"; #write handler for creating new sorted files
    my @lines = ();
    @lines = <HANR>;

    foreach my $line (@lines) {
        chomp($line);
        my $count++;
        next unless $count;    # skip header i.e the first line containing stock details
        print HANW join $line, "\n";
    }

    my $count = -1;
    close(HANW);
    close(HANR);
}

closedir(DIR);
exit 0;

【问题讨论】:

    标签: perl csv merge


    【解决方案1】:

    添加一个计数器,如果计数器等于0则停止打印;

    #! C:Strawberry/perl/bin;
    use feature ':5.12';
    use strict;
    use warnings;
    
    my $data_directory = 'R:/testing_data/';
    
    opendir(DIR,$data_directory) or die "Could not open $data_directory $!\n";
    my @files = grep {/_monthlyreport\.csv$/} readdir(DIR); #to get on the monthly reports csv files
    
    foreach my $file (@files) {
    
                        open(HANR ,"<",'$data_directory'.my $files) or die "cannot open $files: $!";                        #read handler 
                        open(HANW , ">>","G:/outputfile_script.csv") or die "error $! \n";          #write handler for creating new sorted files
                        my @lines=();
                        @lines=<HANR>;
             my $i =0;
    
            foreach my $line (@lines){
    
                    next if ($i==0) ;
                                    chomp ($line) ; 
                                    my $count++;
                                    next unless $count;                                              # skip header i.e the first line containing stock details
                                    print HANW join $line,"\n";
                                    }
    
            my $count= -1;
            close(HANW);
            close(HANR);
                       }
    
    closedir(DIR);
    exit 0;
    

    【讨论】:

    • 感谢您的帮助 jens!代码仍然不能完美运行。你能不能帮我修一下。我得到:全局符号“$files”需要在 G:\script1.plx 第 13 行显示包名。在 G:\script1.plx 第 21 行使用“strict subs”时不允许使用裸词“i”。执行 G :\script1.plx 由于编译错误而中止。
    • @fmfshog:对不起我的错误。我改变了我的答案:(下一个 if ($i==0) )
    【解决方案2】:

    这一行是错误的。

    open(HANR ,"<",'$data_directory'.my $files) or die "cannot open $files: $!";
    

    应该是

    open(HANR ,"<","$data_directory".$files) or die "cannot open $files: $!";
    

    【讨论】:

      【解决方案3】:

      您对输入文件句柄的 open 语句格式错误,my $count++; 也已损坏。

      我还建议使用词法文件句柄来现代化您的代码。以下是您的代码的清理版本:

      use feature ':5.12';
      use strict;
      use warnings;
      use autodie;
      
      my $data_directory = 'R:/testing_data/';
      
      opendir my $dh, "$data_directory";
      
      open my $outfh, ">>", "G:/outputfile_script.csv";
      my $seenheader = 0;
      
      while (my $file = readdir $dh) {
          next unless $file =~ /_monthlyreport\.csv$/;
      
          open my $infh, '<', "$data_directory/$file";
          while (<$infh>) {
              print $outfh $_ if $. > 1 || ! $seenheader++;
          }
      }
      

      【讨论】:

      • 谢谢!但是我遇到了一些错误:G:\scriptnew.plx 第 16 行的语法错误,靠近“open” 不能使用全局 $。在 G:\scriptnew.plx 第 18 行的“my”中,靠近“if $”。由于编译错误,G:\scriptnew.plx 的执行中止。
      • 只是在next 行后面少了一个分号。
      猜你喜欢
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 2017-09-09
      • 2012-11-16
      相关资源
      最近更新 更多