在Python中从字典中提取值

2024-10-03 17:27:08 发布

您现在位置:Python中文网/ 问答频道 /正文

很抱歉,我被卡住了。我想从一个列表中的字典中,在另一个名为“match”的字典中获取一些要打印的值。你可以看到字典下面的一点雷区。这本词典描述了Betfair上两名球员“罗斯”和“加思·穆罗伊”之间的高尔夫博彩市场。我要提取的就是这两条弦。非常感谢任何帮助!在

match = bot.get_market(matches[0])
from pprint import pprint
pprint(match)

{'bspMarket': 'false',
 'countryISO3': 'ZAF',
 'couponLinks': '',
 'discountAllowed': 'true',
 'eventTypeId': '3',
 'event_ids': ['3', '26909125', '26930851'],
 'interval': '0.0',
 'lastRefresh': '1354218248109',
 'licenceId': '1',
 'marketBaseRate': '5.0',
 'marketDescriptionHasDate': 'true',
 'marketDisplayTime': '2012-11-30T09:10:00.000Z',
 'marketId': '107625660',
 'marketStatus': 'ACTIVE',
 'marketSuspendTime': '2012-11-30T09:10:00.000Z',
 'marketTime': '2012-11-30T09:10:00.000Z',
 'marketType': 'O',
 'marketTypeVariant': 'D',
 'maxUnitValue': '0.0',
 'menuPath': '\\Group B\\Nedbank Challenge 2012\\2nd Round 2 Balls',
 'minUnitValue': '0.0',
 'name': 'Rose',
 'numberOfWinners': '1',
 'parentEventId': '26930851',
 'runners': [{'asian_line_id': '0',
              'handicap': '0.0',
              'name': 'Justin Rose',
              'selection_id': '2078854'},
             {'asian_line_id': '0',
              'handicap': '0.0',
              'name': 'Garth Mulroy',
              'selection_id': '2235937'},
             {'asian_line_id': '0',
              'handicap': '0.0',
              'name': 'Tie',
              'selection_id': '39905'}],
 'runnersMayBeAdded': 'false',
 'timezone': 'RSA',
 'unit': ''}

我的尝试:

^{pr2}$

产生错误:

Traceback (most recent call last):
  File "C:/Python27/bots/test.py", line 31, in <module>
    print runners['name']
TypeError: string indices must be integers, not str

Tags: nameidfalsetrue列表字典matchline
1条回答
网友
1楼 · 发布于 2024-10-03 17:27:08

如果原始字典名为original_dictoriginal_dict[runners]会给出跑步者的列表。在

runner_list = original_dict["runners"]
for runner in runner_list:
   name_you_are_looking_for = runner["name"]

但这很难解析,而且您可能希望以一种更容易遵循的方式组织数据。在任何情况下,以后请发布更多代码,以便我们更好地了解您的问题。在

相关问题 更多 >