非缺失密钥的Python JSON KeyError

时间:2015-11-19 19:05:43

标签: python json keyerror

由于某些未知原因,当我运行以下脚本时,将返回以下错误以及所需的输出。出于某种原因,这是昨晚没有任何错误。 API输出确实每分钟都会更改,但我不希望返回KeyError。我不能简单地指出这个错误的来源:

[u'@AAPL  151204C00128000'] <----- What I want to see printed

Traceback (most recent call last):

File "Options_testing.py", line 60, in <module>
 main()
 File "Options_testing.py", line 56, in main
  if quotes[x]['greeks']['impvol'] > 0: #change this for different greek vals

KeyError: 'impvol'

以下是一小段数据:

{"results":{"optionchain":{"expire":"all","excode":"oprac","equityinfo":{"longname":"Apple Inc","shortname":"AAPL"},"money":"at","callput":"all","key":{"symbol":["AAPL"],"exLgName":"Nasdaq Global Select","exShName":"NGS","exchange":"NGS"},"symbolstring":"AAPL"},"quote":[{"delaymin":15,"contract":{"strike":108,"openinterest":3516,"contracthigh":6.16,"contractlow":0.02,"callput":"Put","type":"WEEK","expirydate":"2015-11-13"},"root":{"equityinfo":{"longname":"Apple Inc","shortname":"AAPL"},"key":{"symbol":["AAPL"],"exLgName":"Nasdaq Global Select","exShName":"NGS","exchange":"NGS"}},"greeks":{"vega":0,"theta":0,"gamma":0,"delta":0,"impvol":0,"rho":0}

代码:

#Options screener using Quotemedia's API
import json
import requests
#import csv        

def main():         


    url_auth= "https://app.quotemedia.com/user/g/authenticate/v0/102368/XXXXX/XXXXX"
    decode_auth = requests.get(url_auth)        

    #print decode_auth.json()
    #print(type(decode_auth))           

    auth_data = json.dumps(decode_auth.json())          

    #Parse decode_auth, grab 'sid'          

    sid_parsed = json.loads(auth_data)["sid"]
    #print sid_parsed           

    #Pass sid into qm_options
    #Construct URL          

    symbol = 'AAPL'
    SID  = sid_parsed
    url_raw = 'http://app.quotemedia.com/data/getOptionQuotes.json?webmasterId=102368'  


    url_data = url_raw + '&symbol=' + symbol + '&greeks=true' + '&SID=' + SID           

    #print url_data         

    response = requests.get(url_data)
    #print response
    data = json.dumps(response.json())
    #print data         

    #save data to a file            

    with open('AAPL_20151118.json', 'w') as outfile:
        json.dumps (data, outfile)          

    #Turn into json object
    obj = json.loads(data)        

    #slim the object
    quotes = obj['results']['quote']        

    #find the number of options contracts
    range_count = obj['results']['symbolcount']        

    #print all contracts with an implied vol > 0
    for x in range(0,range_count):
        if quotes[x]['greeks']['impvol'] > 0: #change this for different greek vals
            print quotes[x]['key']['symbol']        

if __name__ == '__main__':
  main()

如有必要,我可以提供样本数据。

1 个答案:

答案 0 :(得分:1)

for x in range(0,range_count):
    if quotes[x]['greeks']['impvol'] > 0: #change this for different greek vals
        print quotes[x]['key']['symbol']

这循环通过多个引号,所以也许只有一个没有impvol属性。

您应该添加一些错误处理,以便了解何时发生这种情况。像这样:

# no need to iterate over indexes, just iterate over the items
for quote in quotes:
    if 'greeks' not in quote:
        print('Quote does not contain `greeks`:', quote)
    elif 'impvol' not in quote['greeks']:
        print('Quote does not contain `impvol`:', quote)

    elif quote['greeks']['impvol'] > 0:
        print quote['key']['symbol']