Python 一元 + 的错误操作数类型:'str'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20591385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bad operand type for unary +: 'str'
提问by heinztomato
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?
我无法弄清楚我在使用 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)
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!
我打的操作异常bad operand type for unary +: 'str'当它到达if int(splitLine[0]) > int(lastUnix):,即使测试时,被比较的两个值打印出整数。谁能给我一些反馈?谢谢你!
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'`
采纳答案by DSM
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:
你说这if int(splitLine[0]) > int(lastUnix):造成了麻烦,但你实际上并没有表现出任何暗示这一点的东西。我认为这一行是问题所在:
print 'Pulled', + stock
Do you see why this line could cause that error message? You want either
你明白为什么这条线会导致那个错误信息吗?你想要
>>> stock = "AAAA"
>>> print 'Pulled', stock
Pulled AAAA
or
或者
>>> print 'Pulled ' + stock
Pulled AAAA
not
不是
>>> 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 +23makes a positive 23, and she's objecting.
您要求 Python 将+符号应用于字符串,例如+23使正数 23,而她反对。
回答by falsetru
The code works for me. (after adding missing exceptclause / importstatements)
该代码对我有用。(添加缺少的except子句/import语句后)
Did you put \in the original code?
你\输入原代码了吗?
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/' \
+ stock + '/chartdata;type=quote;range=5d/csv'
If you omit it, it could be a cause of the exception:
如果省略它,则可能是导致异常的原因:
>>> 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'
BTW, string(e)should be str(e).
顺便说一句,string(e)应该是str(e)。

