首页 > 解决方案 > 如何通过单独文件中特定数量的对象拆分 JSON 文件?

问题描述

import json


content = []
with open("articles.jsonl", "rt") as file:
    for a in file:
        out = json.loads(a)
        content.append(out)
file.close()

count = 0
file_count = 1
with open("articles" + str(file_count) + ".jsonl", "wt") as fp:
    for a in content:
        json.dump(a, fp)
        fp.write("\n")
        count +=1
        if count == 2000:
            file_count +=1
            count = 0
            continue
fp.close()  

{"id": "f7ca322d-c3e8-40d2-841f-9d7250ac72ca", "content": "VETERANS saluted Worcester's first ever breakfast club for ex-soldiers which won over hearts, minds and bellies. \n \nThe Worcester Breakfast Club for HM Forces Veterans met at the Postal Order in Foregate Street at 10am on Saturday. \n \nThe club is designed to allow veterans a place to meet, socialise, eat and drink, giving hunger and loneliness their marching orders. \n \nFather-of-two Dave Carney, aged 43, of Merrimans Hill, Worcester, set up the club after being inspired by other similar clubs across the country. \n \nHe said: \"As you can see from the picture, we had a good response. Five out of the 10 that attended said they saw the article in the newspaper and turned up. \n \n\"We even had an old chap travel from Droitwich and he was late on parade by three hours. \n \n\"It's generated a lot of interest and I estimate (from other veterans who saw the article) that next month's meeting will attract about 20 people. Onwards and upwards.\" \n \nHe said the management at the pub had been extremely hospitable to them. \n \nMr Carney said: \"They bent over backwards for us. They really looked after us well. That is the best choice of venue I could have made. They even put 'reserved for the armed forces'. \n   Promoted stories   \nThe reserve veteran with the Royal Engineers wanted to go to a breakfast club but found the nearest ones were in Bromsgrove and Gloucester so he decided to set up his own, closer to home. \n \nHe was influenced by Derek Hardman who set up a breakfast club for veterans in Hull and Andy Wilson who set one up in Newcastle. He said the idea has snowballed and there were now 70 similar clubs across the country and even some in Germany. \n \nMr Carney said with many Royal British Legion clubs closing he wanted veterans and serving personnel to feel they had somewhere they could go for good grub, beer and banter to recapture the comradery of being in the forces. \n \nThe Postal Order was chosen because of its central location and its proximity to the railway station and hotels and reasonably priced food and drink. \n \nThe management of the pub have even given the veterans a designated area within the pub. \n   \n Share article  \n   \nThe next meeting is at the Postal Order on Saturday, October 3 at 10am. \n \nThe breakfast club meets on the first Saturday of each month for those who want to attend in future.", "title": "Worcester breakfast club for veterans gives hunger its marching orders", "media-type": "News", "source": "Redditch Advertiser", "published": "2015-09-07T10:16:14Z"}

以上是articles.jsonl 文件的一个小样本。

这只是将所有内容写入一个名为articles1.jsonl 的文件,而不是具有一组特定对象的多个文件。有什么建议么?

标签: jsonpython-3.x

解决方案


推荐阅读