简体   繁体   English

使用Perl在列中读取具有多个值的csv文件

[英]Reading csv file with multiple values in a column with Perl

I want to delete a column from csv file using perl, for that I am splitting the line on ",", but there are some columns with multiple values and hence multiple commas. 我想使用perl从csv文件中删除一列,因为这是在“,”上拆分行,但是有些列包含多个值,因此包含多个逗号。 That is each line may have different number of commas, so its hard to delete a column that I want to be deleted. 那就是每行可能有不同数量的逗号,因此很难删除要删除的列。 Can you help in doing so? 您可以帮忙吗?

For processing CSV files, use an actual csv parser like Text::CSV . 要处理CSV文件,请使用实际的csv解析器,例如Text::CSV

This will take care of the instances of fields enclosed in quotes because they contain commas. 这将处理用引号引起来的字段实例,因为它们包含逗号。

Suppose your file looks like 假设您的文件看起来像

Tudor,Vidor,10,Hapci
Szundi,Morgo,7,Szende
Kuka,"Hofeherke, alma",100,Kiralyno
Boszorkany,Herceg,9,Meselo

and you want to find sum of 3rd column, but in 3rd row you see the 2nd column has value with comma so to handle that use Text::CSV as: 并且您想找到第三列的总和,但是在第三行中,您会看到第二列具有逗号值,以便使用Text::CSV来处理该值:

#!/usr/bin/perl
use strict;
use warnings;

use Text::CSV;
my $csv = Text::CSV->new({ sep_char => ',' });

my $file = $ARGV[0] or die "Need to get CSV file on the command line\n";

my $sum = 0;
open(my $data, '<', $file) or die "Could not open '$file' $!\n";
while (my $line = <$data>) {
  chomp $line;

  if ($csv->parse($line)) {

      my @fields = $csv->fields();
      $sum += $fields[2];

  } else {
      warn "Line could not be parsed: $line\n";
  }
}
print "$sum\n";

If you'd prefer to access the fields in your CSV file by name, use Tie:Handle::CSV 如果您希望按名称访问CSV文件中的字段,请使用Tie:Handle :: CSV

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM