python - 解析 JSON 以创建函数
问题描述
我有以下代码,我似乎无法从中创建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}}
解决方案
json 文件中的字典有“PE”或“CE”键。没有字典两者都有。
如果字典中不存在该键,您可以使用它get
来访问自动返回的字典值。None
尝试这样的事情:
for question_data in data['records']['data']:
if question_data.get('CE'):
print(question_data.get('CE')['askQty'])
推荐阅读
- java - ESP8266 滞后一段时间,然后更新非常快
- c# - ASP.NET MVC:一个或多个实体的验证失败。有关更多详细信息,请参阅“EntityValidationErrors”属性
- javascript - 为什么 $('div :not(script)').contents().filter(function() 会导致转义的 HTML 字符变为未转义?
- python - 在 opencv 回调函数之外访问变量
- typescript - 预先不知道属性名称的打字稿类
- visual-studio-2019 - 调试时如何使 Visual Studio 2019 在 try 语句中不中断?
- python - IndexError:数组的索引过多:数组是 0 维的,但有 1 个被索引
- c++ - 输入7个字母单词,输出7位电话号码
- python - 在 Python 中从用户输入中计算“while 循环”中的名称总数
- php - 如何通过 PHP 对齐其帖子下的评论