我有一个转换后的字典,该字典是从API获取的,我应该从路由获取一个名称。当我尝试获得它时,我总是获得第一价值
我一直在尝试,就像在一个示例中一样(我没有搜索示例,我在代码Namereponsibleroutes = alltext[i]['routes'][0]['name']
上显示了它),但是我有第一个值,没有它[0]
,我有错误TypeError: list indices must be integers or slices, not str
alltext=[{'category': {'id': 'd41d97eb-6ecc-4aed-b9be-4fca3411d2c8',
'name': 'С',
'vendorId': 'С'},
'format': {'id': 'fad3d784-6a79-4af8-bf66-db214fadef11',
'name': 'магазин',
'vendorId': '1'},
'freeVisit': False,
'id': 'fd65865b-0f43-468b-80a5-04d5c9f90086',
'name': 'магазин-кафетерій',
'relationships': [{'orderIndex': 1,
'relationship': {'deferment': 90,
'id': '44a3297a-37fc-4844-a6ee-43de0a8bfcf3',
'limit': 0.0,
'name': 'Договор № 32207330',
'vendorId': '32207330'}}],
'routes': [{'dateFrom': '2018-01-01T00:00:00Z',
'id': '9466dd46-53c8-4ff4-bfaa-0115262eeed9',
'name': 'string1',
'vendorId': '154788512'},
{'dateFrom': '2018-01-01T00:00:00Z',
'id': 'eac2a7e1-b9c7-4b14-b165-21f5e95bd71f',
'name': 'string2',
'orderPricePolicy': {'discountPercentEnabled': False,
'discountPercentFrom': 0.0,
'discountPercentIsFractional': False,
'discountPercentTill': 0.0,
'markupPercentEnabled': False,
'markupPercentFrom': 0.0,
'markupPercentIsFractional': False,
'markupPercentTill': 0.0,
'priceEditEnabled': True}},
{'dateFrom': '2018-01-01T00:00:00Z',
'fetchMethod': 'byRelationships',
'id': 'a2890f23-a1d9-458c-9755-3deed8fc3153',
'name': 'string3',
'orderPricePolicy': {'discountPercentEnabled': True,
'discountPercentFrom': 1.1,
'discountPercentIsFractional': True,
'discountPercentTill': 5.5,
'markupPercentEnabled': False,
'markupPercentFrom': 0.0,
'markupPercentIsFractional': False,
'markupPercentTill': 0.0,
'priceEditEnabled': True},
'refundPricePolicy': {'discountPercentEnabled': False,
'discountPercentFrom': 0.0,
'discountPercentIsFractional': False,
'discountPercentTill': 0.0,
'markupPercentEnabled': True,
'markupPercentFrom': 5.5,
'markupPercentIsFractional': True,
'markupPercentTill': 7.7,
'priceEditEnabled': False}},
{'dateFrom': '2018-01-01T00:00:00Z',
'id': '174ead71-d11a-427f-9a15-70dd3606b51e',
'name': 'string4'},
{'dateFrom': '2018-01-01T00:00:00Z',
'fetchMethod': 'byRelationships',
'id': 'b89975cc-5043-4bb9-9aa5-a2330b217d74',
'name': '666',
'vendorId': '666'},
{'dateFrom': '2018-01-01T00:00:00Z',
'id': '6bc4381a-b7a1-4c35-98a2-ab58400c8689',
'name': 'string5'},
{'dateFrom': '2018-01-01T00:00:00Z',
'id': 'ee16712c-f63c-492d-a4f5-cc6158f77a4e',
'name': 'string6',
'orderPricePolicy': {'discountPercentEnabled': True,
'discountPercentFrom': 10.0,
'discountPercentIsFractional': False,
'discountPercentTill': 30.0,
'markupPercentEnabled': True,
'markupPercentFrom': 40.0,
'markupPercentIsFractional': False,
'markupPercentTill': 70.0,
'priceEditEnabled': True},
'vendorId': '1050'},
{'dateFrom': '2018-01-01T00:00:00Z',
'id': 'bf57c2b6-035f-4c21-b8c2-e102eed032d1',
'name': 'string7'}],
'vendorId': '232231'}]
for i in range(len(alltext)):
Namereponsibleroutes = alltext[i]['routes']['name']
print(Namereponsibleroutes)
我希望输出string1,string2,string3,string4,string5,string6,string7,但是实际输出是string1
答案 0 :(得分:2)
alltext[i]['routes']
是词典的列表。因此,要从列表中的每个字典中获取键'name'
的值,您必须提取每个字典。您可以通过列表理解来做到这一点
Nameresponsibleroutes = [routeDict['name'] for routeDict in alltext[i]['routes']]
# I am assuming that alltext usually has more than one dictionary in it, otherwise if it is always only 1, change `i` to `0` or don't have it be a list