-1

I have been trying to run the following code but have looked over stack exchange and Google extensively but can not find the reason why this code keeps giving the following error.

main loop cannot concatenate 'str' and 'tuple' objects

I'm using Python version 2.7.6 on Ubuntu.

>>> print (sys.version)
2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2]

Any help would be much appreciated . Thanks in advance.

import urllib2
import time

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

def pullData(stock):
    try:
        fileLine = stock+'.txt'
        urlToVisit = 'http://ichart.finance.yahoo.com/table.csv?s='+stocksToPull
        print urlToVisit
        sourceCode = urllib2.urlopen(urlToVisit).read()
        splitSource = sourceCode.split('\n')

        for eachLine in splitSource:
            splitLine = eachLine.split(',')
            if len(splitLine)==7:
                if 'value' not in eachLine:
                    saveFile = open(fileLine,'a')
                    lineToWrite = eachLine+'\n'
                    saveFile.write(lineToWrite)

        print 'Pulled',stocksToPull
        print 'sleeping'
        time.sleep(1)


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

for eachStock in stocksToPull:
    pullData('eachStock')
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user1380599
  • 103
  • 2
  • 6

3 Answers3

1

You cant add stocksToPull ='AAPL','GOOG','MSFT','CMG','AMZN','EBAY','TSLA' to 'http://ichart.finance.yahoo.com/table.csv?s='. You're trying to add the tuple stocksToPull to a string. maybe you mean

urlToVisit = 'http://ichart.finance.yahoo.com/table.csv?s='+stock
Camron_Godbout
  • 1,583
  • 1
  • 15
  • 22
0

The problem is

urlToVisit = 'http://ichart.finance.yahoo.com/table.csv?s='+stocksToPull

You are trying to concatenate a tuple directly to a string here. You need to do something like

urlToVisit = 'http://ichart.finance.yahoo.com/table.csv?s=' + stock

instead.

Stylisticly, catching and printing the error the way you are is obscuring the issue as well, since you are squelching the stack trace that would tell you the line number where the issue is actually occurring.

EDIT: Thanks to @chill0r's comment, I saw that you are using the tuple stocksToPull instead of the function variable stock, which is what you mean to do. Updated the answer to reflect that.

Silas Ray
  • 25,682
  • 5
  • 48
  • 63
  • 1
    he's using a `for eachStock in stocksToPull:` at the end - the function variable is `stock` – chill0r Aug 12 '14 at 18:31
  • I don't think so because if you type 'http://ichart.finance.yahoo.com/table.csv?s=AAPL,GOOG' into browser you get 404. I think he's trying to do each one individually to get the csv from the website. – Camron_Godbout Aug 12 '14 at 18:33
  • Yeah, I assumed he was meaning to append the joined tuple, which was the source of the error he was seeing, didn't notice the fact that he actually should have been using the function variable. Thanks. – Silas Ray Aug 12 '14 at 18:34
0
urlToVisit = 'http://ichart.finance.yahoo.com/table.csv?s='+stocksToPull

The stocksToPull variable is a tuple of strings. You can't concatenate the URL string with the tuple.

paidhima
  • 2,312
  • 16
  • 13