Как распечатать несколько объектов в JSON, используя python? - PullRequest
0 голосов
/ 23 апреля 2020

У меня JSON данные выглядят так:

data2 = [{'name': '', 'steps': [{'number': 1, 'step': 'Place olive oil on the bottom of your Instant Pot before adding chicken breasts to the Instant Pot.', 'ingredients': [{'id': 4053, 'name': 'olive oil', 'image': 'olive-oil.jpg'}], 'equipment': [{'id': 414093, 'name': 'instant pot', 'image': ''}]}, {'number': 2, 'step': 'Season chicken breast with salt and pepper.', 'ingredients': [{'id': 1102047, 'name': 'salt and pepper', 'image': 'salt-and-pepper.jpg'}], 'equipment': []}, {'number': 3, 'step': 'Close Instant Pot and make sure the pressure gauge is turned to seal. Set pressure to manual, high pressure and set the timer for 8 minutes. Pressure will build and then the timer will start.', 'ingredients': [], 'equipment': [{'id': 414093, 'name': 'instant pot', 'image': ''}, {'id': 404695, 'name': 'kitchen timer', 'image': 'kitchen-timer.jpg'}], 'length': {'number': 8, 'unit': 'minutes'}}, {'number': 4, 'step': 'When the timer goes off, quick release the Instant Pot by turning pressure gauge to vent. When pressure releases shred chicken breast in the Instant Pot and serve any way you like.', 'ingredients': [], 'equipment': [{'id': 414093, 'name': 'instant pot', 'image': ''}, {'id': 404695, 'name': 'kitchen timer', 'image': 'kitchen-timer.jpg'}]}]}]

И я хочу получить число и шаг, чтобы мой выводной отпечаток выглядел так:

1 Place olive oil on the bottom of your Instant Pot before adding chicken breasts to the Instant Pot.
2 Season chicken breast with salt and pepper.
3 Close Instant Pot and make sure the pressure gauge is turned to seal. Set pressure to manual, high pressure and set the timer for 8 minutes. Pressure will build and then the timer will start.
4 When the timer goes off, quick release the Instant Pot by turning pressure gauge to vent. When pressure releases shred chicken breast in the Instant Pot and serve any way you like.

Мой текущий код выглядит так:

for item in data2[0]['steps']:
    print(item['step'])

И это только распечатывает все шаги, но не число. Как я могу распечатать значение «число» также?

1 Ответ

1 голос
/ 23 апреля 2020

Вы можете попробовать следующий код:

In [22]: steps = data2[0].get('steps',None)

In [23]: steps
Out[23]:
[{'number': 1,
  'step': 'Place olive oil on the bottom of your Instant Pot before adding chicken breasts to the Instant Pot.',
  'ingredients': [{'id': 4053, 'name': 'olive oil', 'image': 'olive-oil.jpg'}],
  'equipment': [{'id': 414093, 'name': 'instant pot', 'image': ''}]},
 {'number': 2,
  'step': 'Season chicken breast with salt and pepper.',
  'ingredients': [{'id': 1102047,
    'name': 'salt and pepper',
    'image': 'salt-and-pepper.jpg'}],
  'equipment': []},
 {'number': 3,
  'step': 'Close Instant Pot and make sure the pressure gauge is turned to seal. Set pressure to manual, high pressure and set the timer for 8 minutes. Pressure will build and then the timer will start.',
  'ingredients': [],
  'equipment': [{'id': 414093, 'name': 'instant pot', 'image': ''},
   {'id': 404695, 'name': 'kitchen timer', 'image': 'kitchen-timer.jpg'}],
  'length': {'number': 8, 'unit': 'minutes'}},
 {'number': 4,
  'step': 'When the timer goes off, quick release the Instant Pot by turning pressure gauge to vent. When pressure releases shred chicken breast in the Instant Pot and serve any way you like.',
  'ingredients': [],
  'equipment': [{'id': 414093, 'name': 'instant pot', 'image': ''},
   {'id': 404695, 'name': 'kitchen timer', 'image': 'kitchen-timer.jpg'}]}]

In [24]: for step in steps:
    ...:     print(step['number'],' ',step['step'])
    ...:
1   Place olive oil on the bottom of your Instant Pot before adding chicken breasts to the Instant Pot.
2   Season chicken breast with salt and pepper.
3   Close Instant Pot and make sure the pressure gauge is turned to seal. Set pressure to manual, high pressure and set the timer for 8 minutes. Pressure will build and then the timer will start.
4   When the timer goes off, quick release the Instant Pot by turning pressure gauge to vent. When pressure releases shred chicken breast in the Instant Pot and serve any way you like.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...