Python implements the compression method during pickling, pythonpickling

Source: Internet
Author: User

Python implements the compression method during pickling, pythonpickling

This article describes how to compress python during pickling. Share it with you for your reference.

The specific method is as follows:

Import cPickle, gzipdef save (filename, * objects): fil1 = gzip. open (filename, 'wb ') for obj in objects: cPickle. dump (obj, fil1, protocol = 2) fil1.close () def load (filename): fil1 = gzip. open (filename, 'rb') while True: try: yield cPickle. load (fil1) failed t EOFError: break fil1.close () data1 = ['abc',] # Test Data data2 = {1: 'aaa', "B ": 'Dad '} data3 = (, 4) data = list(1_data1,data2,data31_1_save('data.zip', data) iter = load('data.zip ') for item in iter: for data in item: print data

The test environment of this example is Python2.7.6.

The program running result is as follows:

['abc', 12, 23]{1: 'aaa', 'b': 'dad'}(1, 2, 4)

The data.zip file is generated under the same directory at the same time as the program runs.

I hope this article will help you learn Python programming.


We need to use python to compress 16-bit hexadecimal data. What is the best compression method?

Use the built-in zip/inflate/deflate in py.
 
Python compression backup Problems

Problem: I didn't test your code under Python2.7, But I tested it under Python3.2 because I didn't install version 2.7. However, it doesn't matter. Its string usage and meaning are the same. I tested it. It should be an error: target_dir = r 'd: \ bak \\'. There is no syntax problem in this sentence. This is also the case. However, the value of target_dir may not be what we think. You can use print to output the value of target_dir, the result I entered in 3.2 is: D: \ bak \\. This is not true. A backslash (\) is added, leading to a path error in Windows.

Solution: Add target_dir = r 'd: \ bak \ 'with the following sentence: target_dir = target_dir [0:-1]. In this way, the last backslash is cleared, and the path is correct! It should be okay. I directly read the code and find that there is a problem with this sentence. I will test this sentence separately. Other code is not tested. However, it should be okay to check the syntax!

In addition, your suffix is .rar, and the compression command is rar, so the backup file name should be rarfile; in Windows, I have never used the rar command line compression command, and it is not clear that it should be rarfile. As for the folder you asked, you can not back up the folder or the directory under the folder. This is not what Python manages (at least in your code, it depends on your Windows rar program support is not supported; if it is supported, you can, if not, not. Cause: OS. the system () function is similar to system () in C/C ++. It submits the parameters passed to it to the system environment (that is, to run the Python program operating system) for execution, this is the same as the direct command under the command line in the corresponding system environment. The difference is that we use a script to save the code for automatic (routine service) or manually and periodically back up data!

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.