读取json键值作为不敏感键

我需要能够从python中的json地址中提取键“ irr”的值:

IRR = conparameters['components'][i]['envelope'][j]['irr']

即使'irr'是其他情况,例如IRR,Irr ...等。

那容易吗?

评论
  • 龙
    回复

    没有内置的功能可以执行此操作,您必须搜索匹配的键。

    See Get the first item from an iterable that matches a condition for how to write a first() function that finds the first element of an iterable that matches a condition. I'll use that in the solution below.

    cur = conparameters['components'][i]['envelope'][j]
    key = first(cur.keys(), lambda k: lower(k) == 'irr')
    IRR = cur[key]