Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

python - main loop 'builtin_function_or_method' object is not iterable

I get this error "main loop 'builtin_function_or_method' object is not iterable" when I run the code below:

I have search stackoverflow, but cant find a answer to my question...

I have checked for typos, but cant find any error. Please help me!

import urllib2
import time
import datetime

stocksToPull = 'AAPL','GOOG','MSFT','CMG','AMZN','EBAY','TSLA'

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=5d/csv'
        saveFileLine = stock+'.txt'

        try:
            readExistingData = open(saveFileLine,'r').read()
            splitExisting = readExistingData.split('
')
            mostRecentLine = splitExisting[-2]
            lastUnix = mostRecentLine.split(',')[0]
        except:
            lastUnix = 0

        saveFile = open(saveFileLine,'a')
        sourceCode = urllib2.urlopen(urlToVisit).read()
        splitSource = sourceCode.split

        for eachLine in splitSource:
            splitLine = eachLine.split(',')
            if len(splitLine) ==6:
                if splitLine[0] > lastUnix:
                    if 'values' not in eachLine:
                        lineToWrite = eachLine+'
'
                        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(300)

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

for eachStock in stocksToPull:
    pullData(eachStock)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Direct Answer

In the code here:

saveFile = open(saveFileLine,'a')
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split

change sourceCode.split to sourceCode.split().


If you want to know more about this error, read below:

When debugging, you'd better remove the try...except block, especially an "expect Exception" block, which is so generic that you will get lost about what is going wrong.

When removed the try...except block and run these code again, you will get error info like this:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-c4fe20f718cd> in <module>()
     43 
     44 for eachStock in stocksToPull:
---> 45     pullData(eachStock)

<ipython-input-5-c4fe20f718cd> in pullData(stock)
     23     splitSource = sourceCode.split
     24 
---> 25     for eachLine in splitSource:
     26         splitLine = eachLine.split(',')
     27         if len(splitLine) ==6:

TypeError: 'builtin_function_or_method' object is not iterable

The error message TypeError: 'builtin_function_or_method' object is not iterable is associated with line 25, which means splitSource is a builtin_function_or_method and is not iterable.

What is splitSource? It is sourceCode.split. Here comes the answer. You should call a method by using (), without which you will get the method itself. The method str.split is obviously not iterable!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...