Skip to content

Instantly share code, notes, and snippets.

@soardex
Created April 24, 2014 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soardex/11248336 to your computer and use it in GitHub Desktop.
Save soardex/11248336 to your computer and use it in GitHub Desktop.
CVS to JSON and outputs as file.
use strict;
use warnings;
use JSON;
my $file = 'table_YHOO.csv';
my @data;
open(my $fh, '<', $file) or die "Can't read '$file' [$!]\n";
while (my $line = <$fh>) {
chomp $line;
my @fields = split(/,/, $line);
push @data, \@fields;
}
my $json = JSON->new->utf8;
my $size = 500;
my @inner_hash;
for (my $i = 1; $i <= $size; $i++) {
my $str->{'date'} = $data[$i][0];
$str->{'open'} = $data[$i][1];
$str->{'high'} = $data[$i][2];
$str->{'low'} = $data[$i][3];
$str->{'close'} = $data[$i][4];
$str->{'volume'} = $data[$i][5];
$str->{'adclose'} = $data[$i][6];
push @inner_hash, $str;
}
my $hash->{'count'} = $size;
$hash->{'records'} = \@inner_hash;
my $out_file = 'market-info.json';
open(my $fw, '>', $out_file) or die "Can't create '$file' [$!]\n";
print $fw $json->encode($hash) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment