Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Simplified NYSE stock price extraction script

by Scott7477 (Chaplain)
on May 31, 2007 at 22:38 UTC ( #618597=CUFP: print w/replies, xml ) Need Help??

This script is an improved version of the code I posted here:
use strict; use Text::CSV::Simple; use DBI; use DBD::SQLite; #usage NYSE091806.csv 091806 my $filename = shift; my $day= shift; my $tmpfile = "nyse.tmp"; open my $out, '>', $tmpfile or die "Couldn't open $tmpfile for writing: $!"; my $parser = Text::CSV::Simple->new(); $parser->want_fields(1, 2, 3, 4, 5, 8); my @data = $parser->read_file($filename); my $datalines = @data; for my $row(@data) { my @line = @$row; foreach my $row1(@line) { print {$out} $row1."\t"; } print {$out} "\n"; } close $out or die "Couldn't close $tmpfile: $!"; #This section eliminates certain unwanted lines open(DATA, $tmpfile) || die("can't open $tmpfile: $!"); while (my $lines = <DATA> ) { chomp($lines); if (substr($lines,0,1) ne "\s") { if (substr($lines,0,4) ne "Symb") { if ( not $lines =~ m/\S/msx ) { next; } print {$out} $lines."\n"; } } } close DATA; #This inserts the extracted data into a SQLite database my $datapath = "c:/sqlite3/nyse.db"; open(DATA, $tmpfile) || die("can't open $tmpfile: $!"); my $dbh = DBI->connect("dbi:SQLite:c:/sqlite3/nyse.db","",""); my $sth = $dbh->prepare("INSERT INTO DATA(Symbol, Open,High, Low, Clos +e, Volume, Date) VALUES (?, ?, ?, ?, ?, ?, ?)"); while(<DATA>) { chomp; my ($sym,$opn,$hi,$lo,$cls,$vol) = split /\t/; my $rc=$sth->execute($sym,$opn,$hi,$lo,$cls,$vol,$day); } $sth->finish; $dbh->disconnect; close DATA; unlink $tmpfile; #cleans up the temp file

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://618597]
Approved by planetscape
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2021-06-19 21:50 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (93 votes). Check out past polls.