python - 打印我的提要的前 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']:
feeds.append (feed)
print (feeds[1]['title'])
我试图只打印数据的前 10 行。我尝试了“枚举”和其他代码,但它们似乎都不起作用。关于如何仅获得输出的前 10 个标题的任何想法?
解决方案
用作[: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'])
推荐阅读
- jupyterhub - 我可以在 Ubuntu 20.04 上安装 JupyterHub 吗?
- fluid - 使用 TCA 选择多个 => 1 时,流体中只有一项可用
- android - `android.packageBuildConfig=false` 已弃用
- c++ - 为什么 qt 对象会丢失它们的值?
- google-cloud-build - 是否可以从 GCB 步骤构建和启动的容器中访问服务帐户凭据?
- javascript - 如何在 Web 组件中使用标签的 querySelector
- python - pytoch RuntimeError: Dimension out of range (expected to be in range of [-1, 0], but got 1
- sql - Postgres 模式添加列条件
- python - 减法赋值给出 0 而不是负数
- javascript - 使用 addScriptTag 时如何使用类型模块评估脚本的结果