Python pickle dump files

pickle — Python object serialization

Pickling Class Instances :
This section explains the general mechanisms available to define, customize, and control how class instances are pickled and unpickled.
No additional code is needed to make instances pickable. By default, pickle will retrieve the class and the attributes of an instance via introspection.
Classes can alter the default behaviour by providing one or several special methods :

  1. object.__getnewargs_ex__()
    This method dictates the values passed to the __new__() method upon unpickling. The method must return a pair (args, kwargs) where args is a tuple of positional arguments and kwargs a dictionary of named arguments for constructing the object.
  2. object.__getnewargs__()
    This method supports only positive arguments. It must return a tuple of arguments args which will be passed to the __new__() method upon unpickling.
  3. object.__getstate__()
    If this method is defined by classes, it is called and the returned object is pickled as the contents for the instance, instead of the contents of the instance’s dictionary.
  4. object.__setstate__(state)
    If this method is defined by classes, it is called with the unpickled state. The pickled state must be a dictionary and its items are assigned to the new instance’s dictionary.
  5. object.__reduce__()
    The __reduce__() method takes no argument and shall return either a string or preferably a tuple.
  6. object.__reduce_ex__(protocol)
    This method is similar to __reduce__ method. It takes a single integer argument. The main use for this method is to provide backwards-compatible reduce values for older Python releases.
Читайте также:  Html button left margin

Example : Handling Stateful Objects
This example shows how to modify pickling behavior for a class. The TextReader class opens a text file, and returns the line number and line contents each time its readline() method is called.

  1. If a TextReader instance is pickled, all attributes except the file object member are saved.
  2. When the instance is unpickled, the file is reopened, and reading resumes from the last location.

Источник

Модуль pickle

Python 3 логотип

Модуль pickle реализует мощный алгоритм сериализации и десериализации объектов Python. «Pickling» — процесс преобразования объекта Python в поток байтов, а «unpickling» — обратная операция, в результате которой поток байтов преобразуется обратно в Python-объект. Так как поток байтов легко можно записать в файл, модуль pickle широко применяется для сохранения и загрузки сложных объектов в Python.

Не загружайте с помощью модуля pickle файлы из ненадёжных источников. Это может привести к необратимым последствиям.

Модуль pickle предоставляет следующие функции для удобства сохранения/загрузки объектов:

pickle.dump(obj, file, protocol=None, *, fix_imports=True) — записывает сериализованный объект в файл. Дополнительный аргумент protocol указывает используемый протокол. По умолчанию равен 3 и именно он рекомендован для использования в Python 3 (несмотря на то, что в Python 3.4 добавили протокол версии 4 с некоторыми оптимизациями). В любом случае, записывать и загружать надо с одним и тем же протоколом.

pickle.dumps(obj, protocol=None, *, fix_imports=True) — возвращает сериализованный объект. Впоследствии вы его можете использовать как угодно.

pickle.load(file, *, fix_imports=True, encoding=»ASCII», errors=»strict») — загружает объект из файла.

pickle.loads(bytes_object, *, fix_imports=True, encoding=»ASCII», errors=»strict») — загружает объект из потока байт.

Модуль pickle также определяет несколько исключений:

  • pickle.PickleError
    • pickle.PicklingError — случились проблемы с сериализацией объекта.
    • pickle.UnpicklingError — случились проблемы с десериализацией объекта.

    Этих функций вполне достаточно для сохранения и загрузки встроенных типов данных.

       , 'a': [1, 2.0, 3, (4+6j)], 'b': ('character string', b'byte string')>

    Для вставки кода на Python в комментарий заключайте его в теги

    Источник

    Python Pickle Example

    Python Pickle Example

    While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

    In this tutorial we will be discussing about Python Pickle Example. In our previous tutorial, we discussed about Python Multiprocessing.

    Python Pickle

    Python Pickle is used to serialize and deserialize a python object structure. Any object on python can be pickled so that it can be saved on disk. At first Python pickle serialize the object and then converts the object into a character stream so that this character stream contains all the information necessary to reconstruct the object in another python script. Note that the pickle module is not secure against erroneous or maliciously constructed data according to the documentation. So, never unpickle data received from an untrusted or unauthenticated source.

    Python Pickle dump

    In this section, we are going to learn, how to store data using Python pickle. To do so, we have to import the pickle module first. Then use pickle.dump() function to store the object data to the file. pickle.dump() function takes 3 arguments. The first argument is the object that you want to store. The second argument is the file object you get by opening the desired file in write-binary (wb) mode. And the third argument is the key-value argument. This argument defines the protocol. There are two type of protocol - pickle.HIGHEST_PROTOCOL and pickle.DEFAULT_PROTOCOL. See the sample code to know how to dump data using pickle.

    import pickle # take user input to take the amount of data number_of_data = int(input('Enter the number of data : ')) data = [] # take input of the data for i in range(number_of_data): raw = input('Enter data '+str(i)+' : ') data.append(raw) # open a file, where you ant to store the data file = open('important', 'wb') # dump information to that file pickle.dump(data, file) # close the file file.close() 

    python pickle dump

    The following program will prompt you to enter some input. In my case, it was like this.

    Python Pickle load

    To retrieve pickled data, the steps are quite simple. You have to use pickle.load() function to do that. The primary argument of pickle load function is the file object that you get by opening the file in read-binary (rb) mode. Simple! Isn’t it. Let’s write the code to retrieve data we pickled using the pickle dump code. See the following code for understanding.

    import pickle # open a file, where you stored the pickled data file = open('important', 'rb') # dump information to that file data = pickle.load(file) # close the file file.close() print('Showing the pickled data:') cnt = 0 for item in data: print('The data ', cnt, ' is : ', item) cnt += 1 
    Showing the pickled data: The data 0 is : 123 The data 1 is : abc The data 2 is : !@#$ 

    Python Pickle Example

    I made a short video showing execution of python pickle example programs - first to store data into file and then to load and print it. As you can see that the file created by python pickle dump is a binary file and shows garbage characters in the text editor.

    Important Notes on Python Pickle

    1. The pickle protocol is specific to Python - it’s not guaranteed to be cross-language compatible. This means you most likely can’t transfer the information to make it useful in other programming languages.
    2. There is also no guarantee of compatibility between different versions of Python because not every Python data structure can be serialized by the module.
    3. The latest version of the pickle protocol is used by default unless you manually change it.
    4. Last but not least, the pickle module is not secure against erroneous or maliciously constructed data according to the documentation.

    So, that’s all about python pickle example. Hope that you understand well. For any further query please use the comment section. 🙂 Reference: Official Documentation

    Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

    Источник

Оцените статью