Python for Finance: Analyze Big Financial Data

(Elle) #1
2014-06-27,41.61,42.29,41.51,42.25,74640000,41.99

2014-06-26,41.93,41.94,41.43,41.72,23604400,41.46

2014-06-25,41.70,42.05,41.46,42.03,20049100,41.77

2014-06-24,41.83,41.94,41.56,41.75,26509100,41.49

2014-06-23,41.73,42.00,41.69,41.99,18743900,41.73

2014-06-20,41.45,41.83,41.38,41.68,47764900,41.42

2014-06-19,41.57,41.77,41.33,41.51,19828200,41.25

2014-06-18,41.61,41.74,41.18,41.65,27097000,41.39

2014-06-17,41.29,41.91,40.34,41.68,22518600,41.42

2014-06-16,41.04,41.61,41.04,41.50,24205300,41.24

2014-06-13,41.10,41.57,40.86,41.23,26310000,40.97

2014-06-12,40.81,40.88,40.29,40.58,29818900,40.33

2014-06-11,40.93,41.07,40.77,40.86,18040000,40.61

2014-06-10,41.03,41.16,40.86,41.11,15117700,40.85

2014-06-09,41.39,41.48,41.02,41.27,15019200,41.01

2014-06-06,41.48,41.66,41.24,41.48,24060500,41.22

2014-06-05,40.59,41.25,40.40,41.21,31865200,40.95

2014-06-04,40.21,40.37,39.86,40.32,23209000,40.07

2014-06-03,40.60,40.68,40.25,40.29,18068900,40.04

2014-06-02,40.95,41.09,40.68,40.79,18504300,40.54

The function urlretrieve allows us to retrieve content and save it to disk in a single step,


which is quite convenient in many circumstances:


In  [ 41 ]: urllib.urlretrieve(url  %   params, ‘./data/msft.csv’)
Out[41]: (‘./data/msft.csv’, <httplib.HTTPMessage instance at 0x7f92ca59afc8>)

A brief inspection of the content of the saved file shows that we have indeed retrieved and


saved the same content as before:


In  [ 42 ]: csv =   open(‘./data/msft.csv’, ‘r’)
csv.readlines()[: 5 ]
Out[42]: [‘Date,Open,High,Low,Close,Volume,Adj Close\n’,
‘2014-07-30,44.07,44.10,43.29,43.58,31921400,43.31\n’,
‘2014-07-29,43.91,44.09,43.64,43.89,27763100,43.62\n’,
‘2014-07-28,44.36,44.51,43.93,43.97,29684200,43.70\n’,
‘2014-07-25,44.30,44.66,44.30,44.50,26737700,44.22\n’]
In [ 43 ]: !rm -f ./data/*
Free download pdf