Persistent Object Storage and Encoding/Decoding

At times, it becomes necessary to store data like strings, lists, dictionaries, tuples, and other structures permanently. For this purpose, Python's pickle module proves useful. This module allows conversion of objects into formats suitable for storage or retrieval.

The pickle module facilitates serialization and deserialization processes. Serializasion enables saving program objects to files, achieving persistent storage. Deserialization retrieves these stored objects back into memory.

Always remember to close file handles after reading, e.g., f1.close().

Sample list

data_list = [12, 2, 3]

Writing to file

with open('data.pkl', 'wb') as file_handle: pickle.dump(data_list, file_handle)

Reading from file

with open('data.pkl', 'rb') as file_handle: retrieved_list = pickle.load(file_handle) print(retrieved_list)


</div>Using the context manager (`with`) eliminates the need to manually close files, simplifying code.

<div>```
# Reading object from file
with open("data.pkl", "rb") as input_file:
    loaded_object = pickle.load(input_file)

# Writing object to file
with open("data.pkl", "wb") as output_file:
    pickle.dump(data_list, output_file)

Encoding converts strings into bytes using a specified encoding format.

Decoding converts bytes back into strings using the same encoding format.

Common encoding standards:

  • GB2312: A Chinese character encoding standard, primarily for Simplified Chinese characters.
  • GBK: An extension of GB2312, supporting both Simplified and Traditional Chinese, plus Japanese kana.
  • cp936: The default encoding used in Windows Command Prompt, equivalent to GB2312.
  • Unicode: A universal character encoding standard accommodating all world languages and symbols. UTF-8, UTF-16, and UTF-32 are its implementations.
  • UTF-8: Widely adopted encoding for Unicode. It uses variable-length byte sequences to represent characters. ASCII characters are represented by single bytes, making ASCII a subset of UTF-8.

Decoding

with open("text_file.txt", "rb") as read_file: raw_bytes = read_file.read() decoded_string = raw_bytes.decode("utf-8") print(decoded_string)


</div></div>

Tags: python pickle encoding decoding serialization

Posted on Mon, 22 Jun 2026 19:01:28 +0000 by cassius