Python 一元+;的操作数类型错误:';str';
我无法找出用Python 2.7编写的代码存在的问题。我正在将引用转换为int,但一元数+:'str'的类型异常一直存在。有人能帮忙吗Python 一元+;的操作数类型错误:';str';,python,operands,Python,Operands,我无法找出用Python 2.7编写的代码存在的问题。我正在将引用转换为int,但一元数+:'str'的类型异常一直存在。有人能帮忙吗 import urllib2 import time import datetime stocksToPull = 'EBAY', 'AAPL' def pullData(stock): try: print 'Currently pulling', stock print str(datetime.datetime
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)
当到达if int(splitLine[0])>int(lastUnix):
时,我遇到了一元数+:“str”的操作数异常错误操作数类型:
,即使正在比较的两个值在测试时都打印为int。有人能给我一些反馈吗?谢谢大家!
以下是异常响应:
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'`
代码对我有用。(添加缺少的语句后,除了
子句/导入
语句)
您是否在原始代码中添加了\
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'
>股票='GOOG'
>>>urlToVisithttp://chartapi.finance.yahoo.com/instrument/1.0/'
>>>+股票+'/图表数据;类型=报价;范围=5d/csv'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:一元+的操作数类型错误:“str”
顺便说一句,string(e)
应该是str(e)
你说如果int(splitLine[0])>int(lastUnix):
导致了问题,但是你实际上没有显示任何提示。
我认为这一行才是问题所在:
print 'Pulled', + stock
你明白为什么这一行会导致错误信息吗?你想要哪一个
>>> stock = "AAAA"
>>> print 'Pulled', stock
Pulled AAAA
或
不是
>打印“已提取的”+“库存”
PulledTraceback(最近一次通话最后一次):
文件“”,第1行,在
打印“已提取”+“库存”
TypeError:一元+的操作数类型错误:“str”
您要求Python将
+
符号应用于字符串,如+23
生成正23,她表示反对。感谢您的反馈falsetru,我取出了\并且仍然收到操作数错误。我想不出来。该程序可以工作,但我担心操作数错误会导致更多问题,它会在if int(splitLine[0])>int(lastUnix):
行继续触发。我会继续寻找修复方法,再次感谢!另外,我在提交后注意到我写了字符串(e),doh@heinztomato,请用完整的回溯更新您的问题。很抱歉延迟,这是完整的代码和运行程序后的响应。它似乎可以打印“pull”,但操作数问题返回而不是stock,然后循环转到下一个输入。有什么想法吗?再次感谢您的帮助。@heinztomato,删除尝试。。。除了…
,请重新运行程序,并用完整的回溯更新问题<代码>尝试。。。除了..,e:print(e)不会显示完整的回溯。不要只是捕获一个异常来打印它,因为您会丢失堆栈跟踪!请注意,如果您尝试连接在条件检查中使用的字符串,也会发生这种情况。例如,if-string1:res=string1+'moreText'
对于登录此页面的用户:如果您\u-str+=str1+str2++str3
,您将收到相同的错误。注意++
中间没有任何内容。@JoshuaBurns这就解决了我阅读这篇文章的原因。有时是一些小的语法问题让你着迷;在我的例子中,这是一个错误的逗号。谢谢你的反馈,我犯了一个严重的错误,并为这一错误向你道歉。谢谢你的帮助,我花了一段时间看错了东西(另一个不是罪魁祸首的原因是打字错误str=+'some text'
,当然应该是str+='some text'
)。
>>> 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'