【问题标题】:Issues while creating CSV out of an array in Perl在 Perl 中从数组创建 CSV 时出现问题
【发布时间】:2016-04-11 12:12:31
【问题描述】:

我的问题的简要描述:

我有一个需要转换为CSV 的数组。我面临着所需输出的问题,而不是转换本身的问题。

我的代码:

use strict;
use warnings;
use Text::CSV;
use Text::CSV_XS;
use Data::Dumper; 

my (@first_set, @final_set); 
my $file = do {
    open my $fh, '<', '<file_name>.txt' or die "Unable to open file for input: $!";
    local $/;
    <$fh>;
};

@first_set = ( $file =~ m/<regex>/g)  ;
while ( @first_set ) {
    @final_set = splice @first_set, 0, 26;
    open(my $output, '>', 'mpc7_file.csv') or die "Couldn't open file file.csv, $!";
    my $csv = Text::CSV -> new ( { sep_char => ",", eol => "\n", binary => 1, quote_char => undef } );
    foreach my $element ( @final_set ) { 
        $csv -> print ( \*$output, \@final_set );
    }   
    close($output) or die "Couldn't close file properly";
}

@final_set的内容:

10:38:49 788 56 51 56 61 56 59 56 51 56 80 56 83 56 50 45 42 45 50 45 50 45 43 45 54
10:38:51 788 56 51 56 61 56 59 56 51 56 80 56 83 56 50 45 42 45 50 45 50 45 43 45 54

CSV 文件的内容:

10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54

期望的输出:

我的CSV 文件应该包含@final_set(每个元素用逗号分隔)。

10:38:49,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54

问题:

我哪里出错了?我真的无法理解我的错误。请帮忙。

编辑:

即使我将CSV 文件创建块移到while 循环之外,输出仍然相同。

【问题讨论】:

  • Contents of @final_set 是实际的数组内容,顺便说一下也应该是CSV 的内容,只是需要将数组转换为CSV

标签: arrays perl csv


【解决方案1】:

好的,我明白你在做什么。问题是 @final_set 每次在 while 循环周围都会发生变化,无论您是像这样全局还是在本地声明它,您仍然需要在每次更改时打印它的内容

对于这么简单的数据,也没有必要使用Text::CSV。如您所见,在第一次使用时声明每个变量,这是大多数语言的最佳实践

use strict;
use warnings;

my $in_file  = '<file_name>.txt';
my $out_file = 'mpc7_file.csv';

my $data = do {
    open my $fh, '<', $in_file or die qq{Unable to open "$in_file" for input: $!};
    local $/;
    <$fh>;
};

my @first_set = $data =~ /<regex>/g;

open my $out_fh, '>', $out_file or die qq{Unable to open "$out_file" for output: $!};

while ( @first_set ) {
    my @final_set = splice @first_set, 0, 26;
    print $out_fh join(',', @final_set), "\n";
}

close $out_fh or die qq{Unable to close "$out_file": $!};

【讨论】:

  • 我意识到Perl v5.14 在我的组织/公司中并不是标准的。所以,我最终得到了这个语法错误:Perl v5.14.0 required--this is only v5.8.9, stopped at example.pl line 7. BEGIN failed--compilation aborted at example.pl line 7. 有什么解决方法吗?
  • @Ghost:好的,这意味着你不能像我建议的那样使用autodie,必须手动检查open 调用。我已经更新了我的解决方案
猜你喜欢
  • 2018-10-23
  • 2014-10-28
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多