Serialization of Python-pickle

Source: Internet
Author: User

Pickle is a Python data interaction that serializes the data and reads it when needed

Serialization of Pickle
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import pickledef Test ():    print (' test ') info = {    ' name ': ' John ', c2/> ' age ': $,    ' sex ': ' Male ',    ' func ': test}f = open (' Pickle.txt ', ' WB ') #pickle序列化生成的数据为二进制f. Write ( Pickle.dumps (Info)) #另一种写法: Pickle.dump (info,f) f.close ()

The resulting file

Some messy data.

Pickle deserialization
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import pickledef Test ():    print (' test ')    print (' test_1 ') F = open ( ' Pickle.txt ', ' RB ') data = Pickle.loads (F.read ()) #另一种写法: data = Pickle.load (f) print (data) print (data[' func ') ())

Run results

Pickle serialization can not only store data, but also store functions

Pickle deserialization can call a function that is deserialized, as long as the function name is the same, the content does not have to be the same

Serialization of Python-pickle

Related Article

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.