Search code examples
pythonpandascsvdataframefinance

How to update instead of rewriting csv by python with pandas to fetch stock data?


I am an absolute noob in terms of programming.
I wish to fetch historical data of a list of stock from yahoo for data analysis.
I modified the script I found and got this.

#settings for importing built-in datetime and date libraries
#and external pandas_datareader libraries

import pandas_datareader.data as web
import datetime
from datetime import timedelta


#read ticker symbols from a file to python symbol list
symbol = []
with open('E:\Google drive\Investment\Python Stock pick\Stocklist1.txt') as f:  
    for line in f:
        symbol.append(line.strip())
f.close

end = datetime.datetime.today()

start = end - timedelta(days=400)

#set path for csv file
path_out = 'E:/Google drive/Investment/Python Stock pick/CSV/'


i=0
while i<len(symbol):
    try:
        df = web.DataReader(symbol[i], 'yahoo', start, end)
        df.insert(0,'Symbol',symbol[i])
        df = df.drop(['Adj Close'], axis=1)
        if i == 0:
            df.to_csv(path_out+symbol[i]+'.csv')
            print (i, symbol[i],'has data stored to csv file')
        else:
            df.to_csv(path_out+symbol[i]+'.csv',header=True)
            print (i, symbol[i],'has data stored to csv file')
    except:
        print("No information for ticker # and symbol:")
        print (i,symbol[i])
        i=i+1
        continue
    i=i+1

And I run the script everyday and it fetches stock data in the past.
It would replace the entire csv file and always replacing the old data with the new one.

Is there anyway for the script to just add the new data into the csv file?

Thanks a lot in advance. I am all new to the programming world and have no idea how to do this.


Solution

  • I think you need to add 'a+' instead. Otherwise the file will keep looping itself. It is what happened to me.