【问题标题】:Parsing JSON to create a function解析 JSON 以创建函数
【发布时间】:2021-08-04 18:18:24
【问题描述】:

我有以下代码,我似乎无法从中创建def

我有一个 Json 文件,其中一些看起来像这样:

{'records': {'expiryDates': ['05-Aug-2021', '12-Aug-2021', '18-Aug-2021', '26-Aug-2021', '02-Sep-2021', '09-Sep-2021', '16-Sep-2021', '23-Sep-2021', '30-Sep-2021', '28-Oct-2021', '30-Dec-2021', '31-Mar-2022', '30-Jun-2022', '29-Dec-2022', '29-Jun-2023', '28-Dec-2023', '27-Jun-2024', '26-Dec-2024', '26-Jun-2025'], 'data': [{'strikePrice': 7500, 'expiryDate': '29-Dec-2022', 'PE': {'strikePrice': 7500, 'expiryDate': '29-Dec-2022', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY29-12-2022PE7500.00', 'openInterest': 21, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 235, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 2800, 'totalSellQuantity': 0, 'bidQty': 2800, 'bidprice': 5.85, 'askQty': 0, 'askPrice': 0, 'underlyingValue': 16130.75}}, {'strikePrice': 8000, 'expiryDate': '30-Dec-2021', 'PE': {'strikePrice': 8000, 'expiryDate': '30-Dec-2021', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY30-12-2021PE8000.00', 'openInterest': 21, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 3.1, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 6850, 'totalSellQuantity': 0, 'bidQty': 1650, 'bidprice': 3.05, 'askQty': 0, 'askPrice': 0, 'underlyingValue': 16130.75}}, {'strikePrice': 8300, 'expiryDate': '30-Jun-2022', 'PE': {'strikePrice': 8300, 'expiryDate': '30-Jun-2022', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY30-06-2022PE8300.00', 'openInterest': 3, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 4.7, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 2500, 'totalSellQuantity': 0, 'bidQty': 2500, 'bidprice': 0.25, 'askQty': 0, 'askPrice': 0, 'underlyingValue': 16130.75}}, {'strikePrice': 8500, 'expiryDate': '30-Dec-2021', 'PE': {'strikePrice': 8500, 'expiryDate': '30-Dec-2021', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY30-12-2021PE8500.00', 'openInterest': 199.5, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 3.25, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 7600, 'totalSellQuantity': 0, 'bidQty': 1500, 'bidprice': 3.05, 'askQty': 0, 'askPrice': 0, 'underlyingValue': 16130.75}}, {'strikePrice': 8500, 'expiryDate': '29-Dec-2022', 'PE': {'strikePrice': 8500, 'expiryDate': '29-Dec-2022', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY29-12-2022PE8500.00', 'openInterest': 2254, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 22.9, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 2850, 'totalSellQuantity': 0, 'bidQty': 2800, 'bidprice': 18.55, 'askQty': 0, 'askPrice': 0, 'underlyingValue': 16130.75}}, 

访问我拥有的数据:

access_records_data = data['records']

所以这会产生这样的结果:

{'expiryDates': ['05-Aug-2021',
  '12-Aug-2021',
  '18-Aug-2021',
  '26-Aug-2021',
  '02-Sep-2021',
  '09-Sep-2021',
  '16-Sep-2021',
  '23-Sep-2021',
  '30-Sep-2021',
  '28-Oct-2021',
  '30-Dec-2021',
  '31-Mar-2022',
  '30-Jun-2022',
  '29-Dec-2022',
  '29-Jun-2023',
  '28-Dec-2023',
  '27-Jun-2024',
  '26-Dec-2024',
  '26-Jun-2025'],
 'data': [{'strikePrice': 7500,
   'expiryDate': '29-Dec-2022',
   'PE': {'strikePrice': 7500,
    'expiryDate': '29-Dec-2022',
    'underlying': 'NIFTY',
    'identifier': 'OPTIDXNIFTY29-12-2022PE7500.00',
    'openInterest': 21,
    'changeinOpenInterest': 0,
    'pchangeinOpenInterest': 0,
    'totalTradedVolume': 0,
    'impliedVolatility': 0,
    'lastPrice': 235,
    'change': 0,
    'pChange': 0,
    'totalBuyQuantity': 2800,
    'totalSellQuantity': 0,
    'bidQty': 2800,
    'bidprice': 5.85,
    'askQty': 0,
    'askPrice': 0,
    'underlyingValue': 16130.75}},

这很好用!接下来要访问记录中的数据,我有这个:

question_access = access_records_data['data']

产生这个:

question_access

    {'strikePrice': 15500,
  'expiryDate': '31-Mar-2022',
  'CE': {'strikePrice': 15500,
   'expiryDate': '31-Mar-2022',
   'underlying': 'NIFTY',
   'identifier': 'OPTIDXNIFTY31-03-2022CE15500.00',
   'openInterest': 0,
   'changeinOpenInterest': 0,
   'pchangeinOpenInterest': 0,
   'totalTradedVolume': 0,
   'impliedVolatility': 0,
   'lastPrice': 0,
   'change': 0,
   'pChange': 0,
   'totalBuyQuantity': 100,
   'totalSellQuantity': 350,
   'bidQty': 50,
   'bidprice': 1150,
   'askQty': 100,
   'askPrice': 2299.9,
   'underlyingValue': 16130.75},
  'PE': {'strikePrice': 15500,
   'expiryDate': '31-Mar-2022',
   'underlying': 'NIFTY',
   'identifier': 'OPTIDXNIFTY31-03-2022PE15500.00',
   'openInterest': 11,
   'changeinOpenInterest': 0,
   'pchangeinOpenInterest': 0,
   'totalTradedVolume': 0,
   'impliedVolatility': 0,
   'lastPrice': 497.05,
   'change': 0,
   'pChange': 0,
   'totalBuyQuantity': 0,
   'totalSellQuantity': 50,
   'bidQty': 0,
   'bidprice': 0,
   'askQty': 50,
   'askPrice': 470,
   'underlyingValue': 16130.75}},

请注意,我没有粘贴整个数据,因为它很长,但我只是想了解我得到了什么结果。接下来,我使用这个列表来获取我可以在列表中的字典中使用的所有键。为此,我已经这样做了:

for question_data in question_access:
    print(question_data)

产生:

{'strikePrice': 18500, 'expiryDate': '27-Jun-2024', 'CE': {'strikePrice': 18500, 'expiryDate': '27-Jun-2024', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY27-06-2024CE18500.00', 'openInterest': 3, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 1810, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 0, 'totalSellQuantity': 500, 'bidQty': 0, 'bidprice': 0, 'askQty': 500, 'askPrice': 1814.6, 'underlyingValue': 16130.75}}

接下来进入我感兴趣的 CE 数据:

   CE_access = question_data['CE']
which contains this data:
{'strikePrice': 18500,
 'expiryDate': '27-Jun-2024',
 'underlying': 'NIFTY',
 'identifier': 'OPTIDXNIFTY27-06-2024CE18500.00',
 'openInterest': 3,
 'changeinOpenInterest': 0,
 'pchangeinOpenInterest': 0,
 'totalTradedVolume': 0,
 'impliedVolatility': 0,
 'lastPrice': 1810,
 'change': 0,
 'pChange': 0,
 'totalBuyQuantity': 0,
 'totalSellQuantity': 500,
 'bidQty': 0,
 'bidprice': 0,
 'askQty': 500,
 'askPrice': 1814.6,
 'underlyingValue': 16130.75}

最后,如果我这样做:

CE_access['askQty']

它产生这个500。这是我一直在寻找的结果。所以我想做的是工作。接下来我的计划是使用这段代码并生成一个函数。为此,我做了以下工作:

代码是:

def ask_price():
    access_records_data = data['records']
    question_access = access_records_data['data']
    for question_data in question_access:
        CE_access = question_data['CE']
         [there is another for loop here for CE_access]
    

我得到这个错误:

<ipython-input-306-153b641331d7> in ask_price()
      3     question_access = access_records_data['data']
      4     for question_data in question_access:
----> 5         CE_access = question_data['CE']
      6 

KeyError: 'CE'

我已经在这段代码上苦苦挣扎了一段时间,但一直没有取得任何进展,请帮助我。为什么我得到这个错误,上面的代码在函数工作之前。

编辑:

print(question_data)


{'strikePrice': 18500, 'expiryDate': '27-Jun-2024', 'CE': {'strikePrice': 18500, 'expiryDate': '27-Jun-2024', 'underlying': 'NIFTY', 'identifier': 'OPTIDXNIFTY27-06-2024CE18500.00', 'openInterest': 3, 'changeinOpenInterest': 0, 'pchangeinOpenInterest': 0, 'totalTradedVolume': 0, 'impliedVolatility': 0, 'lastPrice': 1810, 'change': 0, 'pChange': 0, 'totalBuyQuantity': 0, 'totalSellQuantity': 500, 'bidQty': 0, 'bidprice': 0, 'askQty': 500, 'askPrice': 1814.6, 'underlyingValue': 16130.75}}

【问题讨论】:

  • 很清楚为什么您会收到错误,因为您的question_data dict 之一没有"CE" 密钥 .您只发布了部分数据(如果您打算这样做,您应该确保它实际上是一个完整的示例,我们可以使用 i> 并且不只是切断使其无效源代码的地方)。所以不知道还能告诉你什么。你有没有试过print(question_data),它应该打印出有问题的字典(假设你在一个IPython repl中给出错误输出......)这告诉你什么?
  • 这是如何创建函数的?函数是代码
  • 这是您声称的,但错误消息暗示不同。我将冒险出去并相信错误消息正确地反映了正在发生的事情。同样,您是否尝试打印有问题的 dict 以检查其内容?您看到了什么?
  • 您的问题听起来像是您想根据正在解析的某些 JSON 数据中的内容(动态)创建一个函数。
  • @Slartibartfast - 查看 json 文件的结构。有些字典只有 PE,有些只有 CE。没有字典两者兼得。

标签: python python-3.x keyerror


【解决方案1】:

您的 json 文件中的字典有“PE”或“CE”键。没有字典两者兼得。

您可以使用 get 访问字典值,如果字典中不存在该键,则该字典值会自动返回 None

试试这样的:

for question_data in data['records']['data']:
    if question_data.get('CE'):
        print(question_data.get('CE')['askQty'])

【讨论】:

  • 感谢您的帮助。让我冥想.get
  • @Slartibartfast 这一般不是办法,改为使用if "CE" in question_data:,然后使用print(question_data["CE"]["askQty"]
  • 实际上“Pythonic 方式”是将CE_access = question_data['CE'] 放在try / except KeyError: 子句中,并在它不存在时执行一些替代操作。
猜你喜欢
  • 2020-01-26
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-07
相关资源
最近更新 更多