Bad operand type for unary +: 'str' 对于一元操作符+,操作数类型错误:'str'

7 浏览
0 Comments

Bad operand type for unary +: 'str' 对于一元操作符+,操作数类型错误:'str'

我无法解决我在Python 2.7代码中遇到的问题。我正在将引用转换为整数,但是我一直收到类型异常bad operand type for unary +: 'str'。有人能帮忙吗?

import urllib2
import time
import datetime
stocksToPull = 'EBAY', 'AAPL'
def pullData(stock):
    try:
        print '当前正在获取', 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 '已获取', + stock
        print '正在睡眠....'
        print str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
        time.sleep(120)
    except Exception, e:
        print '主循环', str(e)
for eachStock in stocksToPull:
    pullData(eachStock)

当它到达if int(splitLine[0]) > int(lastUnix):时,我遇到了操作数异常bad operand type for unary +: 'str',即使在测试时,被比较的两个值都显示为整数。有人能给我一些反馈吗?谢谢!

以下是异常响应:

当前正在获取EBAY
2013-12-21 11:32:40
已获取主循环bad operand type for unary +: 'str'
当前正在获取AAPL
2013-12-21 11:32:41
已获取主循环bad operand type for unary +: 'str'

0