在perl中将两个csv文件组合在一起

在perl中将两个csv文件组合在一起,perl,csv,Perl,Csv,大家好,我对perl非常陌生,对perl有一些了解,但我正在尝试创建一个脚本,将两个.csv文件合并成一个新文件 #!/usr/bin/env perl use strict; use warnings; use Text::CSV_XS; my @rows; { # Read the CSV file my $csv = Text::CSV_XS->new() or die "Cannot use Text::CSV_XS ($!)"; my $file = "fi

大家好,我对perl非常陌生,对perl有一些了解,但我正在尝试创建一个脚本,将两个.csv文件合并成一个新文件

#!/usr/bin/env perl
use strict;
use warnings;
use Text::CSV_XS;

my @rows;
{   # Read the CSV file
    my $csv = Text::CSV_XS->new() or die "Cannot use Text::CSV_XS ($!)";
    my $file = "file.csv";
    open my $fh, '<', $file or die "Cannot open $file ($!)";
    while (my $row = $csv->getline($fh)) {
        push @rows, $row;
    }
    $csv->eof or $csv->error_diag();
    close $fh or die "Failed to close $file ($!)";
}

{    # Gather the data
    foreach my $row (@rows) {
        foreach my $col (@{$row}) {
            $col = uc($col);
        }
        print "\n";
    }
}
# (over)Write the data 
#  Needs to be changed to ADD data
{
    my $csv = Text::CSV_XS->new({ binary => 1, escape_char => undef })
        or die "Cannot use Text::CSV ($!)";
    my $file = "output.csv";
    open my $fh, '>', $file or die "Cannot open $file ($!)";
    $csv->eol("\n");
    foreach my $row (@rows) {
        $csv->print($fh, \@{$row}) or die "Failed to write $file ($!)";
    }
    close $fh or die "Failed to close $file ($!)";
}

我认为问题在于:

open my $fh, '>', $file
    or die "Cannot open $file ($!)";
如果我正确地记住了Perl,那么这行应该是:

open my $fh, '>>', $file
    or die "Cannot open $file ($!)";

>
应该打开文件句柄
$fh
进行追加,而不是覆盖。

您可以尝试类似的方法

opendir(hand,"DIRPATH");
@files = readdir(hand);
closedir(hand);

foreach(@files){
                if(/\.csv$/i) {                                                                 #if the filename has .csv at the end
                            push(@csvfiles,$_);
                                }
                }

foreach(@csvfiles) {
                    $csvfile=$_;
                    open(hanr,"DIRPATH".$csvfile)or die"error $!\n";                        #read handler 
                    open(hanw , ">>DIRPATH"."outputfile.csv") or die"error $! \n";          #write handler for creating new sorted files
                    @lines=();
                    @lines=<hanr>;

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

        $count= -1;
        close(hanw);
        close(hanr);
                    }`
opendir(手,DIRPATH);
@files=readdir(hand);
closedir(手);
foreach(@files){
如果(/\.csv$/i){如果文件名末尾有.csv
推送(@csvfiles,$));
}
}
foreach(@csvfiles){
$csvfile=$\uU4;
打开(hanr,“DIRPATH”。$csvfile)或关闭“error$!\n”#读取处理程序
打开(hanw,“>>DIRPATH”“outputfile.csv”)或关闭“error$!\n”#写入用于创建新排序文件的处理程序
@行=();
@行=;
foreach$行(@行){
chomp$行;
$count++;
下一步,除非$count;#跳过标题,即包含股票详细信息的第一行
打印hanw join$行“\n”;
}
$count=-1;
关闭(hanw);
关闭(hanr);
}`

定义“合并两个CSV文件”。它们是否具有相同的列,而您只想将一行连接到另一行?(基本上,只需去掉第二个文件的标题)它们是否有相同的行但不同的列,并且您希望附加列?你从什么开始,你想以什么结束?您甚至可以使用shell级别的命令来实现这一点(无需编写自己的程序)它们都有相同的标题和相同的行和列。只是每个月都会重复出现一个程序抛出csv文件,它们需要自动添加在一起。有没有任何理由你不能只做
cat file1 file2>file3
来连接文件?
\@{$row}
是双重冗余的
$row
已经是一个数组引用,您不需要取消对它的引用,而是对已取消引用的引用进行引用。这只是一个可以简化的“双重否定”。它与您提供的代码“不起作用”,它只需要一个硬编码文件并将其打印到一个新文件:
output.csv
。当然,除非您已经在
output.csv
中写入了一些您在问题中没有透露的内容。@TLP:我一直在重写我的答案,以便在父块中打开
$fh
(我将其作为目标文件),以及其中的硬编码输入文件。原始代码的范围似乎超出了需要。但在我提交更改之前,@Brein接受了我的回答。所以,我是对的,你是对的,一切都很正确@CodeswithHammer好吧,我猜你确实为OP提供了他所需要的,我只是认为这个问题没有意义,因为它被保存起来以备将来参考。
opendir(hand,"DIRPATH");
@files = readdir(hand);
closedir(hand);

foreach(@files){
                if(/\.csv$/i) {                                                                 #if the filename has .csv at the end
                            push(@csvfiles,$_);
                                }
                }

foreach(@csvfiles) {
                    $csvfile=$_;
                    open(hanr,"DIRPATH".$csvfile)or die"error $!\n";                        #read handler 
                    open(hanw , ">>DIRPATH"."outputfile.csv") or die"error $! \n";          #write handler for creating new sorted files
                    @lines=();
                    @lines=<hanr>;

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

        $count= -1;
        close(hanw);
        close(hanr);
                    }`