Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python从字典中提取值_Python - Fatal编程技术网

用Python从字典中提取值

用Python从字典中提取值,python,Python,我为这个可能是无意义的问题道歉,我被卡住了。我想从列表中的一个字典中,在另一个名为“match”的字典中,得到一些值来打印出来。字典如下所示,你可以看到它有点像雷区。这本词典描述了Betfair上两名球员“Rose”和“Garth Mulroy”之间的高尔夫博彩市场。我要提取的是这两个字符串。非常感谢您的帮助 match = bot.get_market(matches[0]) from pprint import pprint pprint(match) {'bspMarket': 'fal

我为这个可能是无意义的问题道歉,我被卡住了。我想从列表中的一个字典中,在另一个名为“match”的字典中,得到一些值来打印出来。字典如下所示,你可以看到它有点像雷区。这本词典描述了Betfair上两名球员“Rose”和“Garth Mulroy”之间的高尔夫博彩市场。我要提取的是这两个字符串。非常感谢您的帮助

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': ''}
我的尝试:

match = bot.get_market(matches[0])
for runners in match:
    print runners['name']
产生错误:

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
回溯(最近一次呼叫最后一次):
文件“C:/Python27/bots/test.py”,第31行,在
打印跑步者['name']
TypeError:字符串索引必须是整数,而不是str

如果您的原始词典名为
original\u dict
original\u dict[runners]
会给出运行者列表

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

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

请减少/简化您的输入。我已根据请求添加了我的尝试。我已修改帖子,使用pprint显示数据-使每个人都更易于阅读Jon Clements在原始帖子上的版本为+10。