如何从远程URL的长CSV的前几行中快速获得一个字符串? [英] How can I QUICKLY get a string from one of the first couple lines of a long CSV at a remote URL?

查看:128
本文介绍了如何从远程URL的长CSV的前几行中快速获得一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个作业,我使用Yahoo的股票价格系统从网上检索了几个股票价格.不幸的是,我必须使用的 Yahoo API 返回该文件显然每天包含一个交易行,对于我正在使用的股票,该行至少包含5000行,而对于其中一些股票,则至少包含1万行(

I'm working on an assignment where I retrieve several stock prices from online, using Yahoo's stock price system. Unfortunately, the Yahoo API I'm required to use returns a .csv file that apparently contains a line for every single day that stock has been traded, which is at least 5 thousand lines for the stocks I'm working with, and over 10 thousand lines for some of them (example).

不过,我只关心当前的价格,该价格在第二行.

I only care about the current price, though, which is in the second line.

我目前正在这样做:

require 'open-uri'
def get_ticker_price(stock)
   open("http://ichart.finance.yahoo.com/table.csv?s=#{stock}") do |io|
      io.read.split(',')[10].to_f
   end
end

...但是它真的很慢.

  1. 是所有延迟都来自获取文件,还是我处理文件的方式有些延迟? io.read是否正在读取整个文件?

  1. Is all the delay coming from getting the file, or is there some from the way I'm handling it? Is io.read reading the entire file?

是否可以从Yahoo CSV文件中仅下载前几行?

Is there a way to download only the first couple lines from the Yahoo CSV file?

如果对问题1和答案的回答为: 2不要使这一无关紧要,有没有一种更好的方法来处理它而不需要查看整个文件(假设io.read在做什么)?

If the answers to questions 1 & 2 don't render this one irrelevant, is there a better way to process it that doesn't require looking at the entire file (assuming that's what io.read is doing)?

推荐答案

您可以使用查询字符串参数,通过使用日期范围参数将数据减少到当前日期.

You can use query string parameters to reduce the data to the current date, by using date range parameters.

2012年7月13日的MO示例:(开始/结束月份从零索引开始,{00-11}).

example for MO on 7/13/2012: (start/end month starts w/ a zero-index, { 00 - 11 } ).

api描述: http://etraderzone.com/free-scripts/47-historical-quotes -yahoo.html

这篇关于如何从远程URL的长CSV的前几行中快速获得一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆