2013-12-15 43 views
13

我找不出用Python 2.7編寫的代碼時遇到的問題。我正在將引用轉換爲整數,但我一直得到一個類型異常bad operand type for unary +: 'str'。任何人都可以協助一元+的錯誤操作數類型:'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'` 
+2

不要只是捕獲一個異常來打印它,因爲你失去了堆棧跟蹤! – Eric

回答

16

你說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,她的反對。

+0

感謝您的反饋,我的錯誤的失誤和道歉與這一個誤導。感謝你的幫助,我花了一段時間看錯了東西! :( – heinztomato

2

的代碼爲我工作。 (在添加缺少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' 

BTW,string(e)應該是str(e)

+0

感謝您的反饋falsetru,我拿了\出,並仍然收到操作數的錯誤。我無法弄清楚。該程序的工作原理,但恐怕操作數錯誤會導致更多的問題進一步下行,它保持觸發在'if int(splitLine [0])> int(lastUnix):'行。生病繼續尋找修復,再次感謝!另外,我注意到,在提交後我立即寫了字符串(e),doh! – heinztomato

+1

@heinztomato,請用完整的回溯更新您的問題。 – falsetru

+0

對於延遲抱歉,這裏是運行程序後的完整代碼和響應。它似乎使它打印'拉',但操作數問題返回而不是股票,然後循環轉到下一個輸入。有任何想法嗎?再次感謝你的幫助。 – heinztomato

相關問題