2012-08-06 124 views
0

对于下面的代码,我总是得到相同的ValueError,并且我很难理解为什么会生成错误。我的理解是,当错误的值被传递给一个函数时会产生这个错误,但是,我真的不明白这个错误告诉我什么。我花了时间在网上和文档中搜索,但我无法理解我做错了什么。简单地说,为什么会产生这个错误?ValueError:对于int()以10为基数的无效字面值:'da'

我的代码:

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代码是不完整的代码。

+1

你得到的错误是哪一行? – murgatroid99 2012-08-06 17:42:59

+0

错误意味着你正试图调用int('da')',即'start_date'或'end_date'格式错误。尝试将它们打印出来或在'pdb'中运行以查看它们实际是什么。 – Dougal 2012-08-06 17:45:07

回答

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'

+0

谢谢。我觉得自己像个putz,但现在正在工作。非常感谢。 – TDL 2012-08-06 18:00:28

2

您正在向函数get_historical_prices发送字符串'start_date'和'end_date'。 看来函数期望实际的字符串日期值被传入。只要删除在这条线的参数周围的引号:

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

谢谢。我应该注意到了。 – TDL 2012-08-06 18:10:30

相关问题