【发布时间】:2016-07-07 04:54:39
【问题描述】:
我正在尝试解析格式如下的 CSV 文件:
dog cats,yellow blue tomorrow,12445
birds,window bank door,-novalue-
birds,window door,5553
aspirin man,red,567
(写入-novalue-的地方没有值)
use strict;
use warnings;
my $filename = 'in.txt';
my $filename2 = 'out.txt';
open(my $in, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";
my $word = "";
while (my $row = <$in>) {
chomp $row;
my @fields = split(/,/,$row);
#Save the first word of the second column
($word) = split(/\s/,$fields[1]);
if ($word eq 'importartWord')
{
printf $out "$fields[0]".';'."$word".';'."$fields[2]";
}
else #keep as it was
{
printf $out "$fields[0]".';'."$fields[1]".';'."$fields[2]";
}
Use of uninitialized value $word in string ne at prueba7.pl line 22, <$in> line 10.
无论我在哪里定义 $word 我都无法停止接收该错误并且无法理解原因。我想我已经正确初始化了 $word 。非常感谢您的帮助。
如果您要建议使用 Text::CSV,请发布一个工作代码示例,因为我无法将它应用于我在此处解释的建议。这就是我最终编写上述代码的原因。
PD: 因为我知道你会使用 Text::CSV 询问我之前的代码,所以这里是:
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;
my $csv = Text::CSV->new({ sep_char => ';', binary => 1 }) or
die "Cannot use CSV: ".Text::CSV->error_diag ();
#directorio donde esta esc_prim2.csv
my $file = 'C:\Users\Sergio\Desktop\GIS\perl\esc_prim2.csv';
my $sal = 'C:\Users\Sergio\Desktop\GIS\perl\esc_prim3.csv';
open my $data, "<:encoding(utf8)", "$file" or die "$file: $!";
open my $out, ">:encoding(utf8)", "$sal" or die "$sal: $!";
$csv->eol ("\r\n");
#initializing variables
my $row = "";
my $word = "";
my $validar = 0;
my $line1 = "";
my @mwords = [""];#Just a try to initialize mwords... doesn't work, error keeps showing
#save the first line with field names on the other file
$line1 = <$data>;
$csv->parse($line1);
my @fields = $csv->fields();
$csv->print($out,[$fields[0], $fields[1], $fields[2]]);
while ($row = <$data>) {
if ($csv->parse($row)) {
@fields = $csv->fields();
#save first word of the field's second element
@mwords = split (/\s/, $fields[1]);
#keep the first one
$word = $mwords[0];
printf($mwords[0]);
#if that word is not one of SAN, EL y LA... writes a line in the new file with the updated second field.
$validar = ($word ne 'SAN') && ($word ne 'EL') && ($word ne 'LA');
if ($validar)
{
$csv->print($out,[$fields[0], $word, $fields[2]]);
}
else { #Saves the line in the new file as it was in the old one.
$csv->print($out,[$fields[0], $fields[1], $fields[2]]);
}
} else {#error procesing row
warn "La row no se ha podido procesar\n";
}
}
close $data or die "$file: $!";
close $out or die "$sal: $!";
这里声明 $validar 的那一行带来了同样的“未初始化值”错误,尽管我这样做了。
我也尝试了 push @rows, $row; 方法,但我真的不知道如何处理 $rows[$i] 因为它们是对数组(指针)的引用,我知道它们不能作为变量操作...找不到有关如何使用它们的工作示例。
【问题讨论】:
-
第 22 行是什么?输入的第 10 行是什么?
-
N° 107 9 de Julio;alberdi juan bautista 942;4724473\n N° 1078 John F. Kennedy;grandoli 4800;4729682\n N° 1080 Gabriela Mistral;san lorenzo 8154;4727581\n
-
输入是这样的......而且这些是在上层代码中引起麻烦的行 ($word) = split(/\s/,$fields[1]); if (($word ne 'SAN')&&($word ne 'LA')&&($word ne 'EL')) 这些行产生我发布的错误
-
请注意,我在输入示例中写了\n,因为我在编辑评论时无法插入换行符
-
John Kennedy 似乎没有
$fields[1],因此出现警告(这不是错误)。
标签: string perl parsing csv text-parsing