How do I efficiently parse a CSV file in Perl?

后端 未结 6 1904
独厮守ぢ
独厮守ぢ 2020-11-27 19:19

I\'m working on a project that involves parsing a large csv formatted file in Perl and am looking to make things more efficient.

My approach has been to split(

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 20:15

    Here is a version that also respects quotes (e.g. foo,bar,"baz,quux",123 -> "foo", "bar", "baz,quux", "123").

    sub csvsplit {
            my $line = shift;
            my $sep = (shift or ',');
    
            return () unless $line;
    
            my @cells;
            $line =~ s/\r?\n$//;
    
            my $re = qr/(?:^|$sep)(?:"([^"]*)"|([^$sep]*))/;
    
            while($line =~ /$re/g) {
                    my $value = defined $1 ? $1 : $2;
                    push @cells, (defined $value ? $value : '');
            }
    
            return @cells;
    }
    

    Use it like this:

    while(my $line = ) {
        my @cells = csvsplit($line); # or csvsplit($line, $my_custom_seperator)
    }
    

提交回复
热议问题