0

I'm a beginner Python coder. I've been trying to get the opening stock price for a range of dates using the IEX API.

My json values are showing:

{'AAPL': {'2017-02-09': {'open': 129.1019, 'high': 129.8815, 'low': 128.5821, 'close': 129.8569, 'volume': 28349859.0}, '2017-02-10': {'open': 129.8962, 'high': 130.3669, 'low': 129.4941, 'close': 129.5628, 'volume': 20065458.0}, '2017-02-13': {'open': 130.5042, 'high': 131.2299, 'low': 130.1806, 'close': 130.7101, 'volume': 23035421.0}}}

If I only want to show only the 'open' prices for all the dates, how do I do that?

I've been googling lists and dictionaries but couldn't find anything useful...

from iexfinance import get_historical_data
from datetime import datetime
import json

start = datetime(2017, 2, 9)

end = datetime(2017, 2, 13)

f = get_historical_data("AAPL", start, end, output_format='json')

json_string = json.dumps(f)
json_read = json.loads(json_string)

print(json_read)
sacuL
  • 49,704
  • 8
  • 81
  • 106
Eric
  • 95
  • 2
  • 11
  • Why are you dumping and immediately reloading the data? If the output format is already `json`, surely only loading is needed? – ShadowRanger Jul 03 '18 at 18:09
  • What about what you've found on accessing lists and dictionaries did you not find useful? What did you try in this case and what was the error? – roganjosh Jul 03 '18 at 18:10
  • For the record, this should behave as a `dict` of `dict` of `dict`s; if you want a `dict` mapping date to open price, you should be able to do `{date: tradinginfo['open'] for date, tradinginfo in json_read['AAPL'].items()}` – ShadowRanger Jul 03 '18 at 18:12
  • Your output format is *already* in JSON, so there is no need to dump and read again. Did you read the [`iexfinance` module documentation](https://addisonlynch.github.io/iexfinance/index.html#documentation)? There it is stated that the JSON format is just a dictionary, really. You could just take the Pandas dataframe instead.. – Martijn Pieters Jul 03 '18 at 18:13
  • Possible duplicate of [python: read json and loop dictionary](https://stackoverflow.com/questions/5147292/python-read-json-and-loop-dictionary) – Michael Doubez Jul 03 '18 at 18:13
  • Thanks ShadowRanger. I guess I'm confused between loads and dumps – Eric Jul 03 '18 at 18:31
  • roganjosh > I wasn't sure if I should be using wildcards for the dates or how I can be referencing dictionaries within dictionaries – Eric Jul 03 '18 at 18:54

3 Answers3

1

The iexfinance module you are using can give you a far more convenient format: a Pandas data frame:

df = get_historical_data("AAPL", start, end, output_format='pandas')
print(df.open)

The data is indexed by date, so the df.open column is a pandas Series of opening values by date:

>>> from iexfinance import get_historical_data
>>> from datetime import datetime
>>> start = datetime(2017, 2, 9)
>>> end = datetime(2017, 2, 13)
>>> df = get_historical_data("AAPL", start, end, output_format='pandas')
>>> print(df.open)
date
2017-02-09    129.1019
2017-02-10    129.8962
2017-02-13    130.5042
Name: open, dtype: float64
>>> for open in df.open:
...     print(open)
...
129.1019
129.8962
130.5042

When you use the json format, the module produces a Python dictionary, there is no need to convert to JSON and back again. The format is suitable for JSON serialisation, but you don't need to jump through those hoops. Granted, the developer's choice of format name is confusing there.

To do the same with a dictionary, just loop over all the items for the dictionary referenced by 'AAPL'; keys are dates, and the values are more dictionaries with a key for each column:

f = get_historical_data("AAPL", start, end)
for date, date_entry in f['AAPL'].items():
    print(date, date_entry['open'])

This would give you entries in an dictionary-defined order; you may want to sort by key first:

for date, date_entry in sorted(f['AAPL'].items(), key=lambda kv: kv[0]):
    print(date, date_entry['open'])
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

Are you looking for something like this:

for day in json_reads['AAPL']:
    print("{0}: {1}".format(day, json_reads['AAPL'][day]['open']))

Albeit excessively hardcoded, you can probably see the general dictionary access idea.

Sam Gomena
  • 1,450
  • 11
  • 21
0

Please try this:

def find_open(stock):

for k,v in stock.items():
    if k == 'open':
        print(stock[k])
    elif isinstance(v, dict):
        find_open(v)

Calling function

find_open({'AAPL': {'2017-02-09': {'open': 129.1019, 'high': 129.8815, 'low': 128.5821, 'close': 129.8569, 'volume': 28349859.0}, '2017-02-10': {'open': 129.8962, 'high': 130.3669, 'low': 129.4941, 'close': 129.5628, 'volume': 20065458.0}, '2017-02-13': {'open': 130.5042, 'high': 131.2299, 'low': 130.1806, 'close': 130.7101, 'volume': 23035421.0}}})

kanishk
  • 91
  • 9