一元的错误操作数类型:'str'

新手上路,请多包涵

我无法弄清楚我在使用 Python 2.7 编写的代码时遇到的问题。我正在将引用转换为整数,但我不断收到类型异常 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)

我遇到了操作数异常 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'`

原文由 heinztomato 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 700
2 个回答

你说 if int(splitLine[0]) > int(lastUnix): 引起了麻烦,但你实际上并没有表现出任何暗示。我认为这一行是问题所在:

 print 'Pulled', + stock

您明白为什么这一行会导致该错误消息吗?你想要

>>> stock = "AAAA"
>>> print 'Pulled', stock
Pulled AAAA

要么

>>> print 'Pulled ' + stock
Pulled AAAA

不是

>>> print 'Pulled', + stock
PulledTraceback (most recent call last):
  File "<ipython-input-5-7c26bb268609>", line 1, in <module>
    print 'Pulled', + stock
TypeError: bad operand type for unary +: 'str'

您要求 Python 将 + 符号应用于字符串,如 +23 得到正数 23,她反对。

原文由 DSM 发布,翻译遵循 CC BY-SA 3.0 许可协议

该代码对我有用。 (添加缺失 except 子句/ import 语句后)

你把 \ 放在原始代码中了吗?

 urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/' \
              + stock + '/chartdata;type=quote;range=5d/csv'

如果你省略它,它可能是异常的原因:

 >>> stock = 'GOOG'
>>> urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'
>>> + stock + '/chartdata;type=quote;range=5d/csv'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for unary +: 'str'

顺便说一句, string(e) 应该是 str(e)

原文由 falsetru 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题