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):
        print 'Currently pulling', stock
        print str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
        urlToVisit = '' + 
            stock + '/chartdata;type=quote;range=3y/csv'
        saveFileLine = stock + '.txt'

            readExistingData = open(saveFileLine, 'r').read()
            splitExisting = readExistingData.split('n')
            mostRecentLine = splitExisting[-2]
            lastUnix = mostRecentLine.split(',')[0]
        except Exception, e:
            print str(e)
            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'

        print 'Pulled', + stock
        print 'Sleeping....'
        print str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))

    except Exception, e:
        print 'main loop', str(e)

for eachStock in stocksToPull:

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!

here is the exception response:

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'`
Asked By: heinztomato



The code works for me. (after adding missing except clause / import statements)

Did you put in the original code?

urlToVisit = '' 
              + stock + '/chartdata;type=quote;range=5d/csv'

If you omit it, it could be a cause of the exception:

>>> stock = 'GOOG'
>>> urlToVisit = ''
>>> + 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'

BTW, string(e) should be str(e).

Answered By: falsetru

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'

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

Answered By: DSM

I had the same error message but the cause was different. A return after tripple quotation marks was the issue.

On Python 3:

print (final_sentence)

works, but not:

print (final_sentence)
Answered By: Al Martins
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.