perl+;读取多个csv文件+;操作文件+;提供输出文件

perl+;读取多个csv文件+;操作文件+;提供输出文件,perl,csv,Perl,Csv,抱歉,如果这是一个有点长篇大论,但我真的很感谢这里的答案,因为我有困难使这项工作 在这个问题的基础上,我有了这个脚本,它可以处理csv文件(orig.csv),并提供我想要的csv文件(format.csv)。我想要的是使它更通用,接受任意数量的“.csv”文件,并为每个输入的文件提供一个“output_csv”。有人能帮忙吗 #!/usr/bin/perl use strict; use warnings; open my $orig_fh, '<',

抱歉,如果这是一个有点长篇大论,但我真的很感谢这里的答案,因为我有困难使这项工作

在这个问题的基础上,我有了这个脚本,它可以处理csv文件(orig.csv),并提供我想要的csv文件(format.csv)。我想要的是使它更通用,接受任意数量的“.csv”文件,并为每个输入的文件提供一个“output_csv”。有人能帮忙吗

 #!/usr/bin/perl

    use strict;
    use warnings;

    open my $orig_fh,   '<', 'orig.csv'   or die $!;
    open my $format_fh, '>', 'format.csv' or die $!;

    print $format_fh scalar <$orig_fh>; # Copy header line

    my %data;
    my @labels;

    while (<$orig_fh>) {
      chomp;
      my @fields = split /,/, $_, -1;
      my ($label, $max_val) = @fields[1,12];
      if ( exists $data{$label} ) {
        my $prev_max_val = $data{$label}[12] || 0;
        $data{$label} = \@fields if $max_val and $max_val > $prev_max_val;
      }
      else {
        $data{$label} = \@fields;
        push @labels, $label;
      }
    }

    for my $label (@labels) {
      print $format_fh join(',', @{ $data{$label} }), "\n";
    }

**编辑::我尝试过合并版本,但出现了错误。非常感谢您的建议*

UserName@wabcl13 ~/Perl
$ perl formatfile_QforStackOverflow.pl
Parentheses missing around "my" list at formatfile_QforStackOverflow.pl line 13.
source dir -> /home/UserName/Perl
Can't use string ("/home/UserName/Perl/format_or"...) as a symbol ref while "strict refs" in use at formatfile_QforStackOverflow.pl line 28.
组合代码::

使用严格;
使用警告;
使用autodie;#这用于“多文件”部分。。。
#开始::获取当前工作目录
使用Cwd qw();
我的$source_dir=Cwd::Cwd();
#结束::获取当前工作目录
打印“源目录->$source\u dir\n”;
我的$output_前缀='format_u';
opendir my$dh、$source\u dir#将此更改为在当前目录下工作;调回
对于我的$file(readdir($dh)){
下一个if$file!~/\.csv$/;
下一步如果$file=~/^\Q$output_prefix\E/;
my$orig_file=“$source_dir/$file”;
my$format_file=“$source_dir/$output_prefix$file”;
#…这里是旧的处理代码。。。
##开始::此部分用于为此脚本编辑的一个文件##
#打开我的$orig_fh、'format.csv'或die$!;
#打印$format_fh scalar;#复制标题行#原稿需要更改
打印$format_文件标量;#复制标题行
我的%数据;
我的@标签;
#而{#orig需要改变
而(){
咀嚼;
my@fields=split/,/,$\u1;
我的($label,$max_val)=@fields[1,12];
if(存在$data{$label}){
my$prev_max_val=$data{$label}[12]| 0;
$data{$label}=\@如果$max\u val和$max\u val>$prev\u max\u val;
}
否则{
$data{$label}=\@字段;
按@标签,$label;
}
}
对于我的$label(@labels){
#打印$format_fh join(',',@{$data{$label}),“\n”#源代码需要更改
打印$format_文件联接(',',@{$data{$label}),“\n”;
}
##结束::此部分用于为此脚本编辑的一个文件##
}

您计划如何输入要处理的文件列表及其首选输出目标?可能只是有一个固定的目录,用于处理所有cvs文件,并在结果前加前缀

#!/usr/bin/perl

use strict;
use warnings;
use autodie;

my $source_dir = '/some/dir/with/cvs/files';
my $output_prefix = 'format_';

opendir my $dh, $source_dir;
for my $file (readdir($dh)) {
    next if $file !~ /\.csv$/;
    next if $file =~ /^\Q$output_prefix\E/;

    my $orig_file = "$source_dir/$file";
    my $format_file = "$source_dir/$output_prefix$file";

    .... old processing code here ...

}

或者,您可以只使用一个输出目录,而不是为文件添加前缀。无论哪种方式,这都会让您走上正轨。

很抱歉,我认为这很清楚,我只想将格式化的文件保存在同一目录中,但它们会有一个前缀来区分。这是一种好的做法吗?tksIt是一种足够好的做法,只要当你正确地编写代码时。我向你展示的大部分工作都将由你完成。不过,你最终需要学习更多的编码技能,而不是仅仅在论坛上拼凑别人提供的代码。祝你好运。仍然会出错,请你提出建议,编辑我的Q,tks@HattrickNZ如果出现语法错误,则首先应该关注理解和修复错误。错误通常会告诉您发生错误的确切行号。如果您想帮助理解特定的错误消息,请务必发布代码并提出特定的问题。但我希望先听听您是如何尝试自己找出错误消息的.
opendir( DIR, $path ) or die "$!";  # open the current directory 
open( FH, "$path/$file" ) or die "$!"; #open the file
UserName@wabcl13 ~/Perl
$ perl formatfile_QforStackOverflow.pl
Parentheses missing around "my" list at formatfile_QforStackOverflow.pl line 13.
source dir -> /home/UserName/Perl
Can't use string ("/home/UserName/Perl/format_or"...) as a symbol ref while "strict refs" in use at formatfile_QforStackOverflow.pl line 28.
  use strict;
    use warnings;
    use autodie;   # this is used for the multiple files part...

    #START::Getting current working directory 
    use Cwd qw();
    my $source_dir = Cwd::cwd();
    #END::Getting current working directory 

    print "source dir -> $source_dir\n";
    my $output_prefix = 'format_';

    opendir my $dh, $source_dir; #Changing this to work on current directory; changing back

    for my $file (readdir($dh)) {
        next if $file !~ /\.csv$/;
        next if $file =~ /^\Q$output_prefix\E/;

        my $orig_file = "$source_dir/$file";
        my $format_file = "$source_dir/$output_prefix$file";

        # .... old processing code here ...
        ## Start:: This part works on one file edited for this script ##
        #open my $orig_fh,   '<', 'orig.csv'   or die $!; #line 14 and 15 above already do this!!
        #open my $format_fh, '>', 'format.csv' or die $!;

        #print $format_fh scalar <$orig_fh>; # Copy header line #orig needs changeing
        print $format_file  scalar <$orig_file>; # Copy header line

        my %data;
        my @labels;

        #while (<$orig_fh>) { #orig needs changing
        while (<$orig_file>) {
          chomp;
          my @fields = split /,/, $_, -1;
          my ($label, $max_val) = @fields[1,12];
          if ( exists $data{$label} ) {
            my $prev_max_val = $data{$label}[12] || 0;
            $data{$label} = \@fields if $max_val and $max_val > $prev_max_val;
          }
          else {
            $data{$label} = \@fields;
            push @labels, $label;
          }
        }

        for my $label (@labels) {
          #print $format_fh join(',', @{ $data{$label} }), "\n";  #orig needs changing
          print $format_file join(',', @{ $data{$label} }), "\n";
        }
        ## END:: This part works on one file edited for this script ##

    }
#!/usr/bin/perl

use strict;
use warnings;
use autodie;

my $source_dir = '/some/dir/with/cvs/files';
my $output_prefix = 'format_';

opendir my $dh, $source_dir;
for my $file (readdir($dh)) {
    next if $file !~ /\.csv$/;
    next if $file =~ /^\Q$output_prefix\E/;

    my $orig_file = "$source_dir/$file";
    my $format_file = "$source_dir/$output_prefix$file";

    .... old processing code here ...

}