错误:主循环只能将元组(不是" str")连接到元组

时间:2015-06-17 16:45:12

标签: python loops matplotlib stocks

基本上尝试修改本教程而不是 股票:http://pythonprogramming.net/advanced-matplotlib-graphing-charting-tutorial/

使用我当前的代码:

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

代码:

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 个答案:

答案 0 :(得分:1)

您正在将CurrencysToPull元组传递给您的函数:

for eachStock in CurrencysToPull:
    pullData(CurrencysToPull)

然后尝试将字符串连接到:

fileline = Currency+'.txt'

您可能打算传入eachStock

for eachStock in CurrencysToPull:
    pullData(eachStock)

答案 1 :(得分:0)

错误在这一行: fileline = Currency+'.txt' 货币是一个元组,.txt是一个字符串

在你的for循环中,你传递CurrencysToPull而不是每个股票。 它应该是:

for eachStock in CurrencysToPull:

您可以在异常处理中使用回溯获得有关错误的更好信息。

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