Dino Geek, try to help you

How to use pickle module in Python?


The `pickle` module in Python is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk. Pickling is a way to convert a python object (list, dict, etc.) into a character stream. The idea is that this character stream contains all the information necessary to reconstruct the object in another python script.

Here are some examples of how to use the `pickle` module in Python:

1. Store data (serialization)

You can use `pickle.dump()` to store a variable’s data permanently to a file:

```
import pickle

  1. an example dict
    grades = {‘John’: ‘A’, ‘Emily’: ‘A+’, ‘Betty’: ‘B’, ‘Mike’: ‘C’}
  1. write python dict to a file
    with open(‘myGrades.pkl’, ‘wb’) as f: pickle.dump(grades, f)
    ```
    In the example above, `pickle.dump()` is used for dumping the data, and the file is opened in write mode (‘wb’ as pickle data is binary data).

1. Load data (deserialization)

Use `pickle.load()` to load the data at a later point from the file:

```
import pickle

  1. read python dict back from the file
    with open(‘myGrades.pkl’, ‘rb’) as f: myGrades = pickle.load(f)

print(myGrades)
```
In the example above, the file is opened in read mode for binary ‘rb’. Then, the data is loaded back using `pickle.load()`.

1. Pickle a Python object

```
import pickle

  1. this is any python object
    some_obj = {‘id’: ‘123’, ‘name’: ‘Scott’, ‘jobs’: [‘dev’, ‘mgr’]}
  1. pickle the object
    pickled_obj = pickle.dumps(some_obj)

print(pickled_obj)
```
In this example, `pickle.dumps()` function is used for converting the python object into a character stream.

1. Unpickle a Python object

```
import pickle

  1. unpickle the object
    unpickled_obj = pickle.loads(pickled_obj)

print(unpickled_obj)
```
In the example above, `pickle.loads()` function is used to covert the character stream into the python object.

Note: Pickling is not recommended for use as a form of long-term, secure data storage. For more secure options, consider using the `json` or `sqlite3` modules instead. Also, never unpickle data that came from an untrusted source as it could execute arbitrary code.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use