打印我提要的前 10 行
printing first 10 lines of my feed
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 行。我尝试了 'enumerate' 和其他代码,但其中 none 似乎有效。关于如何只获得输出的前 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'])
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 行。我尝试了 'enumerate' 和其他代码,但其中 none 似乎有效。关于如何只获得输出的前 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'])