问题
I'm looking to read a certain "category" from a .csv file that looks something like this:
Category 1, header1, header2, header3,...,
, data, data, data,...,
, data, data, data,...,
, data, data, data,...,
Category 2, header1, header2, header3,...,
, data, data, data,...,
, data, data, data,...,
, data, data, data,...,
Category 3, header1, header2, header3,...,
, data, data, data,...,
, data, data, data,...,
, data, data, data,...
Let's say I wanted to print only the data from a specific "category"... how would I go about doing this?
ie: I want to print Category 2 data, the output should look like:
Category 2, header1, header2, header3,...,
, data, data, data,...,
, data, data, data,...,
, data, data, data,...
回答1:
Unless your data includes quoted fields, like a,b,c,"complicated field, quoted",e,f,g
there is no advantage in using Text::CSV
over a simple split /,/
.
This example categorizes the data into a hash that you can access simply and directly. I have used Data::Dump
only to show the contents of the resulting data structure.
use strict;
use warnings;
use autodie;
open my $fh, '<', 'mydata.csv';
my $category;
my %data;
while (<$fh>) {
chomp;
my @data = split /,/;
my $cat = shift @data;
$category = $cat if $cat =~ /\S/;
push @{ $data{$category} }, \@data;
}
use Data::Dumper;
$Data::Dumper::Useqq = 1;
print Dumper \%data;
output
{
"Category 1" => [
[" header1", " header2", " header3", "..."],
[" data", " data", " data", "..."],
[" data", " data", " data", "..."],
[" data", " data", " data", "..."],
],
"Category 2" => [
[" header1", " header2", " header3", "..."],
[" data", " data", " data", "..."],
[" data", " data", " data", "..."],
[" data", " data", " data", "..."],
],
"Category 3" => [
[" header1", " header2", " header3", "..."],
[" data", " data", " data", "..."],
[" data", " data", " data", "..."],
[" data", " data", " data", "..."],
],
}
Update
If all you want is to separate a given section of the file then there is no need to put it into a hash. This program will do what you want.
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
my ($file, $wanted) = @ARGV;
open my $fh, '<', $file;
my $category;
while (<$fh>) {
my ($cat) = /\A([^,]*)/;
$category = $cat if $cat =~ /\S/;
print if $category eq $wanted;
}
Run it like this on the command line
get_category.pl mydata.csv 'Category 2' > cat2.csv
output
Category 2, header1, header2, header3,...,
, data, data, data,...,
, data, data, data,...,
, data, data, data,...
回答2:
If that output is definitely what you want, then you could do this with a perl one-liner:
perl -ne "$p = 0 if /^Category/;$p = 1 if /^Category 2/;print if $p;" myfile.csv
来源:https://stackoverflow.com/questions/24041045/perl-reading-specific-lines-from-a-csv-file