2015-06-17 28 views
0

基本上試圖修改本教程爲貨幣的,而不是 股票:http://pythonprogramming.net/advanced-matplotlib-graphing-charting-tutorial/錯誤:主迴路只能串聯元組(而不是「STR」),以元組

與我當前的代碼我得到:

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) 

回答

1

您傳入CurrencysToPull元組的功能:

for eachStock in CurrencysToPull: 
    pullData(CurrencysToPull) 

您然後嘗試連接一個字符串:

fileline = Currency+'.txt' 

你可能意味着在eachStock票代替:

for eachStock in CurrencysToPull: 
    pullData(eachStock) 
+0

三江源@martijn皮特斯 – LibertySIlver

+0

@馬丁PIETERS但現在我得到一個錯誤pulledata沒有定義.... – LibertySIlver

+0

@LibertySIlver:你只要運行我的循環沒有你的代碼的其餘部分? –

0

的錯誤是在這一行: fileline = Currency+'.txt' 貨幣是一個元組,.txt是一個字符串

在您的for循環y您正在通過CurrencysToPull而不是每個庫存。 它應該是:

for eachStock in CurrencysToPull: 

你可能已經得到了有關在異常處理使用追蹤誤差更好的信息。

except Exception,(e): 
    print('main loop'), str(e) 
    print(traceback.format_exc()) 
相關問題