-2

The output of historical data using the Python Yahoo-Finance API is structured as follows:

{u'Adj_Close': u'63.209999',
 u'Close': u'63.209999',
 u'Date': u'2015-08-27',
 u'High': u'63.259998',
 u'Low': u'61.080002',
 u'Open': u'61.459999',
 u'Symbol': u'CAM',
 u'Volume': u'22808600'}

What's the best way to handle this data? For example, how can I access the Close price of 63.209999? Should I use a regex?

trf
  • 1
  • 1

2 Answers2

0

As shown you have a dictionary; use the keys to access values in the dictionary:

>>> d={u'Adj_Close': u'63.209999',
...  u'Close': u'63.209999',
...  u'Date': u'2015-08-27',
...  u'High': u'63.259998',
...  u'Low': u'61.080002',
...  u'Open': u'61.459999',
...  u'Symbol': u'CAM',
...  u'Volume': u'22808600'}
>>> 
>>> 
>>> d['Close']
u'63.209999'
>>> d['High']
u'63.259998'

Probably, however, you have a JSON string. If so you can convert the JSON string to a dictionary using json.loads():

>>> s = '{"High": "63.259998", "Volume": "22808600", "Low": "61.080002", "Date": "2015-08-27", "Close": "63.209999", "Symbol": "CAM", "Open": "61.459999", "Adj_Close": "63.209999"}'
>>> import json
>>> d = json.loads(s)
>>> d['Close']
u'63.209999'
mhawke
  • 84,695
  • 9
  • 117
  • 138
0

Pandas is an excellent library to handle such tabular problem.

You can do like this

>>> import pandas as pd
>>>
>>> data = {u'Adj_Close': u'63.209999',
...         u'Close': u'63.209999',
...         u'Date': u'2015-08-27',
...         u'High': u'63.259998',
...         u'Low': u'61.080002',
...         u'Open': u'61.459999',
...         u'Symbol': u'CAM',
...         u'Volume': u'22808600'}
>>>
>>> df = pd.DataFrame(data.items())
>>> print df.head()
        0           1
0    High   63.259998
1  Volume    22808600
2     Low   61.080002
3    Date  2015-08-27
4   Close   63.209999

NOTE: Be careful with u'Symbol': u'CAM' because it is a scalar value.

Eric
  • 2,636
  • 21
  • 25