首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >词典解析词典

词典解析词典
EN

Stack Overflow用户
提问于 2016-06-28 21:31:32
回答 2查看 1.4K关注 0票数 0

我有以下的json答复:

代码语言:javascript
复制
{'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“的值。

我试过以下几种方法,但效果不佳:

代码语言:javascript
复制
for item in json_data:
    print(item['Holdings']['DisplayCode']) 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-28 21:34:40

您需要对从json_data["Securities"]返回的列表进行索引,您想要的键位于索引0处:

代码语言:javascript
复制
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"])

这给了你:

代码语言:javascript
复制
AAPL

如果它可以出现在另一个索引和/或如果您需要所有的DisplayCodes:

代码语言:javascript
复制
for dct in js["Securities"]:
     print(dct["DisplayCode"])

这会让你:

代码语言:javascript
复制
AAPL
AAWW
BBRY
ZGSI
票数 4
EN

Stack Overflow用户

发布于 2020-07-12 16:59:26

当我遇到这个错误的时候,我的原因是我做了这样的事情

代码语言:javascript
复制
    for instrument in self.instruments:
        instrument['bought'] = 0
        instrument['sold'] = 0

相反,实现这一目标的正确方法是:

代码语言:javascript
复制
    for instrument_id, instrument in self.instruments.items():
        instrument['bought'] = 0
        instrument['sold'] = 0

使用dicts中的items()方法解决了我的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38086653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档