Python json write serialized content to json file

import uuid
from datetime import datetime
import threading
import psutil
import os
import json
import asyncio
import time
from tkinter import messagebox

idx=0
def get_memory():
    global idx
    pid=os.getpid()
    proc=psutil.Process(pid)
    mem_info=proc.memory_info()
    rss_mem=f'{mem_info.rss/1024/1024:.2f} M'
    vms_mem=f'{mem_info.vms/1024/1024:.2f} M'

    pc_mem=psutil.virtual_memory()
    total_mem=f'{pc_mem.total/1024/1024/1024:.2f} G'
    avail_mem=f'{pc_mem.available/1024/1024/1024:.2f} G'
    used_rate=f'{pc_mem.percent} %'
    return f'Idx:{idx},{datetime.now()},rss mem:{rss_mem},vms mem:{vms_mem},total mem:{total_mem},avail mem:{avail_mem},used rate:{used_rate}'

def get_time_uuid():
    global idx
    idx+=1
    return f'{idx}_{datetime.now().strftime('%Y%m%d%H%M%S%f')}_{uuid.uuid4().hex}'

def get_uuid_time():
    global idx
    idx+=1
    return f'{idx}_{uuid.uuid4().hex}_{datetime.now().strftime('%Y%m%d%H%M%S%f')}'

class Book:
    def __init__(self,id,name,isbn,author,abstract,comment,content,summary,title,topic):
        self.id=id
        self.name=name
        self.isbn=isbn
        self.author=author
        self.abstract=abstract
        self.comment=comment
        self.content=content
        self.summary=summary
        self.title=title
        self.topic=topic
    
    def to_dict(self):
        return {
            'Id':self.id,
            'Name':self.name,
            'ISBN':self.isbn,
            'Author':self.author,
            'Abstract':self.abstract,
            'Comment':self.comment,
            'Content':self.content,
            'Summary':self.summary,
            'Title':self.title,
            'Topic':self.topic
        }



if __name__=='__main__':
   
    data_list=[]
    arr=range(1,20000001)
    for a in arr:
        idx+=1
        bk=Book(a,f'Name_{a}',f'ISBN_{a}_{uuid.uuid4().hex}',f'Author_{a}',f'Abstract_{a}',f'Comment_{a}',f'Content_{a}',f'Summary_{a}',f'Title_{a}',f'Topic_{a}')
        data_list.append(bk.to_dict())
        if a%1000000==0:
            print(get_memory())
    
    json_file=f'Json_{datetime.now().strftime('%Y%m%d%H%M%S%f')}.json'
    with open(json_file,'w',encoding='utf-8-sig') as json_write_file:
        json.dump(data_list,json_write_file,indent=4)
    
    print(f'{datetime.now()},write {len(data_list)} to {json_file}')
    messagebox.showinfo(datetime.now(),f'Write {len(data_list)} to {json_file}')

 

 

image

 

 

image

 

 

 

image

 

posted @ 2026-02-22 20:38  FredGrit  阅读(4)  评论(0)    收藏  举报