Getting a KeyError when rotating through a for loop to create a dict

1k Views Asked by At

I'm importing trade data in from a product ticker from coinbase pro. I have the for loop set up to import specific information into a specific attribute. The for loop iterates (what appears to be) a random number of times and spits out a key error. I'm certain that the data set contains the attribute in the dict it's sourcing. I know this because it spits the data out in a random order so the specific attribute it calls has been called previous times successfully. (additionally I call those values it paused on separately to double check they aren't empty) I also reduced the for loop range to a third of the size. It still seems to have the KeyError(although occasionally it did parse through and print all the data. Update I don't have the data set in a file because i call it from coinbase pro, but if you would like i can add a text file of the data it pulls in?

import cbpro,
public_client = cbpro.PublicClient()
aprods=public_client.get_products()
prods=[temp['id'] for temp in aprods]

for i in range(0,len(prods)):
    print(i,prods[i])
    tprods={
            'id': prods[i],
            'price': float(public_client.get_product_ticker(product_id=prods[i]) ['price']),
            'h24': float(public_client.get_product_24hr_stats(prods[i]) ['high']),
            'l24': float(public_client.get_product_24hr_stats(prods[i]) ['low']),
            'range':(float(public_client.get_product_24hr_stats(prods[i]) ['high'])-float(public_client.get_product_24hr_stats(prods[i]) ['low'])),
            '%range':100*(float(public_client.get_product_24hr_stats(prods[i]) ['high'])-float(public_client.get_product_24hr_stats(prods[i]) ['low']))/float(public_client.get_product_ticker(product_id=prods[i])['price'])
            }

Below is the traceback

Traceback (most recent call last):
  File "C:\Users\Slide 2.0\Desktop\cryptopj\apt.py", line 24, in <module>
    'h24': float(public_client.get_product_24hr_stats(prods[i]) ['high']),
KeyError: 'high'
1

There are 1 best solutions below

0
On

For the starter, you should either use a debugger (if your IDE is having such) or prepare some mechanism to give you more details on the line that caused an error.

For the latter, the simplest thing would be to print the line before accessing the dictionary keys

print(public_client.get_product_24hr_stats(prods[i]))

Additionally, you seem to be querying some API for the new data each time, so you are not working on the same construct each time. You may want to assign a variable and access its keys later. The solution you present may hit some kind of rate limit and return some strange dictionary with an error message in it, instead of the actual data.

prods_data= public_client.get_product_24hr_stats(prods[i])
tprods={
            'id': prods[i],
            'price': float(public_client.get_product_ticker(product_id=prods[i]) ['price']),
            'h24': float(prods_data.get('high', 0)),
            'l24': float(prods_data.get('low', 0)),

etc.. Also mind that you can use the "get" method of a dictionary, with a default value (0 in this case) for a better error handling.