Python ValueError:以10为基数的int()的文本无效:';da';
我一直在为下面的代码获取相同的ValueError,我很难理解为什么会生成错误。我的理解是,这个错误是在错误的值被传递到函数中时产生的,但是,我并不真正理解这个错误告诉了我什么。我花时间在网上和文档中搜索,但我无法理解我做错了什么。简单地说,为什么会产生这个错误 我的代码:Python ValueError:以10为基数的int()的文本无效:';da';,python,python-2.7,Python,Python 2.7,我一直在为下面的代码获取相同的ValueError,我很难理解为什么会生成错误。我的理解是,这个错误是在错误的值被传递到函数中时产生的,但是,我并不真正理解这个错误告诉了我什么。我花时间在网上和文档中搜索,但我无法理解我做错了什么。简单地说,为什么会产生这个错误 我的代码: import datetime import ystockquote def new_time(n): fmt = "%Y%m%d" end_date1 = datetime.datetime.strpti
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代码不是完整的代码。在函数
average\u atr()
中,更改以下行:
initial_list = ystockquote.get_historical_prices('symbol', 'start_date', 'end_date')
致:
在当前版本中,不是将变量传递到
ystockquote.get_historical_prices()
,而是传递带有变量名的文本字符串。这将导致str(int(end\u date[4:6])-1)
,变量end\u date
的值为'end\u date'
,而'end\u date'[4:6]
是'da'您将字符串“start\u date”和“end\u date”发送到函数get\u historical\u prices中。
函数似乎希望传入实际的字符串日期值。只需删除此行中参数周围的引号:
initial_list = ystockquote.get_historical_prices('symbol', 'start_date', 'end_date')
您在哪一行收到错误?错误表示您试图调用int('da')
,即start\u date
或end\u date
的格式错误。试着把它们打印出来或运行在pdb
中,看看它们到底是什么。谢谢。我觉得自己像个傻瓜,但它现在起作用了。非常感谢,谢谢。我应该注意到这一点。
initial_list = ystockquote.get_historical_prices('symbol', 'start_date', 'end_date')