json多个对象的文件读写

文件样子

{
    
    "id": "777.jpg", "num": 88}
{
    
    "id": "777.jpg", "num": 0}
{
    
    "id": "777.jpg", "num": 1}
{
    
    "id": "777.jpg", "num": 2}
{
    
    "id": "777.jpg", "num": 3}
{
    
    "id": "777.jpg", "num": 4}
{
    
    "id": "777.jpg", "num": 5}
{
    
    "id": "777.jpg", "num": 6}
{
    
    "id": "777.jpg", "num": 7}
{
    
    "id": "777.jpg", "num": 8}
{
    
    "id": "777.jpg", "num": 9}
{
    
    "id": "777.jpg", "num": 10}

写入的模块(封装):

import json

每写一行要写入一个换行符哦

def write_jsonlines(json_file_name,date_dict):
    with open(json_file_name, 'a') as f:
        json.dump(date_dict, f)
        f.write("\n")

读入模块(已封装):

import jsonlines
def json_data_read(json_file_name):
    """输入:需要读取的多对象json文件路径
    返回:由多个字典组成的列表"""

    json_list = []
    with open(json_file_name, 'r+') as f:
        for item in jsonlines.Reader(f):
            json_list.append(item)

    return  json_list

猜你喜欢

转载自blog.csdn.net/weixin_43134049/article/details/117612789
今日推荐