2015-11-02 76 views
-2

我想讀取兩個股票代碼的名稱,創建一個包含三列的CSV文件,標題行: date stock1 stock2 包含每個股票的開盤價每個交易日前30天。 csv文件應該命名爲'stock1stock2date.csv',其中stock1和stock2是用戶輸入的名稱,日期是今天的日期。 到目前爲止,我有:用於讀取股票代碼的CSV文件

import csv 

with open('ticker1','ticker2', 'r') as csvfile: 
    ticker1 = csv.reader(csvfile, delimiter=',') 
    ticker2 = csv.reader(csvfile, delimiter=',') 


def saveAsCsv(ticker1): 
    filename = ticker1 + ticker2 + date + ".csv" 
    for row in filename: 
     #What goes here? 

def stockInfo(ticker1): 
    url1 = urllib.request.urlopen(
    'http://ichart.yahoo.com/table.csv?s=%s'%ticker1) 
    t11Data = url1.readlines() 

def main(): 
    ticker1 = input('Enter the first stock ticker.') 
    ticker2 = input('Enter the second stock ticker.') 
    date = input('Enter todays date.') 

我停留在for循環創建的行和列。有什麼建議麼?

+1

你怎麼卡住了?你想要實現什麼邏輯? – Celeo

+0

以open('output.out','w')作爲輸出文件寫入新文件 – Dap

回答

0
import datetime 
from pandas.io.data import get_data_yahoo 

def main(): 
    ticker1 = input('Enter the first stock ticker.') 
    ticker2 = input('Enter the second stock ticker.') 
    data = get_data_yahoo([ticker1, ticker2]) 
    filename = ticker1 + ticker2 + str(datetime.date.today()) + ".csv" 
    data.Open[-30:].to_csv(filename)