-1

I've been trying to figure out for several hours how I would extract data from this file. Usually you can iterate through the keys using a for loop. But since the "main keys" are irregular i.e. Dates. How would I access all the data? I want to be able to extract all "1. open" values and put them into an array and not have to input the dates manually as keys.

I tried implementing a calendar looping system where you select starting date and end date and then use that fluctuating variable as a key, but I feel like that is overkill.

{
  "2020-02-06": {
    "1. open": "699.9200",
    "2. high": "795.8300",
    "3. low": "687.0000",
    "4. close": "748.9600",
    "5. volume": "39647744"
  },
  "2020-02-05": {
    "1. open": "823.2600",
    "2. high": "845.9800",
    "3. low": "704.1106",
    "4. close": "734.7000",
    "5. volume": "48423837"
  },
  "2020-02-04": {
    "1. open": "882.9600",
    "2. high": "968.9899",
    "3. low": "833.8800",
    "4. close": "887.0600",
    "5. volume": "60938758"
  },
  "2020-02-03": {
    "1. open": "673.6900",
    "2. high": "786.1400",
    "3. low": "673.5200",
    "4. close": "780.0000",
    "5. volume": "47233495"
  },
martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

0

You have a dict of dicts.

So, you can loop over those like this

for d, quotes in data.items():
   print('Quotes for day {} are {}'.format(d, quotes))

I want to be able to extract all "1. open" values and put them into an array and not have to input the dates manually as keys

Loop over each of the keys in the dict

open_quotes = [data[x]["1. open"] for x in data]
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I've gotten to this before. I manage to list all the "Dates/Keys" and then their respective dicts containing "1. open". However I can't manage to extract the specific data. All I get is: Dates: 2019-09-23 data: {'1. open': '240.0000', '2. high': '245.1794', '3. low': '239.2200', '4. close': '241.2300', '5. volume': '4391630'} – DimbergTechnologies Feb 08 '20 at 00:29
  • I'm a beginner at this, I tried using the bottom code you supplied and I get this error: open_quotes = [x["1. open"] for x in data] TypeError: string indices must be integers – DimbergTechnologies Feb 08 '20 at 00:32
0

You mean like this?

>>> d = {
  "2020-02-06": {
    "1. open": "699.9200",
    "2. high": "795.8300",
    "3. low": "687.0000",
    "4. close": "748.9600",
    "5. volume": "39647744"
  },
  "2020-02-05": {
    "1. open": "823.2600",
    "2. high": "845.9800",
    "3. low": "704.1106",
    "4. close": "734.7000",
    "5. volume": "48423837"
  },
  "2020-02-04": {
    "1. open": "882.9600",
    "2. high": "968.9899",
    "3. low": "833.8800",
    "4. close": "887.0600",
    "5. volume": "60938758"
  },
  "2020-02-03": {
    "1. open": "673.6900",
    "2. high": "786.1400",
    "3. low": "673.5200",
    "4. close": "780.0000",
    "5. volume": "47233495"
  }}

>>> open_list = [d[date]["1. open"] for date in d.keys()]
>>> open_list
['699.9200', '823.2600', '882.9600', '673.6900']

revliscano
  • 2,227
  • 2
  • 12
  • 21