我想不出用Python2.7编写的代码有什么问题。我正在将引用转换为int,但是我一直得到一个类型异常bad operand type for unary +: 'str'
。有人能帮忙吗?
import urllib2
import time
import datetime
stocksToPull = 'EBAY', 'AAPL'
def pullData(stock):
try:
print 'Currently pulling', stock
print str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/' + \
stock + '/chartdata;type=quote;range=3y/csv'
saveFileLine = stock + '.txt'
try:
readExistingData = open(saveFileLine, 'r').read()
splitExisting = readExistingData.split('\n')
mostRecentLine = splitExisting[-2]
lastUnix = mostRecentLine.split(',')[0]
except Exception, e:
print str(e)
time.sleep(1)
lastUnix = 0
saveFile = open(saveFileLine, 'a')
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
if 'values' not in eachLine:
splitLine = eachLine.split(',')
if len(splitLine) == 6:
if int(splitLine[0]) > int(lastUnix):
lineToWrite = eachLine + '\n'
saveFile.write(lineToWrite)
saveFile.close()
print 'Pulled', + stock
print 'Sleeping....'
print str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
time.sleep(120)
except Exception, e:
print 'main loop', str(e)
for eachStock in stocksToPull:
pullData(eachStock)
当操作数异常到达if int(splitLine[0]) > int(lastUnix):
时,我正在命中操作数异常bad operand type for unary +: 'str'
,即使两个被比较的值在测试时都打印为int。有人能给我一些反馈吗?谢谢您!
以下是异常响应:
Currently pulling EBAY
2013-12-21 11:32:40
Pulled main loop bad operand type for unary +: 'str'
Currently pulling AAPL
2013-12-21 11:32:41
Pulled main loop bad operand type for unary +: 'str'`
代码对我有效。(在添加缺少的
except
子句/import
语句之后)你把
\
放进原始代码了吗?如果省略,则可能是异常的原因:
顺便说一下,
string(e)
应该是str(e)
。你说是
if int(splitLine[0]) > int(lastUnix):
引起了麻烦,但实际上你没有表现出任何暗示。 我认为这一行是问题所在:你明白为什么这一行会导致错误信息吗?你也要
或者
不是
你要求Python将
+
符号应用于一个字符串,比如+23
使23为正,而她反对。相关问题 更多 >
编程相关推荐