我想从字典中的列表中获取值。发出Oanda REST-API请求时,得到以下json数据结构。
当我使用get函数时,可以从“ trades”和“ lastTransactionID”访问dict和键及其值,但不知道如何获取“ trades”键中的列表项。
如果您能帮助我完成这项工作,我将不胜感激
作品
打印rv.get(“ trades”)
不起作用
打印rv.get(“ trades”)。get(“ financing”)
{
"trades": [
{
"financing": "0.0000",
"openTime": "2017-11-08T17:21:49.533679739Z",
"price": "1.15901",
"unrealizedPL": "-0.0001",
"realizedPL": "0.0000",
"instrument": "EUR_USD",
"state": "OPEN",
"initialUnits": "1",
"currentUnits": "1",
"id": "2046"
},
{
"financing": "0.0000",
"openTime": "2017-11-08T17:19:27.343697147Z",
"price": "1.15905",
"unrealizedPL": "-0.0001",
"realizedPL": "0.0000",
"instrument": "EUR_USD",
"state": "OPEN",
"initialUnits": "1",
"currentUnits": "1",
"id": "2044"
}
],
"lastTransactionID": "2046"
}
感谢您的帮助和问候
最佳答案
只需遍历列表:
for trade in rv.get("trades"):
print trade.get("financing")
print rv.get("trades").get("financing")
不起作用,因为rv.get("trades")
返回词典列表。这些字典是带有"financing"
键的字典。关于python - 如何从字典列表中的字典中获取键和值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47186148/