Extracting output data
In this section, we'll be discussing different ways to extract our output data to different file formats.
Outputting data to text files
The following code will download the daily price data for Microsoft and save it to a text file:
import pandas_datareader.data as getData import re ticker='msft' f=open("c:/temp/msft.txt","w") p = getData.DataReader(ticker, "google") f.write(str(p)) f.close()
The first several saved observations are shown in the following screenshot:
Saving our data to a .csv file
The following program first retrieves IBM price data, and then saves it as a .csv
file under c:/temp
:
from matplotlib.finance import quotes_historical_yahoo_ochl as getData import csv f=open("c:/temp/c.csv","w") ticker='c' begdate=(2016,1,1) enddate=(2017,1,9) p = getData(ticker, begdate, enddate,asobject=True,adjusted=True) writer = csv.writer(f) writer.writerows(p) f.close()
In the preceding code, we rename the quotes_historical_yahoo_ochl()
function as getData
for convenience...