我找不出用Python 2.7編寫的代碼時遇到的問題。我正在將引用轉換爲整數,但我一直得到一個類型異常bad operand type for unary +: 'str'
。任何人都可以協助一元+的錯誤操作數類型:'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)
我打的操作異常bad operand type for unary +: 'str'
當它到達if int(splitLine[0]) > int(lastUnix):
即使測試時,被比較的兩個值打印出整數。任何人都可以給我一些反饋?謝謝!
這裏是異常響應:
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'`
不要只是捕獲一個異常來打印它,因爲你失去了堆棧跟蹤! – Eric