我正在尝试阅读csv,从csv abc.csv
中选择一些列并创建一个新的csv def.csv
。以下是我的代码(片段): -
genNewCsv();
sub genNewCsv {
my $csv = Text::CSV->new();
my $aCsv = "abc.csv"
my $mCsv = "def.csv";
my $fh = FileHandle->new( $aCsv, "r" );
my $nf = FileHandle->new($mCsv,"w");
$csv->print($nf,[ "id","flops""ub" ]);
while (my $row = $csv->getline($fh)){
my $id = $row->[0];
my $flops = $row->[2];
my $ub = "TRUE";
$csv->print($nf,[ $id,$flops,$ub]);
}
$nf->close();
$fh->close();
}
当我运行此代码时,在def.csv
文件中,我得到的行没有中断,即
id,flop,ub,ASM_1,in,TRUE,ASM_2,out,TRUE
我希望csv在另一行之后是一行,即
id,flop,ub
ASM_1,in,TRUE
ASM_2,out,TRUE
任何人都可以帮助我。
答案 0 :(得分:3)
BINGO
我应该使用这个
my $csv = Text::CSV->new ({ binary => 1, eol => $/ });