我有以下的json答复:
{'Securities': [{'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Computers & Peripherals', 'SecurityId': 2654, 'DisplayCode': 'AAPL', 'PricingSymbol': 'AAPL US Equity', 'PriceLast': 93.59, 'SecurityDesc': 'APPLE INC', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 92.04, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Information Technology'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Air Freight & Logistics', 'SecurityId': 2660, 'DisplayCode': 'AAWW', 'PricingSymbol': 'AAWW US Equity', 'PriceLast': 38.81, 'SecurityDesc': 'ATLAS AIR WORLDWIDE HOLDINGS', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 38.45, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Industrials'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Communications Equipment', 'SecurityId': 3445, 'DisplayCode': 'BBRY', 'PricingSymbol': 'BBRY US Equity', 'PriceLast': 6.48, 'SecurityDesc': 'BLACKBERRY LTD', 'Currency': 'USD', 'Country': 'CANADA', 'TradingFactor': 1.0, 'PriceStart': 6.29, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Information Technology'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': '', 'SecurityId': 403321, 'DisplayCode': 'ZGSI', 'PricingSymbol': 'ZGSI US Equity', 'PriceLast': 6.5, 'SecurityDesc': 'ZERO GRAVITY SOLUTIONS INC', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 0.0, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': ''}], 'Holdings': [{'HoldingId': 919049, 'MTDPnLTotal': -36434.0, 'Quantity': -40000.0, 'DTDPnLTotal': -36434.0, 'NetExposure': -3743600.0, 'LastModified': '2016-06-28T12:40:51.527', 'WTDPnLTotal': -36434.0, 'AcuId': 212, 'SecurityId': 2654, 'QuantityTraded': -40000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -36434.0, 'BetaExposure': -4155021.64, 'BookValue': -3743600.0, 'YTDPnLTotal': -36434.0, 'QuantityStart': 0.0, 'ShareEquivalent': -40000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1476408, 'MTDPnLTotal': -40.0, 'Quantity': -1000.0, 'DTDPnLTotal': -40.0, 'NetExposure': -38810.0, 'LastModified': '2016-06-28T12:38:03.72', 'WTDPnLTotal': -40.0, 'AcuId': 212, 'SecurityId': 2660, 'QuantityTraded': -1000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -40.0, 'BetaExposure': -63954.999, 'BookValue': -38810.0, 'YTDPnLTotal': -40.0, 'QuantityStart': 0.0, 'ShareEquivalent': -1000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1586376, 'MTDPnLTotal': -30200.0, 'Quantity': 10000.0, 'DTDPnLTotal': -30200.0, 'NetExposure': 64800.0, 'LastModified': '2016-06-28T13:14:42.237', 'WTDPnLTotal': -30200.0, 'AcuId': 212, 'SecurityId': 3445, 'QuantityTraded': 10000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -30200.0, 'BetaExposure': 92178.0, 'BookValue': 64800.0, 'YTDPnLTotal': -30200.0, 'QuantityStart': 0.0, 'ShareEquivalent': 10000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1586379, 'MTDPnLTotal': 0.0, 'Quantity': 100.0, 'DTDPnLTotal': 0.0, 'NetExposure': 650.0, 'LastModified': '2016-06-28T14:07:55.58', 'WTDPnLTotal': 0.0, 'AcuId': 212, 'SecurityId': 403321, 'QuantityTraded': 100.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': 0.0, 'BetaExposure': 650.0, 'BookValue': 650.0, 'YTDPnLTotal': 0.0, 'QuantityStart': 0.0, 'ShareEquivalent': 100.0, 'CustodianAccountCode': 'CSPB-739R70'}], 'LiveDate': '2016-06-28T00:00:00', 'RefreshTime': '2016-06-28T21:25:22.8529416Z'}这是一本字典。我想检索"AAPL",这是键"DisplayCode“的值。
我试过以下几种方法,但效果不佳:
for item in json_data:
print(item['Holdings']['DisplayCode']) 发布于 2016-06-28 21:34:40
您需要对从json_data["Securities"]返回的列表进行索引,您想要的键位于索引0处:
js = {'Securities': [{'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Computers & Peripherals', 'SecurityId': 2654, 'DisplayCode': 'AAPL', 'PricingSymbol': 'AAPL US Equity', 'PriceLast': 93.59, 'SecurityDesc': 'APPLE INC', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 92.04, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Information Technology'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Air Freight & Logistics', 'SecurityId': 2660, 'DisplayCode': 'AAWW', 'PricingSymbol': 'AAWW US Equity', 'PriceLast': 38.81, 'SecurityDesc': 'ATLAS AIR WORLDWIDE HOLDINGS', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 38.45, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Industrials'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Communications Equipment', 'SecurityId': 3445, 'DisplayCode': 'BBRY', 'PricingSymbol': 'BBRY US Equity', 'PriceLast': 6.48, 'SecurityDesc': 'BLACKBERRY LTD', 'Currency': 'USD', 'Country': 'CANADA', 'TradingFactor': 1.0, 'PriceStart': 6.29, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Information Technology'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': '', 'SecurityId': 403321, 'DisplayCode': 'ZGSI', 'PricingSymbol': 'ZGSI US Equity', 'PriceLast': 6.5, 'SecurityDesc': 'ZERO GRAVITY SOLUTIONS INC', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 0.0, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': ''}], 'Holdings': [{'HoldingId': 919049, 'MTDPnLTotal': -36434.0, 'Quantity': -40000.0, 'DTDPnLTotal': -36434.0, 'NetExposure': -3743600.0, 'LastModified': '2016-06-28T12:40:51.527', 'WTDPnLTotal': -36434.0, 'AcuId': 212, 'SecurityId': 2654, 'QuantityTraded': -40000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -36434.0, 'BetaExposure': -4155021.64, 'BookValue': -3743600.0, 'YTDPnLTotal': -36434.0, 'QuantityStart': 0.0, 'ShareEquivalent': -40000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1476408, 'MTDPnLTotal': -40.0, 'Quantity': -1000.0, 'DTDPnLTotal': -40.0, 'NetExposure': -38810.0, 'LastModified': '2016-06-28T12:38:03.72', 'WTDPnLTotal': -40.0, 'AcuId': 212, 'SecurityId': 2660, 'QuantityTraded': -1000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -40.0, 'BetaExposure': -63954.999, 'BookValue': -38810.0, 'YTDPnLTotal': -40.0, 'QuantityStart': 0.0, 'ShareEquivalent': -1000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1586376, 'MTDPnLTotal': -30200.0, 'Quantity': 10000.0, 'DTDPnLTotal': -30200.0, 'NetExposure': 64800.0, 'LastModified': '2016-06-28T13:14:42.237', 'WTDPnLTotal': -30200.0, 'AcuId': 212, 'SecurityId': 3445, 'QuantityTraded': 10000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -30200.0, 'BetaExposure': 92178.0, 'BookValue': 64800.0, 'YTDPnLTotal': -30200.0, 'QuantityStart': 0.0, 'ShareEquivalent': 10000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1586379, 'MTDPnLTotal': 0.0, 'Quantity': 100.0, 'DTDPnLTotal': 0.0, 'NetExposure': 650.0, 'LastModified': '2016-06-28T14:07:55.58', 'WTDPnLTotal': 0.0, 'AcuId': 212, 'SecurityId': 403321, 'QuantityTraded': 100.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': 0.0, 'BetaExposure': 650.0, 'BookValue': 650.0, 'YTDPnLTotal': 0.0, 'QuantityStart': 0.0, 'ShareEquivalent': 100.0, 'CustodianAccountCode': 'CSPB-739R70'}], 'LiveDate': '2016-06-28T00:00:00', 'RefreshTime': '2016-06-28T21:25:22.8529416Z'}
print(js["Securities"][0]["DisplayCode"])这给了你:
AAPL如果它可以出现在另一个索引和/或如果您需要所有的DisplayCodes:
for dct in js["Securities"]:
print(dct["DisplayCode"])这会让你:
AAPL
AAWW
BBRY
ZGSI发布于 2020-07-12 16:59:26
当我遇到这个错误的时候,我的原因是我做了这样的事情
for instrument in self.instruments:
instrument['bought'] = 0
instrument['sold'] = 0相反,实现这一目标的正确方法是:
for instrument_id, instrument in self.instruments.items():
instrument['bought'] = 0
instrument['sold'] = 0使用dicts中的items()方法解决了我的问题。
https://stackoverflow.com/questions/38086653
复制相似问题