""" 读取json数据到list """
def read_json_list ( json_file) : if not ( os. path. exists( json_file) and os. path. isfile( json_file) ) : with open ( json_file, 'w' ) as f: f. write( '[]' ) with open ( json_file, 'r' , encoding= 'utf-8' ) as f: try : school_list = json. load( f) except : school_list = [ ] return school_list
""" 以追加方式向json文件追加list数据 """
def add_to_josn ( school_brochure) : remove_last_char( json_file) with open ( json_file, 'a' , encoding= 'utf-8' ) as f: if os. path. getsize( json_file) > 10 : f. write( ',' ) f. write( json. dumps( school_brochure, ensure_ascii= False ) ) f. write( "]" )
""" 删除文件最后一个字符 """
def remove_last_char ( json_file) : with open ( json_file, 'rb+' ) as f: f. seek( - 1 , os. SEEK_END) f. truncate( )