如何使用Perl从CSV文件中提取多个列
我对Perl非常陌生,希望有人能帮我解决这个问题。我需要从嵌入逗号的CSV文件中提取两列。格式如下所示:如何使用Perl从CSV文件中提取多个列,perl,csv,Perl,Csv,我对Perl非常陌生,希望有人能帮我解决这个问题。我需要从嵌入逗号的CSV文件中提取两列。格式如下所示: "ID","URL","DATE","XXID","DATE-LONGFORMAT" 我需要提取DATE列、XXID列以及紧跟在XXID之后的列。注意,每一行不一定跟随相同的列数 XXID列包含两个字母的前缀,并且不总是以相同的字母开头。它几乎可以是任何一封平静的信。长度总是一样的 最后,一旦提取了这三列,我需要对XXID列进行排序,并获得重复项的计数。您肯定希望使用CPAN库解析CSV,
"ID","URL","DATE","XXID","DATE-LONGFORMAT"
我需要提取DATE
列、XXID
列以及紧跟在XXID
之后的列。注意,每一行不一定跟随相同的列数
XXID
列包含两个字母的前缀,并且不总是以相同的字母开头。它几乎可以是任何一封平静的信。长度总是一样的
最后,一旦提取了这三列,我需要对
XXID
列进行排序,并获得重复项的计数。您肯定希望使用CPAN库解析CSV,因为您永远不会考虑格式的所有问题
请参阅:
请参阅:
但是,对于您提供的特定字符串,这里有一个非常简单且非惯用的解决方案:
use strict;
use warnings;
my $string = '"ID","URL","DATE","XXID","DATE-LONGFORMAT"';
my @words = ();
my $word = "";
my $quotec = '"';
my $quoted = 0;
foreach my $c (split //, $string)
{
if ($quoted)
{
if ($c eq $quotec)
{
$quoted = 0;
push @words, $word;
$word = "";
}
else
{
$word .= $c;
}
}
elsif ($c eq $quotec)
{
$quoted = 1;
}
}
for (my $i = 0; $i < scalar @words; ++$i)
{
print "column " . ($i + 1) . " = $words[$i]\n";
}
使用严格;
使用警告;
my$string='“ID”、“URL”、“DATE”、“XXID”、“DATE-LONGFORMAT”';
我的@words=();
我的$word=“”;
我的$quotec='”;
我的报价美元=0;
foreach my$c(拆分/,$string)
{
如果($报价)
{
如果($c eq$quotec)
{
$quoted=0;
推送@words,$word;
$word=“”;
}
其他的
{
$word.=$c;
}
}
elsif($c eq$quotec)
{
$quoted=1;
}
}
对于(my$i=0;$i
您肯定希望使用CPAN库解析CSV,因为您永远不会考虑该格式的所有问题
请参阅:
请参阅:
但是,对于您提供的特定字符串,这里有一个非常简单且非惯用的解决方案:
use strict;
use warnings;
my $string = '"ID","URL","DATE","XXID","DATE-LONGFORMAT"';
my @words = ();
my $word = "";
my $quotec = '"';
my $quoted = 0;
foreach my $c (split //, $string)
{
if ($quoted)
{
if ($c eq $quotec)
{
$quoted = 0;
push @words, $word;
$word = "";
}
else
{
$word .= $c;
}
}
elsif ($c eq $quotec)
{
$quoted = 1;
}
}
for (my $i = 0; $i < scalar @words; ++$i)
{
print "column " . ($i + 1) . " = $words[$i]\n";
}
使用严格;
使用警告;
my$string='“ID”、“URL”、“DATE”、“XXID”、“DATE-LONGFORMAT”';
我的@words=();
我的$word=“”;
我的$quotec='”;
我的报价美元=0;
foreach my$c(拆分/,$string)
{
如果($报价)
{
如果($c eq$quotec)
{
$quoted=0;
推送@words,$word;
$word=“”;
}
其他的
{
$word.=$c;
}
}
elsif($c eq$quotec)
{
$quoted=1;
}
}
对于(my$i=0;$i
下面是一个使用模块解析csv数据的示例脚本。请参阅模块的文档,以找到数据的正确设置
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;
my $csv = Text::CSV->new({ binary => 1 });
while (my $row = $csv->getline(*DATA)) {
print "Date: $row->[2]\n";
print "Col#1: $row->[3]\n";
print "Col#2: $row->[4]\n";
}
下面是一个使用模块解析csv数据的示例脚本。请参阅模块的文档,以找到数据的正确设置
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;
my $csv = Text::CSV->new({ binary => 1 });
while (my $row = $csv->getline(*DATA)) {
print "Date: $row->[2]\n";
print "Col#1: $row->[3]\n";
print "Col#2: $row->[4]\n";
}
我发布了一个名为的模块,该模块允许Perl作为本机Perl嵌套数组与CSV交互。如果您使用它,您可以采用搜索逻辑并应用它,就像您的数据已经在数组引用数组中一样。看一看
#!/usr/bin/env perl
use strict;
use warnings;
use File::Temp;
use Tie::Array::CSV;
use List::MoreUtils qw/first_index/;
use Data::Dumper;
# this builds a temporary file from DATA
# normally you would just make $file the filename
my $file = File::Temp->new;
print $file <DATA>;
#########
tie my @csv, 'Tie::Array::CSV', $file;
#find column from data in first row
my $colnum = first_index { /^\w.{6}$/ } @{$csv[0]};
print "Using column: $colnum\n";
#extract that column
my @column = map { $csv[$_][$colnum] } (0..$#csv);
#build a hash of repetitions
my %reps;
$reps{$_}++ for @column;
print Dumper \%reps;
#/usr/bin/env perl
严格使用;
使用警告;
使用File::Temp;
使用Tie::Array::CSV;
使用列表::MoreUtils qw/first_index/;
使用数据::转储程序;
#这将根据数据生成一个临时文件
#通常,您只需将$file作为文件名
my$file=file::Temp->new;
打印$file;
#########
tie my@csv,'tie::Array::csv',$file;
#从第一行的数据中查找列
my$colnum=first_index{/^\w.{6}$/}@{$csv[0]};
打印“使用列:$colnum\n”;
#提取该列
我的@column=map{$csv[$\][$colnum]}(0..$\csv);
#建立重复的散列
我的%reps;
@column的$reps{${}++;
打印转储程序\%reps;
我发布了一个名为的模块,它允许Perl作为本机Perl嵌套数组与CSV交互。如果您使用它,您可以采用搜索逻辑并应用它,就像您的数据已经在数组引用数组中一样。看一看
#!/usr/bin/env perl
use strict;
use warnings;
use File::Temp;
use Tie::Array::CSV;
use List::MoreUtils qw/first_index/;
use Data::Dumper;
# this builds a temporary file from DATA
# normally you would just make $file the filename
my $file = File::Temp->new;
print $file <DATA>;
#########
tie my @csv, 'Tie::Array::CSV', $file;
#find column from data in first row
my $colnum = first_index { /^\w.{6}$/ } @{$csv[0]};
print "Using column: $colnum\n";
#extract that column
my @column = map { $csv[$_][$colnum] } (0..$#csv);
#build a hash of repetitions
my %reps;
$reps{$_}++ for @column;
print Dumper \%reps;
#/usr/bin/env perl
严格使用;
使用警告;
使用File::Temp;
使用Tie::Array::CSV;
使用列表::MoreUtils qw/first_index/;
使用数据::转储程序;
#这将根据数据生成一个临时文件
#通常,您只需将$file作为文件名
my$file=file::Temp->new;
打印$file;
#########
tie my@csv,'tie::Array::csv',$file;
#从第一行的数据中查找列
my$colnum=first_index{/^\w.{6}$/}@{$csv[0]};
打印“使用列:$colnum\n”;
#提取该列
我的@column=map{$csv[$\][$colnum]}(0..$\csv);
#建立重复的散列
我的%reps;
@column的$reps{${}++;
打印转储程序\%reps;