Python's cpickle for file access

Source: Internet
Author: User

The computer's memory is stored in a binary sequence (of course, in the Linux eye, it is a text stream ). We can directly fetch the data from the location of an object, convert it to a text stream (the process is called serialize), and then deposit the text stream into a file. Since Python is referring to the class definition of an object when creating an object, when we read the object from the text, we must have the class definition of the object handy to know how to reconstruct the object. When reading from a file, for Python's built-in (built-in) objects (such as integers, dictionaries, tables, and so on), it is not necessary for us to define classes in the program because their class definitions are already loaded into memory. However, for a user-defined object, you must define the class before you can load the object from the file .

In Python, the Pickle class can typically be used to serialize Python objects, while Cpickle provides a faster and simpler interface, as the python document says: "Cpickle-A faster pickle".

Cpickle can serialize any type of Python object, such as List,dict, even objects of a class. The so-called serialization, my superficial understanding is to be able to complete the preservation and can be completely reversible recovery. In Cpickle, there are four main functions that can do this work, as described in the following examples.

1, dump: Serializes the Python object to a local file.
>>> Import Cpickle
>>> data = range (1000)
>>> Cpickle.dump (Data,open ("Test\\data.pkl", "WB"))
The dump function needs to specify two parameters, the first is the Python object name that needs to be serialized, the second is a local file, and it is important to note that you need to use the Open function to start a file and specify a write operation.


2. Load: Load local file, restore Python object
>>> data = cpickle.load (open ("Test\\data.pkl", "RB"))
Like dump, you need to open a local file using the Open function and specify the "read" operation


3. Dumps: Serializes the Python object into a string variable.

>>> data_string = cpickle.dumps (data)


4. Loads: Loading a Python object from a string variable
>>> data = cpickle.loads (data_string)


"Source code Example"

Import h5pyimport Cpickle#data_path = ' f:\\20150727_1010\\feat_name.mat ' data_path = ' f:\\wfpdm\\20150727_1010\\feat_ Name.mat ' #data_path = ' h:\\wfpdm\\20150727_1010\\feat_name.mat ' Root_path = ' f:\\wfpdm\\20150727_1010\\ ' #root_path = "H:\\wfpdm\\20150727_1010\\" myfile=h5py. File (Data_path, ' r ') Data_set = [myfile[element[0]][:]. T-element in myfile[' feat_name '] #data = [myfile[element[0]][:]. T-element in myfile[' Feat_name ']] #print data[0].shape #print data[1].shapeprint data_set[0].shapemyfile.close () Filename= ' tst.pkl ' FID = open (Root_path+filename, ' WB ') cpickle.dump (Data_set,fid) fid.close () FIM = open (root_path+ FileName, "RB") LoadData = Cpickle.load (FIM) fim.close () print Loaddata[0].shape


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python's cpickle for file access

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.