问题
I have this front end CSS data stored in a web page. Here is the data in text file.
secid,Initial_shares
002826,3777
0028262,3777
0028262,3777
0028262,3777
0028262,3777
0028262,3777
I need to convert this text file into the below format. once, i convert it to below format, i will be able to display it on the front end. this format below is used to display data in jqgrid.
var secid =
[
"002826", "0028262", "0028262", "0028262", "0028262", "0028262"];
var Initial_shares =
[
"3777", "3777", "3777", "3777", "3777", "3777"1
];
In order to convert the text to above format, i have used the below perl code. Please note. This conversion is static. i.e. it knows how many columns are there. In this case, there will be 2 columns. secid and Initial_shares. so here is the static code
my @a=();
my @b=();
my @a1=();
my @b1=();
my @perl_array=();
my @filena = split(/\|/, $filename);
open (TXT, "<$filename") || die "Can't open $filename: $!\n";
while (my $line=<TXT>) {
chomp($line);
($a2, $b2) = split /,/, $line;
push @a,('"', "$a2", '"', ',');
push @b,('"', "$b2", '"', ',');
}
splice @a,0,4; # this splice is used to remove the header name.
splice @b,0,4; # i.e. first row data- secid, Initial_shares
push @a1,"var secid=[@a]";
push @b1,"var Initial_shares=[@b]";
push @perl_array, "@a1; @b1";
close TXT;
The @perl_array will be then exactly similar to the kind of data we were expecting at the start. i wil transfer this perl variable to front end for displaying then.
I need help in the following case. What if instead of 2 columns, there are 5 columns. How can we convert the same csv file to the format mentioned earlier. it should be all dynamic. Can someone shed some light please.
回答1:
Here's how I would have done it, using the suggestions I gave you:
use strict;
use warnings;
use JSON;
use Text::CSV;
my $filename = '/path/to/file.csv';
my $csv = Text::CSV->new({ binary => 1 }) or die Text::CSV->error_diag;
open(my $fh, '<', $filename) or die $!;
$csv->column_names($csv->getline($fh));
my $data = $csv->getline_hr_all($fh);
close($fh);
for my $column ($csv->column_names) {
my $json = encode_json([ map { $_->{$column} } @$data ]);
print "var $column = $json;\n";
}
回答2:
The idea here is to process the header line on its on first, then base don that you can create a hash of arrays which map to each header field. below is a sample code to demonstrate.
use strict;
use warnings;
use Data::Dumper;
my %dynamic;
my @fields = split(/,/,<DATA>);
chomp(@fields);
while(<DATA>){
chomp();
my @data_fields=split(/,/);
for (my $i=0; $i<@fields; $i++){
push(@{$dynamic{$fields[$i]}}, '"' . $data_fields[$i] . '"');
}
}
my @data_array;
foreach (@fields){
push(@data_array, "var $_ = [" . join(',',@{$dynamic{$_}}) . "];");
}
print join("\n",@data_array), "\n";
__DATA__
secid,Initial_shares,columna,someothercolumn
002826,3777,1,2
0028262,3777,a,b
0028262,3777,5,6
0028262,3777,g,h
0028262,3777,4,5
0028262,3777,h,j
I have printed out the data structure of the final array to show its contents.
var secid = ["002826","0028262","0028262","0028262","0028262","0028262"];
var Initial_shares = ["3777","3777","3777","3777","3777","3777"];
var columna = ["1","a","5","g","4","h"];
var someothercolumn = ["2","b","6","h","5","j"];
this will dynamically expand or shrink based on your csv input.
回答3:
This can be done very simply by reading the file into an array and reformatting it on output
This program expects the path to the input file as a parameter on the command line
use strict;
use warnings;
my @data;
while ( <> ) {
my @row = /[^,\s]+/g;
push @{ $data[$_] }, $row[$_] for 0 .. $#row;
}
for my $i ( 0 .. $#data ) {
my $column = $data[$i];
printf "var %s = [\n", shift @$column;
printf " %s\n", join ', ', map qq{"$_"}, @$column;
print "];\n";
}
output
var secid = [
"002826", "0028262", "0028262", "0028262", "0028262", "0028262"
];
var Initial_shares = [
"3777", "3777", "3777", "3777", "3777", "3777"
];
来源:https://stackoverflow.com/questions/32191842/convert-csv-data-to-specific-format-dynamic