csv help

J

Junkone

hello
Here is a extract of csv file that i use
Ticker,Trade,Date,Price
FLWS,Long,01/08/2007 2:00:00 PM,9.05

It will always have only 2 rows. How can i load it as a hash so that i
get access it like
hashvar["Ticker"] .....
It there an existing api or do i have to load it as csv and then looop
thro the array and create the hash.

thanks for the help in advance.
 
J

James Edward Gray II

hello
Here is a extract of csv file that i use
Ticker,Trade,Date,Price
FLWS,Long,01/08/2007 2:00:00 PM,9.05

It will always have only 2 rows. How can i load it as a hash so that i
get access it like
hashvar["Ticker"] .....
It there an existing api or do i have to load it as csv and then looop
thro the array and create the hash.

FasterCSV can do it for you:

#!/usr/bin/env ruby -wKU

require "rubygems"
require "faster_csv"

csv = <<END_CSV
Ticker,Trade,Date,Price
FLWS,Long,01/08/2007 2:00:00 PM,9.05
END_CSV

data = FCSV.parse(csv, :headers => true)[0].to_hash
p data
# >> {"Trade"=>"Long", "Date"=>"01/08/2007 2:00:00 PM",
"Price"=>"9.05", "Ticker"=>"FLWS"}

__END__

Hope that helps.


James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top