1

I am taking a symbol and requesting Yahoo Finance web service to get the data for that symbol(company).

It is returning a CSV::Table when I do the following operation:

symbols_list.each{|symbol| 
                table= CSV.parse open("http://ichart.finance.yahoo.com/table.csv?s=#{URI.encode(symbol)}").read, :headers=>true, :converters=>:numeric

The output is a CSV::Table with headers for each row. However, I want to extract only the first row from the results and get a part of that row.

I have tried using table.push but it is printing up all the rows instead of one.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • What do you mean by 'only the first row" and "part of that row"? Have you tried opening up your terminal and seeing what values you can get from it? – Farley Knight Oct 02 '13 at 00:50

1 Answers1

2

This is what I get, messing around on the command line:

[16] pry(main)> table= CSV.parse open("http://ichart.finance.yahoo.com/table.csv?s=GOOG").read, :headers=>true, :converters=>:numeric
=> #<CSV::Table mode:col_or_row row_count:2297>
[17] pry(main)> table.first
=> #<CSV::Row "Date":"2013-10-01" "Open":880.25 "High":887.67 "Low":880.05 "Close":887.0 "Volume":1684800 "Adj Close":887.0>
[18] pry(main)> table.first.first
=> ["Date", "2013-10-01"]

To get the closing price:

[23] pry(main)> table.first["Close"]
=> 887.0
Farley Knight
  • 1,795
  • 1
  • 13
  • 17
  • I want to extract the closing price i.e the "Close" from the first row. How should I? – Veerendra Manikonda Oct 02 '13 at 00:57
  • That's just the answer I was going to give! As an additional note, [this previous Q&A](http://stackoverflow.com/questions/11497722/how-can-i-quickly-get-a-string-from-one-of-the-first-couple-lines-of-a-long-csv?rq=1) indicates how to just query today's data so you don't have to download thousands of lines for each ticker symbol. – Ken Y-N Oct 02 '13 at 00:58
  • Here's what rubocop has to say: `The use of Kernel#open is a serious security risk.` What is the proper way to read a csv file into a CSV::Table? – Damion Gomez Apr 19 '19 at 18:10
  • Here's what I used: `table = CSV.table('filepath.csv', headers: true)` – Damion Gomez Apr 19 '19 at 18:20