I can’t figure out how to make the tick of the stock from each txt file, in which on each new line, the quotes are downloaded line by line.

Here is the main code:

from pandas_datareader import data input = open('C:/Users/II/Downloads/1.txt', 'r') s = input.read() r = data.DataReader(s, 'yahoo', '1900-01-01') r.to_csv('C:/Users/II/Downloads/'+s+'_data.csv') 

I can not understand how to put while or for line, please write in the sample code. Thank.

An example of what is obtained at the output of the csv file.

 Date,Open,High,Low,Close,Volume,Adj Close 2010-11-18,35.0,35.990002000000004,33.889998999999996,34.189999,457044300,30.210939 2010-11-19,34.150002,34.5,33.110001000000004,34.259997999999996,107842000,30.272792 2010-11-22,34.200001,34.48,33.810001,34.080002,36650600,30.113744 2010-11-23,33.950001,33.990002000000004,33.189999,33.25,31170200,29.380338000000002 2010-11-24,33.73,33.799999,33.220001,33.48,26138000,29.58357 2010-11-26,33.41,33.810001,33.209998999999996,33.799999,12301200,29.866328000000003 2010-11-29,33.799999,33.810001,33.07,33.799999,27776900,29.866328000000003 
  • Give an example of the expected CSV file (5-7 lines). Do you want to save quotes for different companies in different files? - MaxU February
  • Your title doesn’t match the question. - Avernial

1 answer 1

Here is a working example:

 import pandas as pd import pandas_datareader.data as wb tickers_fn = r'D:\temp\tickers.txt' with open(tickers_fn) as f: stocklist = f.read().splitlines() p = wb.DataReader(stocklist, 'yahoo', '1900-01-01') # show Panel axes... #print(p.axes) for ticker in p.minor_axis: p.loc[:, :, ticker].dropna().to_csv('d:/temp/{}_data.csv'.format(ticker)) 

tickers.txt:

 AAPL GOOG FB AMZN 

What happened (I will give only a few first lines for several files):

GOOG_data.csv:

 Date,Open,High,Low,Close,Volume,Adj Close 2004-08-19,100.000168,104.060182,95.960165,100.340176,44871300.0,50.119968 2004-08-20,101.010175,109.08018700000001,100.500174,108.31018300000001,22942800.0,54.10099 2004-08-23,110.75019099999999,113.480193,109.050183,109.40018500000001,18342800.0,54.645447 

FB_data.csv:

 Date,Open,High,Low,Close,Volume,Adj Close 2012-05-18,42.049999,45.0,38.0,38.23,573576400.0,38.23 2012-05-21,36.529999,36.66,33.0,34.029999,168192700.0,34.029999 2012-05-22,32.610001000000004,33.59,30.940001000000002,31.0,101786600.0,31.0 

AMZN_data.csv:

 Date,Open,High,Low,Close,Volume,Adj Close 1997-05-15,29.25,30.0,23.125,23.5,72156000.0,1.958333 1997-05-16,23.625,23.75,20.5,20.75,14700000.0,1.729167 1997-05-19,21.125,21.25,19.5,20.5,6106800.0,1.708333 

AAPL_data.csv:

 Date,Open,High,Low,Close,Volume,Adj Close 1980-12-12,28.75,28.875,28.75,28.75,117258400.0,0.425 1980-12-15,27.375001,27.375001,27.25,27.25,43971200.0,0.402826 1980-12-16,25.375,25.375,25.25,25.25,26432000.0,0.373261 

I would advise you (if possible) not to store data in CSV format. It does not support storing data types and is also very slow. Try using HDF5 or Feather-Format .

PS look also at this answer (similar)

  • Man! Thank you for what you need. - Alex
  • @Alex, always happy to help :-) - MaxU February
  • And why HDF5 is preferable than for example JSON? - Alex
  • @Alex, for the same reasons - lack of information about data types (for example, working with dates and time requires conversion) and speed - MaxU