将 csv 文件打印/导出到数组中

将 csv 文件打印/导出到数组中

我在一个非常大的 csv 中有以下数据:

"sortorder","name","description"
"1","B.1","Boiler room"
"2","1.1","First office"
"3","1.2","Second office"
etc...

我希望最终得到这样的结果:

{ name => 'B.1', description => 'Boiler room', sortorder => 1 },
{ name => '1.1', description => 'First office', sortorder => 2 },
{ name => '1.2', description => 'Second office', sortorder => 3 },
etc.

我怎样才能使用 perl 或 bash 来实现这一点?

答案1

一般的sed解决方案(不使用字段)

sed '
    1{
        h
        d
    }
    G
    :a
    s/\(^\|,\)\([^=]\+\n\)"\?\([^,]\+\)"\?,\?/\1 \3 => \2/
    ta
    s/^/{/
    s/\n/ },/
    ' file.csv

但如果你想让拳头领域成为最后一个——只需添加

s/\([^,]*\), \([^\n]*\)/\2, \1/

ta

答案2

一点红宝石单线

  ruby -rcsv -ne '
    row = CSV.parse_line($_)
    if $. == 1 then
      keys = row
    else
      h = Hash[keys.zip(row)]
      puts h.to_s + ","
    end
  '

输出

{"sortorder"=>"1", "name"=>"B.1", "description"=>"Boiler room"},
{"sortorder"=>"2", "name"=>"1.1", "description"=>"First office"},
{"sortorder"=>"3", "name"=>"1.2", "description"=>"Second office"},

或 Perl,生成完整的数据结构(哈希数组)

use strict;
use warnings;
use v5.10;

use Text::CSV;
use autodie;
use Data::Dump      qw/ dump /;

my @rows;
open my $fh, "<", shift @ARGV;
my $csv = Text::CSV->new( {binary => 1} );
my $keys = $csv->getline($fh);
while (my $row = $csv->getline($fh) ) {
    my %h;
    @h{ @$keys } = @$row;
    push @rows, \%h;
}

# print it
say dump \@rows;

输出

[
  { description => "Boiler room", name => "B.1", sortorder => 1 },
  { description => "First office", name => 1.1, sortorder => 2 },
  { description => "Second office", name => 1.2, sortorder => 3 },
]

答案3

使用perl脚本

#!/usr/bin/perl

open INFILE, "<", "inputfile";
open OUTFILE, ">", "outputfile";

$header_line = <INFILE>;
chop($header_line);
@headers = split /,/, $header_line;
foreach $header_column (@headers) {
    $header_column =~ s/^"(.*)"$/$1/;
}

while(<INFILE>) {
    chop($_); # clear the newline at the end of string
    @fields = split /,/; # split string by ,
    foreach $field (@fields) {
        $field =~ s/^"(.*)"$/$1/;
    }   
    for($i = 0; $i <= $#headers; $i++) {
        $fields[$i] = $headers[$i] . " => " . $fields[$i];
    }
    print OUTFILE "{ " . join(', ', @fields) . "},\n";

}

和你的inputfile

"sortorder","name","description"
"1","B.1","Boiler room"
"2","1.1","First office"
"3","1.2","Second office"

产生输出

{ name => 'name', description => 'description', sortorder => 'sortorder' },
{ name => 'B.1', description => 'Boiler room', sortorder => '1' },
{ name => '1.1', description => 'First office', sortorder => '2' },
{ name => '1.2', description => 'Second office', sortorder => '3' },

该脚本保持第一行开始的列顺序。

相关内容