Serialization and compression of python

Source: Internet
Author: User

When to use serialization?

For example, I have a very complex data structure (similar to a dictionary, key and value are basic objects of python) and I want to store them in a database. What are you going to do, split one item and store it separately? Answer: no.

Now we can use serialization to directly convert the data structure into byte strings, which are stored in the database. Directly retrieve thisByte stringTo restore the string.

Common modules are marshal and cPickle.

They all use the dump, dumps, load, and loads methods. According to some professional tests, the speed of marshal is faster than that of cPickle. cPickle is written by C, which is also super fast. If a long string exists, cPickle is faster.

At the same time, cPickle can be used with gzip to compress the converted data to the shortest byte string, saving space and storage.

 

Import cPickle, gzip
Def save (filename, * objects ):
Fil = gzip. open (filename, 'wb ')
For obj in objects:
CPickle. dump (obj, fil, proto = 2)
Fil. close ()
Def load (filename ):
Fil = gzip. open (filename, 'rb ')
While True:
Try:
Yeild cPickle. load (fil)
Failed t EOFError: break
Fil. close ()

For example, to process the list of 45000 strings, after cPickle uses protocol 0 in dump, the size of the disk file generated is 972KB, and Protocol 2 only needs kb. If both gzip and Protocol 2 are used, the generated file is 268KB, but the compression of Protocol 0 is actually the best. After testing, the files generated by Protocol 0 and gzip are 252KB.

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.