gpt4 book ai didi

python - 主循环 'builtin_function_or_method' 对象不可迭代

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:01 25 4
gpt4 key购买 nike

当我运行下面的代码时,我得到这个错误“main loop 'builtin_function_or_method' object is not iterable”:

我有搜索 stackoverflow,但找不到我的问题的答案...

我检查了拼写错误,但找不到任何错误。请帮助我!

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)

最佳答案

直接回答

这里的代码:

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

sourceCode.split 更改为 sourceCode.split()


如果您想了解有关此错误的更多信息,请阅读以下内容:

调试时,你最好删除 try...except block ,尤其是“expect Exception” block ,它太通用了,你会不知道哪里出错了。

当删除 try...except block 并再次运行这些代码时,您将得到如下错误信息:

---------------------------------------------------------------------------
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

错误消息 TypeError: 'builtin_function_or_method' object is not iterable 与第 25 行相关联,这意味着 splitSource 是一个 builtin_function_or_method 并且是不是可迭代的

什么是splitSource?它是 sourceCode.split。答案来了。您应该使用 () 调用方法,否则您将获得方法本身。 str.split 方法显然不是iterable!

关于python - 主循环 'builtin_function_or_method' 对象不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30145926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com