Python 主循环“builtin_function_or_method”对象不可迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30145926/
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
main loop 'builtin_function_or_method' object is not iterable
提问by Isak La Fleur
I get this error "main loop 'builtin_function_or_method' object is not iterable" when I run the code below:
当我运行以下代码时,我收到此错误“主循环‘builtin_function_or_method’对象不可迭代”:
I have search stackoverflow, but cant find a answer to my question...
我有搜索 stackoverflow,但找不到我的问题的答案...
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('\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)
采纳答案by Da Tong
Direct Answer
直接回答
In the code here:
在此处的代码中:
saveFile = open(saveFileLine,'a')
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split
change sourceCode.split
to sourceCode.split()
.
更改sourceCode.split
为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.
调试时,最好删除 try...except 块,尤其是“expect Exception”块,它非常通用,以至于您会迷失于出错的地方。
When removed the try...except block and run these code again, you will get error info like this:
当删除 try...except 块并再次运行这些代码时,您将获得如下错误信息:
---------------------------------------------------------------------------
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
.
错误消息TypeError: 'builtin_function_or_method' object is not iterable
与第 25 行相关联,这意味着splitSource
是 abuiltin_function_or_method
和不是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
!
什么是splitSource
?它是sourceCode.split
。答案来了。您应该使用 using 调用方法()
,否则您将获得方法本身。方法str.split
显然不行iterable
!