一元+的错误操作数类型:'str' [英] Bad operand type for unary +: 'str'

查看:331
本文介绍了一元+的错误操作数类型:'str'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚使用Python 2.7编写的代码所遇到的问题.我正在将引用转换为整数,但是我不断收到类型异常bad operand type for unary +: 'str'.有人可以协助吗?

I cannot figure out a problem I am having with code written in Python 2.7. I am converting the references to ints, but I keep getting a type exception bad operand type for unary +: 'str'. Can anyone assist?

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):时,即使比较的两个值在测试时都打印为整数.谁能给我一些反馈?谢谢!

I am hitting the operand exception bad operand type for unary +: 'str' when it gets to if int(splitLine[0]) > int(lastUnix): even though both values being compared print out as ints when tested. can anyone give me some feedback? thank you!

这是异常响应:

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'`

推荐答案

您说if int(splitLine[0]) > int(lastUnix):是造成麻烦的原因,但实际上并没有显示任何暗示该问题的信息. 我认为这是问题所在:

You say that if int(splitLine[0]) > int(lastUnix): is causing the trouble, but you don't actually show anything which suggests that. I think this line is the problem instead:

print 'Pulled', + stock

您知道为什么这一行会导致该错误消息吗?您想要

Do you see why this line could cause that error message? You want either

>>> 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,而她反对.

You're asking Python to apply the + symbol to a string like +23 makes a positive 23, and she's objecting.

这篇关于一元+的错误操作数类型:'str'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆