0

我对使用 API 密钥比较陌生。我已经尝试过 IG Markets API 并获得以下内容。通过使用 r.json(),我设法创建了类型(字典)。我想知道如何处理这种类型的数据并以“更好”的方式访问这些值。

我试过使用: for key, value in data() : print (key, value) 和 data["positions"] 但字典似乎只包含一个键。任何帮助表示赞赏!

data = r.json()
print(data)
{'positions': [{'market': {'bid': 1719.59,
                           'delayTime': 0,
                           'epic': '-',
                           'expiry': '-',
                           'high': 1749.95,
                           'instrumentName': 'Sverige30 Cash (100SK)',
                           'instrumentType': 'INDICES',
                           'lotSize': 100.0,
                           'low': 1713.33,
                           'marketStatus': 'TRADEABLE',
                           'netChange': -25.44,
                           'offer': 1721.09,
                           'percentageChange': -1.46,
                           'scalingFactor': 1,
                           'streamingPricesAvailable': True,
                           'updateTime': '22:15:16',
                           'updateTimeUTC': '21:15:16'},
                'position': {'contractSize': 100.0,
                             'controlledRisk': False,
                             'createdDate': '2020/07/13 23:12:53:000',
                             'createdDateUTC': '2020-07-13T21:12:53',
                             'currency': 'USD',
                             'dealId': '',
                             'dealReference': '',
                             'direction': 'BUY',
                             'level': 1720.74,
                             'limitLevel': None,
                             'limitedRiskPremium': None,
                             'size': 5.0,
                             'stopLevel': None,
                             'trailingStep': None,
                             'trailingStopDistance': None}}]}
4

2 回答 2

1

假设您的数据结构可以更大并且可能有多个'positions',您可以循环'positions'并检索size每个位置,如下所示:

for pos in data['positions']:
    size = pos['position']['size']
    print(size)

印刷:

5.0
于 2020-07-15T19:50:01.973 回答
1
  • 在示例中,只有一条记录,所以我创建了一个包含多条记录的示例positions
  • 我会使用熊猫来访问所有数据
import pandas as pd

# create dataframe
df = pd.json_normalize(data, 'positions')

# display(df)
   market.bid  market.delayTime market.epic market.expiry  market.high   market.instrumentName market.instrumentType  market.lotSize  market.low market.marketStatus  market.netChange  market.offer  market.percentageChange  market.scalingFactor  market.streamingPricesAvailable market.updateTime market.updateTimeUTC  position.contractSize  position.controlledRisk     position.createdDate position.createdDateUTC position.currency position.dealId position.dealReference position.direction  position.level position.limitLevel position.limitedRiskPremium  position.size position.stopLevel position.trailingStep position.trailingStopDistance
0     1719.59                 0           -             -      1749.95  Sverige30 Cash (100SK)               INDICES           100.0     1713.33           TRADEABLE            -25.44       1721.09                    -1.46                     1                             True          22:15:16             21:15:16                  100.0                    False  2020/07/13 23:12:53:000     2020-07-13T21:12:53               USD                                                       BUY         1720.74                None                        None            5.0               None                  None                          None
1     1719.59                 0           -             -      1749.95  Sverige30 Cash (100SK)               INDICES           100.0     1713.33           TRADEABLE            -25.44       1721.09                    -1.46                     1                             True          22:15:16             21:15:16                  100.0                    False  2020/07/13 23:12:53:000     2020-07-13T21:12:53               USD                                                       BUY         1720.74                None                        None            5.0               None                  None                          None
2     1719.59                 0           -             -      1749.95  Sverige30 Cash (100SK)               INDICES           100.0     1713.33           TRADEABLE            -25.44       1721.09                    -1.46                     1                             True          22:15:16             21:15:16                  100.0                    False  2020/07/13 23:12:53:000     2020-07-13T21:12:53               USD                                                       BUY         1720.74                None                        None            5.0               None                  None                          None
3     1719.59                 0           -             -      1749.95  Sverige30 Cash (100SK)               INDICES           100.0     1713.33           TRADEABLE            -25.44       1721.09                    -1.46                     1                             True          22:15:16             21:15:16                  100.0                    False  2020/07/13 23:12:53:000     2020-07-13T21:12:53               USD                                                       BUY         1720.74                None                        None            5.0               None                  None                          None

data有多个记录

data =  {
            'positions': [{
                    'market': {
                        'bid': 1719.59,
                        'delayTime': 0,
                        'epic': '-',
                        'expiry': '-',
                        'high': 1749.95,
                        'instrumentName': 'Sverige30 Cash (100SK)',
                        'instrumentType': 'INDICES',
                        'lotSize': 100.0,
                        'low': 1713.33,
                        'marketStatus': 'TRADEABLE',
                        'netChange': -25.44,
                        'offer': 1721.09,
                        'percentageChange': -1.46,
                        'scalingFactor': 1,
                        'streamingPricesAvailable': True,
                        'updateTime': '22:15:16',
                        'updateTimeUTC': '21:15:16'
                    },
                    'position': {
                        'contractSize': 100.0,
                        'controlledRisk': False,
                        'createdDate': '2020/07/13 23:12:53:000',
                        'createdDateUTC': '2020-07-13T21:12:53',
                        'currency': 'USD',
                        'dealId': '',
                        'dealReference': '',
                        'direction': 'BUY',
                        'level': 1720.74,
                        'limitLevel': None,
                        'limitedRiskPremium': None,
                        'size': 5.0,
                        'stopLevel': None,
                        'trailingStep': None,
                        'trailingStopDistance': None
                    }
                }, {
                    'market': {
                        'bid': 1719.59,
                        'delayTime': 0,
                        'epic': '-',
                        'expiry': '-',
                        'high': 1749.95,
                        'instrumentName': 'Sverige30 Cash (100SK)',
                        'instrumentType': 'INDICES',
                        'lotSize': 100.0,
                        'low': 1713.33,
                        'marketStatus': 'TRADEABLE',
                        'netChange': -25.44,
                        'offer': 1721.09,
                        'percentageChange': -1.46,
                        'scalingFactor': 1,
                        'streamingPricesAvailable': True,
                        'updateTime': '22:15:16',
                        'updateTimeUTC': '21:15:16'
                    },
                    'position': {
                        'contractSize': 100.0,
                        'controlledRisk': False,
                        'createdDate': '2020/07/13 23:12:53:000',
                        'createdDateUTC': '2020-07-13T21:12:53',
                        'currency': 'USD',
                        'dealId': '',
                        'dealReference': '',
                        'direction': 'BUY',
                        'level': 1720.74,
                        'limitLevel': None,
                        'limitedRiskPremium': None,
                        'size': 5.0,
                        'stopLevel': None,
                        'trailingStep': None,
                        'trailingStopDistance': None
                    }
                }, {
                    'market': {
                        'bid': 1719.59,
                        'delayTime': 0,
                        'epic': '-',
                        'expiry': '-',
                        'high': 1749.95,
                        'instrumentName': 'Sverige30 Cash (100SK)',
                        'instrumentType': 'INDICES',
                        'lotSize': 100.0,
                        'low': 1713.33,
                        'marketStatus': 'TRADEABLE',
                        'netChange': -25.44,
                        'offer': 1721.09,
                        'percentageChange': -1.46,
                        'scalingFactor': 1,
                        'streamingPricesAvailable': True,
                        'updateTime': '22:15:16',
                        'updateTimeUTC': '21:15:16'
                    },
                    'position': {
                        'contractSize': 100.0,
                        'controlledRisk': False,
                        'createdDate': '2020/07/13 23:12:53:000',
                        'createdDateUTC': '2020-07-13T21:12:53',
                        'currency': 'USD',
                        'dealId': '',
                        'dealReference': '',
                        'direction': 'BUY',
                        'level': 1720.74,
                        'limitLevel': None,
                        'limitedRiskPremium': None,
                        'size': 5.0,
                        'stopLevel': None,
                        'trailingStep': None,
                        'trailingStopDistance': None
                    }
                }, {
                    'market': {
                        'bid': 1719.59,
                        'delayTime': 0,
                        'epic': '-',
                        'expiry': '-',
                        'high': 1749.95,
                        'instrumentName': 'Sverige30 Cash (100SK)',
                        'instrumentType': 'INDICES',
                        'lotSize': 100.0,
                        'low': 1713.33,
                        'marketStatus': 'TRADEABLE',
                        'netChange': -25.44,
                        'offer': 1721.09,
                        'percentageChange': -1.46,
                        'scalingFactor': 1,
                        'streamingPricesAvailable': True,
                        'updateTime': '22:15:16',
                        'updateTimeUTC': '21:15:16'
                    },
                    'position': {
                        'contractSize': 100.0,
                        'controlledRisk': False,
                        'createdDate': '2020/07/13 23:12:53:000',
                        'createdDateUTC': '2020-07-13T21:12:53',
                        'currency': 'USD',
                        'dealId': '',
                        'dealReference': '',
                        'direction': 'BUY',
                        'level': 1720.74,
                        'limitLevel': None,
                        'limitedRiskPremium': None,
                        'size': 5.0,
                        'stopLevel': None,
                        'trailingStep': None,
                        'trailingStopDistance': None
                    }
                }
            ]
        }
于 2020-07-15T19:55:55.753 回答