Error: main loop can only concatenate tuple (not "str") to tuple

224 views Asked by At

Basically trying to modify the this tutorial for Currency's instead of stocks: http://pythonprogramming.net/advanced-matplotlib-graphing-charting-tutorial/

with my current code I get:

Error: main loop can only concatenate tuple (not "str") to tuple

The code:

import urllib2
import time



CurrencysToPull = 'audusd','eurusd','usdcad'

def pullData(Currency):
    try:
        fileline = Currency+'.txt'
        urlToVisit = 'http://finance.yahoo.com/echarts?s=Currency=X#{"allowChartStacking":true}/'+Currency+'/chartdata;type=quote:range=1y/csv'
        sourcecode = urllib2.urlopen(urlToVisit).read()
        splitSource = sourcecode.split('\n')

        for eachLine in splitSource:
            splitLine = eachLine.split(',')
            if len(splitLine)==6:
                if 'valuse' not in eachLine:
                    saveFile = open(fileLine,'a')
                    lineToWrite = eachLine+'\n'
                    saveFile.write(lineToWrite)
        print 'Pulled', Currency
        print 'sleeping'
        time.sleep(1)

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

for eachStock in CurrencysToPull:
    pullData(CurrencysToPull)
2

There are 2 answers

1
Martijn Pieters On

You are passing in the CurrencysToPull tuple to your function:

for eachStock in CurrencysToPull:
    pullData(CurrencysToPull)

which you then try to concatenate a string to:

fileline = Currency+'.txt'

You probably meant to pass in eachStock instead:

for eachStock in CurrencysToPull:
    pullData(eachStock)
0
DhruvPathak On

The error is in this line : fileline = Currency+'.txt' Currency is a tuple, .txt is a string

In your for loop you are passing CurrencysToPull instead of eachStock. it should be:

for eachStock in CurrencysToPull:

You could have got better information about the errors using traceback in your exception handling.

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