. Advertisement .
..3..
. Advertisement .
..4..
I am working with python and getting the error message:
main loop 'builtin_function_or_method' object is not iterable
Here is the detail of the code that I ran
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('\n')
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+'\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(300)
except Exception,e:
print 'main loop',str(e)
for eachStock in stocksToPull:
pullData(eachStock)
I need an explanation for the problems I’ve encountered. How to fix typeerror: ‘builtin_function_or_method’ object is not iterable?
Here is the solution to fix the error
Change
sourceCode.split
tosourceCode.split()
.The error message TypeError: ‘builtin_function_or_method’ object is not iterable was associated with line 25, meaning splitSource is builtin_function_or_method, and not iterable.
What is splitSource? It is sourceCode.split. This is the solution. () is the code to call a method. Without it, you’ll get the method. str.split is not iterable.