Two modules for serialization in Python
- JSON is used to convert between "string" and "Python Basic data Types"
- Pickle for "Python-specific type" and "Python basic data type" to convert between
The JSON module provides four functions: dumps, dump, loads, load
The Pickle module provides four functions: dumps, dump, loads, load
PICKLE.DUMP[OJB, file [, Protocol]
Serializes the object and writes the resulting data stream to the file object.
Required parameter OJB represents the object to encapsulate
The required parameter file represents the OJB write to the Files object, and the filename file must be binary mode open, such as ' RB '
The parameter protocol is a serialization mode with a default value of 0.
Pickle.load (file)
Deserializes the object. Resolves the data in a file to a Python object.
Required parameter file must be opened in binary readable mode, "RB", other optional parameters
Pickle.dumps (obj)
Returns the encapsulated object as a byte object without writing to the file
Pickle.loads (Bytes_object):
Reads the encapsulated object from a byte object and returns
There are three kinds of exceptions that can occur with pickle:
1. Pickleerror: Exception classes that occur when encapsulating and unpacking, inherited from exception
2. Picklingerror: Exception that occurs when a non-encapsulated object is encountered, inherited from Pickleerror
3. Unpicklingerror: An exception occurred during the unpacking of the object, inherited from Pickleerror
Here is an example of a modified file:
ImportPickleaccount= { 'xiaoming' :{ 'name':'Mingrizhaoyang', 'Emai':'[email protected]', 'Password':'abc123', 'Balance': 10000, 'BANK_ACC' : { 'ICB':'23123123123', 'CCB':'28321831823123' } }, 'Xiaohong' :{ 'name':'Buzhibujue', 'Emai':'[email protected]', 'Password':'qaq123', 'Balance': 10000, 'BANK_ACC' : { 'ICB':'12372847384', 'CCB':'12324452661'}}}with Open ('Acc_data','WB') as F:pickle.dump (account, F)
Import Pickle,pprintwith Open ('acc_data'rb') as FB: = pickle.load (FB) pprint.pprint (data)
ImportPICKLEFB= Open ('Acc_data','RB') Ac_data= Pickle.load (FB)#也可以c_data = Pickle.loads (Fb.read ())ac_data["Xiaohong"]['Balance'] = ac_data["Xiaohong"]['Balance']-1000Fb.close () with open ('Acc_data','WB') as F:pickle.dump (Ac_data, F) #也可以 F.write (Pickle.dumps (ac_data) )
The JSON usage is basically the same as Pickele, and the operation of the file does not have to go binary.
ImportJsonaccount= { 'xiaoming' :{ 'name':'Mingrizhaoyang', 'Emai':'[email protected]', 'Password':'abc123', 'Balance': 10000, 'BANK_ACC' : { 'ICB':'23123123123', 'CCB':'28321831823123' } }, 'Xiaohong' :{ 'name':'Buzhibujue', 'Emai':'[email protected]', 'Password':'qaq123', 'Balance': 10000, 'BANK_ACC' : { 'ICB':'12372847384', 'CCB':'12324452661'}}}with Open ('Acc_data','W') as F:json.dump (account, F)
Import Jsonwith Open ('acc_data'R') as FB: = Json.load (FB) print(data)
ImportJSON#with open (' Acc_data ', ' R ') as FB:#data = json.load (FB)FB = open ('Acc_data','R') Data=json.load (FB) data["Xiaohong"]['Balance'] = data["Xiaohong"]['Balance']-1000With Open ('Acc_data','W') as F:json.dump (data, F)
Python Serialization Pickle,json Module