Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the pickle module of Python data serialization

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Python data serialization of the pickle module how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that everyone after reading this Python data serialization of the pickle module how to use the article will have a harvest, let's take a look.

Foreword:

In English, the noun pickle is kimchi, and the verb is pickled. It can be understood as pickling something up and saving it into a file, reading it and washing it when you want to use it.

Python's pickle module implements basic data serialization and deserialization.

Serialized objects can be saved on disk and read when needed. Any object can perform serialization operations.

The essence of pickle is to restore Python data to binary data in memory for users to transfer and store.

The pickle module is usually used in two scenarios:

Save the Python data as a binary file on disk, read and restore it to Python data when needed, which can be printable basic data (strings, lists, dictionaries, etc.) or non-printable class data. By contrast, only printable basic data can be saved using Json

Serialize the created class data and save it as a variable. Directly read the variable and restore it to class data the next time the program runs, omitting the process of creating class data

1 store Python data as a local file

Design a program, the output is Excel workbook output.xlsx, there is a worksheet out_sheet in the workbook is copied from the in_sheet in the standard document input.xlsx.

The content of the worksheet in_sheet is so large that it is difficult to reproduce it manually. We usually choose to use the standard document input.xlsx as the reference material for the program in the project. Copy the worksheet in_sheet to the worksheet out_sheet while the program is running.

If we require that the worksheet in_sheet file can not be stored in clear text, we can only store the Worksheet () data corresponding to the worksheet, and we will use the pickle module

① stored procedure import picklefrom openpyxl import load_workbook# creates class data wb = load_workbook ('input.xlsx') ws = wb [' in_sheet'] # copy in_sheet to out_sheet# Note: usr_copy_sheet () here is a custom method Copy_sheet () included with openpyxl cannot copy worksheetusr_copy_sheet (ws, out_ws) in different workbook # convert out_ws to binary data obj = pickle.dumps (out_ws) # save binary data to a local file, you must use binary to write with open (file='data.txt', mode='wb') as f: f.write (obj)

At this point, the ws is stored in the local file data.txt, and the opening is binary garbled.

② loading process import picklefrom openpyxl.worksheet.worksheet import Worksheet# creates empty class data ready to receive data. If the class has a parent attribute, it must be consistent with the data to be received out_ws= Worksheet (parent=out_wb) # to load the local file data onto the class data, you must open the file with open (file='data.txt', mode='rb') as f: out_ws= pickle.loads (f.read ()) using binary

You can save the process of reading worksheet in_sheet, copying to worksheet out_sheet, and so on.

2 store Python data as part of the program

If we require that the data of the worksheet in_sheet file not only cannot be stored in clear text, but also cannot be used as an external file, it must be part of the program .exe in case it is lost. Some Mini Program has only one exe file altogether, so it is not convenient to plug in one file. You can use the pickle module at this time.

① stored procedure import picklefrom openpyxl import load_workbook# creates class data wb = load_workbook ('input.xlsx') ws = wb [' in_sheet'] # copy in_sheet to out_sheet# Note: usr_copy_sheet () here is a custom method Copy_sheet () included with openpyxl cannot copy worksheetusr_copy_sheet (ws, out_ws) in different workbook # convert out_ws to binary data obj = pickle.dumps (out_ws) # Save binary data as py file You must use text to write with open (file='out_sheet.py', mode='w') as f: # to convert binary data into a string of baked binary xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.replace ('b\','b\') +'\''f.write (data) in the py file.

At this point, the out_ws is stored in the local file out_sheet.py, which is a string of binary codes:

Data=b'''\ x80\ x04.loading ② process

Load the generated out_sheet.py into the program and read the data directly

Import picklefrom openpyxl.worksheet.worksheet import Worksheetimport out_sheet# creates empty class data ready to receive data. If the class has a parent attribute, it must be consistent with the data to be received out_ws= Worksheet (parent=out_wb) # read the data variable out_ws= pickle.loads (out_sheet.data) in the out_sheet.py file directly

Out_sheet.py will eventually be compiled as part of the program

This is the end of the article on "how to use the pickle module for Python data serialization". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use the pickle module of Python data serialization". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report