我在为解析 CSV 文件而编写的 Perl 脚本中做错了什么?
我有两个脚本正在尝试使用 CSV_XS。首先,我对所有内容进行了硬编码:源目录、文件名和我想要查找的 csv 分隔符。该脚本效果很好。然而,在第二个例子中,我尝试尽可能多地动态发现。该脚本似乎运行了,但没有输出任何内容。
我很难弄清楚为什么,我希望 Perl 的好朋友们不会介意用第二双眼睛来解决这个问题:
首先,成功的脚本:
#!/usr/bin/perl -w
use Text::CSV_XS;
my @records;
my $file = 'Data/space.txt';
my $csv=Text::CSV_XS->new({ sep_char => " " });
open(FILE,$file) || die "Couldn't open $file: $!\n";
while (<FILE>){
$csv->parse($_);
push(@records,[$csv->fields]);
}
close FILE;
foreach (@records){
print $_->[0], ",", $_->[1], ",", $_->[2], ",", $_->[3], ",", $_->[4], "\n";
}
其次,“失败”的脚本:
#!/usr/bin/perl -w
use Text::CSV_XS;
$input_dir = $ARGV[0]; #I pass "Data" on the command line
my @records;
opendir(DIR, $input_dir) || die "cannot open dir $input_dir: $!";
my @filelist = grep {$_ ne '.' && $_ ne '..'} readdir DIR;
closedir DIR;
foreach $file (@filelist){
print "Input file='",$input_dir,"/",$file,"'\n";
if ($file =~ /comma/) {$sep=','}
elsif ($file =~ /pipe/) {$sep='|'}
elsif ($file =~ /space/) {$sep=' '}
else {die "Cannot identify separator in $file: $!";}
print "Delimiter='",$sep,"'\n";
open(FILE,$input_dir||"/"||$file) || die "Couldn't open $file: $!\n";
my $csv=Text::CSV_XS->new({ sep_char => $sep });
while (<FILE>){
$csv->parse( $_ );
push(@records,[$csv->fields]);
print "File Input Line:'", $_ ,$csv->fields,"'\n";
};
close FILE;
}
foreach $record (@records){
print $record->[0], ",", $record->[1], ",", $record->[2], ",", $record->[3], ",", $record->[4], "\n";
}
I have two scripts in which I'm experimenting with CSV_XS. In the first, I hard-coded everything: source directory, filename, and the csv delimiter I wanted to look for. The script works great. In the second, however, I try to dynamically discover as much as possible. That script seems to run, but it outputs nothing.
I'm having trouble figuring out why, and I was hoping you fine Perl folks wouldn't mind lending a second set of eyes to the problem:
First, the successful script:
#!/usr/bin/perl -w
use Text::CSV_XS;
my @records;
my $file = 'Data/space.txt';
my $csv=Text::CSV_XS->new({ sep_char => " " });
open(FILE,$file) || die "Couldn't open $file: $!\n";
while (<FILE>){
$csv->parse($_);
push(@records,[$csv->fields]);
}
close FILE;
foreach (@records){
print $_->[0], ",", $_->[1], ",", $_->[2], ",", $_->[3], ",", $_->[4], "\n";
}
And second, the "failing" script:
#!/usr/bin/perl -w
use Text::CSV_XS;
$input_dir = $ARGV[0]; #I pass "Data" on the command line
my @records;
opendir(DIR, $input_dir) || die "cannot open dir $input_dir: $!";
my @filelist = grep {$_ ne '.' && $_ ne '..'} readdir DIR;
closedir DIR;
foreach $file (@filelist){
print "Input file='",$input_dir,"/",$file,"'\n";
if ($file =~ /comma/) {$sep=','}
elsif ($file =~ /pipe/) {$sep='|'}
elsif ($file =~ /space/) {$sep=' '}
else {die "Cannot identify separator in $file: $!";}
print "Delimiter='",$sep,"'\n";
open(FILE,$input_dir||"/"||$file) || die "Couldn't open $file: $!\n";
my $csv=Text::CSV_XS->new({ sep_char => $sep });
while (<FILE>){
$csv->parse( $_ );
push(@records,[$csv->fields]);
print "File Input Line:'", $_ ,$csv->fields,"'\n";
};
close FILE;
}
foreach $record (@records){
print $record->[0], ",", $record->[1], ",", $record->[2], ",", $record->[3], ",", $record->[4], "\n";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这行看起来有点可疑:
我认为你不想把那些
||
放在那里。它的作用是检查$input_dir
是否为真,如果不是,则检查"/"
是否为真(它总是如此) 。您的$input_dir
可能始终为 true,因此您只需打开$input_dir
。您应该使用
File::Spec
来创建完全限定的文件:这将在适当的位置放置
/
来“做正确的事情”(或者,如果您'在 Windows 上,\
)。然后将其传递给您的open()
命令。此外,您应该使用词法文件句柄和目录句柄,以及三个选项
open()
:词法文件句柄更安全,因为它们不能被定义
的其他模块覆盖。 >FILE
文件句柄。三选项open()
更容易理解,并且当您的文件名包含>
或<
时不易出错> 或|
。如果您想真正疯狂,请将
use autodie;
放在顶部,这样您甚至不必检查open()
或的返回值>opendir()
:This line looks kind of suspect:
I don't think you want to put those
||
in there. What that does is check to see if$input_dir
is true, then if it isn't, it check to see if"/"
is true (which it always is). Your$input_dir
is likely always true, so you're just opening the$input_dir
.You should be using
File::Spec
to create your fully-qualified files:This will "do the right thing" in putting a
/
where appropriate (or, if you're on Windows,\
). Then pass that in to youropen()
command.Further, you should be using lexical filehandles and directory handles, along with the three-option
open()
:Lexical filehandles are much safer, as they can't get overridden by some other module defining a
FILE
filehandle. Three-optionopen()
is easier to understand and isn't prone to error when you have a filename that has a>
or<
or|
in it.If you want to get really crazy, put
use autodie;
at the top, so you don't even have to check for the return value ofopen()
oropendir()
: