#JSON pickle shelve module serialization#JSON modules are used for data transfer between different languages, but the qualification data type: Str,int,list,dict, BooleanImportJSON#Four ways:#dumps and loads: For network data transfer and multiple data writes to a fileDict = {'wo':'Good_person','ni':'Bad_person'}ret= Json.dumps (Dict,ensure_ascii=false)#{"Wo": "Good_person", "ni": "Bad_person"}#convert Dict to serialized string Ensure_ascii=false: Chinese visual of StringPrint(ret)#deserializationPrint(Json.loads (ret)) Dict1= {'wo':'Good_person','ni':'Bad_person'}dict2= {'wo':'Good_person','ni':'Bad_person'}dict3= {'wo':'Good_person','ni':'Bad_person'}#multiple data written in one fileWith open ('Json_files','W', encoding='Utf-8') as F:ret1= Json.dumps (dict1,ensure_ascii=False) f.write (Ret1+'\ n') Ret2= Json.dumps (dict2,ensure_ascii=False) f.write (Ret2+'\ n') Ret3= Json.dumps (dict3,ensure_ascii=False) f.write (Ret3+'\ n') with open ('Json_files','R', encoding='Utf-8') as F1: forLineinchF1:Print(Json.loads (line)) Dict1= {'wo':'Good_person','ni':'Bad_person'}dump and load: Used to write a file in a file F= Open ('Json_file','W', encoding='Utf-8') Json.dump (dict1,f)#Serialization ofF.close () F= Open ('Json_file','R', encoding='Utf-8')Print(Json.load (f))#deserialization#pickle data transfer between Python languages is suitable for almost all data typesImportPickle#dumps loadsDict1 = {'wo':'Good_person','ni':'Bad_person'}ret= Pickle.dumps (Dict1)#bytes TypePrint(ret)Print(Pickle.loads (ret))#deserializationImportPickledict1= {'wo':'Good_person','ni':'Bad_person'}dict2= {'wo':'Good_person','ni':'Bad_person'}dict3= {'wo':'Good_person','ni':'Bad_person'}#Dump Load#Read and write to a file#f = open (' Pickle_files ', ' WB ')#pickle.dump (dict1,f)With Open ('Pickle_files','RB') as F1: whileTrue:Try: Print(Pickle.load (F1))exceptEoferror: Break
Module-json,pickle