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)
7
  • Why are you dumping and immediately reloading the data? If the output format is already json, surely only loading is needed? Commented Jul 3, 2018 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? Commented Jul 3, 2018 at 18:10
  • For the record, this should behave as a dict of dict of dicts; 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()} Commented Jul 3, 2018 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? There it is stated that the JSON format is just a dictionary, really. You could just take the Pandas dataframe instead.. Commented Jul 3, 2018 at 18:13
  • Possible duplicate of python: read json and loop dictionary Commented Jul 3, 2018 at 18:13

3 Answers 3

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'])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the all the details! This is great!
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.

Comments

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}}})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.