将csv数据转换为特定的格式动态

时间:2021-08-23 17:34:33

I have this front end CSS data stored in a web page. Here is the data in text file.

我有这个前端CSS数据存储在网页中。这是文本文件中的数据。

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.

我需要把这个文本文件转换成下面的格式。一旦我将它转换为下面的格式,我将能够在前端显示它。下面的格式用于在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

为了将文本转换为上述格式,我使用了下面的perl代码。请注意。这种转换是静态的。它知道有多少列。在这种情况下,有两列。secid Initial_shares。这是静态代码

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.

然后,@perl_array将与我们在开始时预期的数据类型完全相似。我将把这个perl变量转移到前端显示。

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.

在下列情况下我需要帮助。如果不是2列,而是5列。如何将相同的csv文件转换为前面提到的格式。它应该是动态的。有人能弄点光吗?

3 个解决方案

#1


2  

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


1  

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.

这里的想法是先处理其上的标题行,然后根据don创建映射到每个标题字段的数组散列。下面是要演示的示例代码。

    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.

这将根据您的csv输入动态地展开或收缩。

#3


1  

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"
];

#1


2  

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


1  

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.

这里的想法是先处理其上的标题行,然后根据don创建映射到每个标题字段的数组散列。下面是要演示的示例代码。

    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.

这将根据您的csv输入动态地展开或收缩。

#3


1  

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"
];