【问题标题】:How can I print out multiple object in JSON using python?如何使用 python 在 JSON 中打印多个对象?
【发布时间】:2020-04-23 01:17:45
【问题描述】:

我的 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'])

这只会打印出所有步骤,而不是数字。我怎样才能打印出“数字”的值呢?

【问题讨论】:

  • 尝试项目["number"]
  • 你可以这样做:print(f"{item['number']} {item['step']}")
  • print(item['number'], item['step'])
  • 但我必须使用 for 循环 for item in data2[0]['step'] 才能访问每个项目...
  • 没问题,上面的代码就可以了。

标签: python json for-loop


【解决方案1】:

你可以试试下面的代码:

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.

【讨论】:

  • 哇,它就像魔术一样工作。谢谢!我从来不知道字典上的 .get 函数。我正在尝试其他数据...非常感谢
  • 我想马上做,但它告诉我要等几分钟......但我不会忘记!
猜你喜欢
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
  • 2015-05-13
  • 2016-02-17
  • 2016-08-29
  • 2017-12-08
  • 2018-10-03
  • 1970-01-01
相关资源
最近更新 更多