用于阅读股票行情的CSV文件

时间:2015-11-02 22:59:29

标签: python loops csv

我正在尝试阅读两个股票行情的名称,创建一个包含三列的csv文件,标题行: date stock1 stock2 包含前30天交易中每个股票的开盘价。 csv文件应命名为'stock1stock2date.csv',其中stock1和stock2是用户输入的名称,日期是今天的日期。 到目前为止,我有:

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

答案 0 :(得分: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)