Python Serialization Pickle,json Module

Source: Internet
Author: User
Tags pprint

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.