Question

I have a data sample like following

Ticker,Date/Time,Conseqhigher,Conseqlower,H,L,newHigh,newLow,Prevbarlow,Prevbarhigh,uptrend,dntrend,min15low,min15high
EUR.USD-IDEALPRO-CASH,2012-02-19 17:20,1,0,1.3208,1.3198,1,1,1.3183,1.3211,0,0,1.3139,1.3153
EUR.USD-IDEALPRO-CASH,2012-02-19 17:25,0,0,1.3209,1.3198,1,0,1.3198,1.3208,0,0,1.3139,1.3153

I am trying to use faster csv to open it and process it later.

require 'fastercsv'
def query()

return FasterCSV.read("c:\\temp\\test.csv")

end

The read method does not have any headers option. How can i fix it?

Was it helpful?

Solution

You should use the standard CSV library. It's FasterCSV plus support for Ruby 1.9's m17n encoding engine.

Back to your question, there is a :headers option to access each value by the headers.

The #read method will slurp the entire file:

CSV.read(CSV_FILE_PATH, :headers => true).each do |line|
  puts line['Ticker']
end

The #foreach method is the intended primary interface for reading:

CSV.foreach(CSV_FILE_PATH, :headers => true) do |line|
  puts line['Ticker']
end

Both methods will output:

EUR.USD-IDEALPRO-CASH
EUR.USD-IDEALPRO-CASH

See http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top