【发布时间】:2014-04-29 09:00:38
【问题描述】:
我有一个 python 函数getstock(),它可以从 yahoo 中提取随机公司的市场数据进行分析。有时,在脚本中的某个特定点,如果我的函数进入了 Yahoo Finance 无法识别的公司,我会收到以下错误:
Traceback (most recent call last):
File "<pyshell#65>", line 8, in <module>
stockhistory=pandas.io.data.get_data_yahoo(stock, start=datetime(1900,1,1), end=datetime(2014,1,1))
File "/usr/lib/python2.7/dist-packages/pandas/io/data.py", line 405, in get_data_yahoo
adjust_price, ret_index, chunksize, 'yahoo', name)
File "/usr/lib/python2.7/dist-packages/pandas/io/data.py", line 351, in _get_data_from
hist_data = src_fn(symbols, start, end, retry_count, pause)
File "/usr/lib/python2.7/dist-packages/pandas/io/data.py", line 200, in _get_hist_yahoo
return _retry_read_url(url, retry_count, pause, 'Yahoo!')
File "/usr/lib/python2.7/dist-packages/pandas/io/data.py", line 177, in _retry_read_url
"return a 200 for url %r" % (retry_count, name, url))
IOError: after 3 tries, Yahoo! did not return a 200 for url 'http://ichart.finance.yahoo.com/table.csv?s=LE&a=0&b=1&c=1900&d=0&e=1&f=2014&g=d&ignore=.csv'
这可以通过使用不同的公司来解决(再次调用该函数)。
我的问题是:我如何编写一个有效地表示“如果发生上述错误,请再次运行函数 getstock()”的 if 语句。
【问题讨论】:
标签: python function if-statement error-handling