Perl-从CSV文件读取特定行 [英] Perl - Reading Specific Lines from a CSV file

查看:244
本文介绍了Perl-从CSV文件读取特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从.csv文件中读取某种类似于以下内容的类别:

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?

即:我要打印类别2数据,输出应如下所示:

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


推荐答案

除非您数据包含带引号的字段,例如 a,b,c,复杂的字段,带引号,e,f,g ,使用 Text没有优势:: CSV 通过简单的分割/,/

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

此示例将数据转换为哈希,您可以直接直接访问该哈希。我仅使用 Data :: Dump 来显示结果数据结构的内容。

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;

输出

{
  "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", "..."],
                  ],
}






更新

如果只想分隔文件的给定部分,则无需将其放入哈希中。

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;
}

在命令行上像这样运行

get_category.pl mydata.csv 'Category 2' > cat2.csv

输出

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

这篇关于Perl-从CSV文件读取特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆