【问题标题】:printing first 10 lines of my feed打印我的提要的前 10 行
【发布时间】:2018-08-15 02:07:23
【问题描述】:
import json 

with open('output.json', encoding='utf-8') as json_file:  
   output = json.loads(json_file.read())
feeds = []
for feed in output ['posts']:
   feeds.append (feed)
print (feeds[1]['title'])

我正在尝试仅打印数据的前 10 行。我尝试了“枚举”和其他代码,但它们似乎都不起作用。关于如何仅获得输出的前 10 个标题的任何想法?

【问题讨论】:

  • in output ['posts'][:10]?

标签: python json feed


【解决方案1】:

使用[:10] 作为切片可以让您最多获得前 10 个元素:

import json

with open('output.json', encoding='utf-8') as json_file:  
    output = json.loads(json_file.read())
feeds = []
for feed in output ['posts'][:10]: #   <---- Change on this line
    feeds.append (feed)
print (feeds[1]['title'])

【讨论】:

  • 嗨,Alex 感谢您的回复。您建议的代码仍然只打印提要 [1] 所示的第一行。如果我想要前 10 行数据该怎么办?
  • 您好,我尝试了以下方法并且成功了:feeds = []for feed in output ['posts'][:10]: print (str(feed['title'])) feeds.append (饲料)
猜你喜欢
  • 1970-01-01
  • 2016-09-03
  • 2021-12-20
  • 1970-01-01
  • 2016-09-02
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2021-12-19
相关资源
最近更新 更多