Python Pickle Module

Source: Internet
Author: User

The Pickle module consists of the dumps () function, the loads () function, the dump () function, and the load () function.

#pickle. Dumps (' object ')     #序列化对象, the return value cannot be read directly
#pickle. Dump (' object ', f) #序列化对象到文件中
#pickle. Loads (' object ') #反序列化对象
#pickle. Load (f) #从文件中反序列对象, return to the original object
ImportPickleobj= 123,"ABCDEDF",["AC", 123],{"Key":"value","Key1":"value1"}#pickle.dumps (' object ') #序列化对象, the return value cannot be read directly#pickle.dump (' object ', f) #序列化对象到文件中#pickle.loads (' object ') #反序列化对象#pickle.load (f) #从文件中反序列对象, return the original objectR1=pickle.dumps (obj)Print(R1) R2=pickle.loads (R1)Print(R2)#f = open (' db ', ' rb+ ') # ' DB ' is the file name#pickle.dump (obj, f)#f.close ()With Open ('DB','rb+') as F:pickle.dump (obj, f) with open ('DB','RB') as F:r3=pickle.load (f)Print(R3)

Note: The pickle is binary, so the file should be opened with a B, such as ' WB ' or ' RB ' if only open with W or R will be an error

Python Pickle 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.