main loop 'builtin_function_or_method' object is not iterable

前端 未结 1 1300
青春惊慌失措
青春惊慌失措 2020-12-20 18:46

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 t

相关标签:
1条回答
  • 2020-12-20 19:30

    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!

    0 讨论(0)
提交回复
热议问题