简体   繁体   English

Perl-从CSV文件读取特定行

[英]Perl - Reading Specific Lines from a CSV file

I'm looking to read a certain "category" from a .csv file that looks something like this: 我想从看起来像这样的.csv文件中读取某个“类别”:

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: 即:我要打印类别2数据,输出应如下所示:

Category 2, header1, header2, header3,...,
          , data, data, data,...,
          , data, data, data,...,
          , data, data, data,...

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 /,/ . 除非您的数据包含带引号的字段(如a,b,c,"complicated field, quoted",e,f,g ,否则使用Text::CSV而不是简单的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. 我仅使用Data::Dump来显示结果数据结构的内容。

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,...

如果该输出绝对是您想要的,那么您可以使用perl一线执行此操作:

perl -ne "$p = 0 if /^Category/;$p = 1 if /^Category 2/;print if $p;" myfile.csv

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

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