0

对于以下代码,我不断收到相同的 ValueError,我很难理解为什么会生成错误。据我了解,当将错误的值传递给函数时会生成此错误,但是,我真的不明白此错误告诉我什么。我花时间在网上和文档中搜索,但我无法理解我做错了什么。简单地说,为什么会产生这个错误?

我的代码:

import datetime
import ystockquote

def new_time(n):
    fmt = "%Y%m%d"
    end_date1 = datetime.datetime.strptime(n, fmt)
    sixty_day = datetime.timedelta(days=60)
    start_date  = end_date1 - sixty_day
    start_date1 = str(start_date)
    start_date2 = start_date1[:4] + start_date1[5:7] + start_date1[8:10]
    return start_date2

def average_atr():
    print "Enter your stock symbol: "
    symbol      = raw_input(" ")
    print "Enter the end date in (YYYYMMDD) format: "
    end_date    = raw_input(" ")
    start_date  = new_time(end_date)
    initial_list = ystockquote.get_historical_prices('symbol', 'start_date', 'end_date')

def start():
    average_atr()

start()

这是 ystockquote 的相关代码:

def get_historical_prices(symbol, start_date, end_date):
    """
    Get historical prices for the given ticker symbol.
    Date format is 'YYYYMMDD'

    Returns a nested list.
    """
    url = 'http://ichart.yahoo.com/table.csv?s=%s&' % symbol + \
          'd=%s&' % str(int(end_date[4:6]) - 1) + \
          'e=%s&' % str(int(end_date[6:8])) + \
          'f=%s&' % str(int(end_date[0:4])) + \
          'g=d&' + \
          'a=%s&' % str(int(start_date[4:6]) - 1) + \
          'b=%s&' % str(int(start_date[6:8])) + \
          'c=%s&' % str(int(start_date[0:4])) + \
          'ignore=.csv'
    days = urllib.urlopen(url).readlines()
    data = [day[:-2].split(',') for day in days]
    return data

请注意,上面的 ystockquote 代码不是完整的代码。

4

2 回答 2

6

在函数average_atr()中,更改以下行:

initial_list = ystockquote.get_historical_prices('symbol', 'start_date', 'end_date')

到:

initial_list = ystockquote.get_historical_prices(symbol, start_date, end_date)

在您当前的版本中,您不是将变量传递给 ,而是传递ystockquote.get_historical_prices()带有变量名称的文字字符串。这导致str(int(end_date[4:6]) - 1)变量end_date具有值'end_date',并且'end_date'[4:6]'da'

于 2012-08-06T17:44:35.387 回答
2

您将字符串“start_date”和“end_date”发送到函数 get_historical_prices。似乎该函数期望传入实际的字符串日期值。只需删除此行中参数周围的引号即可:

initial_list = ystockquote.get_historical_prices('symbol', 'start_date', 'end_date')
于 2012-08-06T17:46:40.657 回答