# Csv help

**URL:** <https://www.ruby-forum.com/t/csv-help/121539>\
**Category:** Ruby\
**Created:** [November 28, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/csv-help/121539 "2007-11-28T19:56:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [November 28, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/csv-help/121539/1 "2007-11-28T19:56:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [November 28, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/csv-help/121539/2 "2007-11-28T20:08:34Z")

</div>

On Nov 28, 2007, at 12:55 PM, Junkone wrote:

> 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 G. II

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [November 28, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/csv-help/121539/3 "2007-11-28T20:26:13Z")

</div>

On Nov 28, 2007 1:55 PM, Junkone [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thanks for the help in advance.

If the file is always two lines and the format is always the same, I’d  
do:

data = {}  
lines = File.readlines.collect {|line| line.chomp}  
lines[0].each {|header| data[header] = lines[1].shift}

But you could use a CSV gem instead.

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [November 28, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/csv-help/121539/4 "2007-11-28T20:27:59Z")

</div>

On Nov 28, 2007 2:25 PM, Christian von Kleist [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > It there an existing api or do i have to load it as csv and then looop  
> > lines[0].each {|header| data[header] = lines[1].shift}
> 
> But you could use a CSV gem instead.

Oops, bad mistakes in that little snippet. Here we go:

data = {}  
lines = File.readlines(‘data.csv’).collect {|line|  
line.chomp.split(‘,’)}  
lines[0].each {|header| data[header] = lines[1].shift}
