将csv数据转换为DYNAMIC格式 [英] Convert csv data to specific format DYNAMIC

查看:127
本文介绍了将csv数据转换为DYNAMIC格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  secid,Initial_shares 
002826,3777
0028262,3777
0028262,3777
0028262,3777
0028262,3777
0028262,3777

我需要将此文本文件转换成以下格式。一次,我把它转换成下面的格式,我将能够显示在前端。以下格式用于显示jqgrid中的数据。

  var secid = 
[
002826 ,0028262,0028262,0028262,0028262,0028262];
var Initial_shares =
[
3777,3777,3777,3777,3777,37771
]

为了将文本转换为上述格式,我使用了以下perl代码。请注意。这种转换是静态的。即知道有多少列。在这种情况下,将有2列。 secid和initial_shares。所以这里是静态代码

  my @ a =(); 
my @ b =();

my @ a1 =();
my @ b1 =();

我的@ perl_array =();
我的@filena = split(/ \ | /,$ filename);

打开(TXT,&$ $ filename)||死无法打开$ filename:$!\\\
;
while(my $ line =< TXT>){
chomp($ line);
($ a2,$ b2)= split /,/,$ line;

push @a,(''',$ a2,'',',');
push @b,('',$ b2,'',',');
}

splice @ a,0,4; #这个拼接用于删除标题名称。
splice @ b,0,4; #即第一行data-secid,Initial_shares

push @ a1,var secid = [@ a];
push @ b1,var Initial_shares = [@ b];

push @perl_array,@ a1; @ b1;
关闭TXT;

@perl_array将完全类似于我们开始期待的数据类型。我将此perl变量转移到前端显示。



在以下情况下,我需要帮助。如果代替2列,则有5列。我们如何将相同的csv文件转换为前面提到的格式。它应该是动态的。有人可以点点光线。

解决方案

使用我给你的建议,我将如何做到这一点:

 使用strict; 
使用警告;

使用JSON;
使用Text :: CSV;

我的$ filename ='/path/to/file.csv';

我的$ csv = Text :: CSV-> new({binary => 1})或者文本:: CSV-> error_diag;
打开(我的$ fh,'<',$ filename)或死$!

$ csv-> column_names($ csv-> getline($ fh));
我的$ data = $ csv-> getline_hr_all($ fh);

close($ fh);

为我的$列($ csv-> column_names){
我的$ json = encode_json([map {$ _-> {$ column}} @ $ data]);
printvar $ column = $ json; \\\
;
}


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.

解决方案

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

这篇关于将csv数据转换为DYNAMIC格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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