-3

I used python 2.7 to fetch historical data from Google finance. I would like to save the output in .csv with the stock individual names. Example, AAPL.csv,MSFT.csv and etc. Since I have 900 stocks in the list, I use a for loop to name my file but it is unable to name stock in csv (I mean the .csv extension is missing with the way I wrote my script). Please advice.

if __name__ == '__main__':
  stocklist=['AAPL','MSFT','ABC','KKF']
    stocklen=len(stocklist)
or x in range(1,stocklen,1):
    q = GoogleQuote(stocklist[x],'2015-12-21')              
    print q                                          
      q.write_csv(stocklist[x])     

This is the snippet for the code as it is too long. Since the stocks also included foreign stocks, I am unable to use pandas datareader. I could have use thisq.write_csv('ABC.csv')but it is not practical since I have 900 stocks and need to name it one by one. I afraid with current type of file without .csv, I could not perform data analysis using pandas. [snapshot of the file without .csv extension1

bkcollection
  • 913
  • 1
  • 12
  • 35
  • 2
    This question is badly formatted, not clearly written and unclear. – Caridorc Dec 30 '15 at 14:19
  • I want to save the historical data in .csv, with each of the stocks name. Now, I can save it with the stokcs name with the code above but without the .csv extension – bkcollection Dec 30 '15 at 14:25
  • _but without the .csv extension_ you mean that you are taking the data form a txt file? Sorry I cannot understand clearly what you are trying to do, much less your problem. – Caridorc Dec 30 '15 at 14:27
  • Just create the filename dynamically by concatenating `stocklist[x]` with ".csv" I don't know the string concatenation syntax for python so you will need to check it, but in java syntax for example it would be something like `write_csv(stocklist[x]+".csv")` – EkcenierK Dec 30 '15 at 14:42
  • Hi Caridorc, please refer the snapshot attachment. you can see the YTL file is without the.csv in file type – bkcollection Dec 30 '15 at 15:02

1 Answers1

1

Your indentations are off in the snippet you posted, but it sounds like you need to append ".csv" to the end of the file name. You could replace your last line with the following:

q.write_csv('{}.csv'.format(stocklist[x]))
mattvivier
  • 2,230
  • 1
  • 15
  • 17