
- Python 基礎
- Python - 首頁
- Python - 概述
- Python - 歷史
- Python - 特性
- Python vs C++
- Python - Hello World 程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境搭建
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode 系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python 控制語句
- Python - 控制流
- Python - 決策制定
- Python - if 語句
- Python - if else
- Python - 巢狀 if
- Python - Match-Case 語句
- Python - 迴圈
- Python - for 迴圈
- Python - for-else 迴圈
- Python - while 迴圈
- Python - break 語句
- Python - continue 語句
- Python - pass 語句
- Python - 巢狀迴圈
- Python 函式與模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 僅限關鍵字引數
- Python - 位置引數
- Python - 僅限位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python 字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python 列表
- Python - 列表
- Python - 訪問列表元素
- Python - 修改列表元素
- Python - 新增列表元素
- Python - 刪除列表元素
- Python - 迴圈遍歷列表
- Python - 列表推導式
- Python - 排序列表
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python 元組
- Python - 元組
- Python - 訪問元組元素
- Python - 更新元組
- Python - 解包元組
- Python - 迴圈遍歷元組
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python 集合
- Python - 集合
- Python - 訪問集合元素
- Python - 新增集合元素
- Python - 刪除集合元素
- Python - 迴圈遍歷集合
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python 字典
- Python - 字典
- Python - 訪問字典元素
- Python - 修改字典元素
- Python - 新增字典元素
- Python - 刪除字典元素
- Python - 字典檢視物件
- Python - 迴圈遍歷字典
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python 陣列
- Python - 陣列
- Python - 訪問陣列元素
- Python - 新增陣列元素
- Python - 刪除陣列元素
- Python - 迴圈遍歷陣列
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python 檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS 檔案/目錄方法
- Python - OS 路徑方法
- 面向物件程式設計
- Python - OOPs 概念
- Python - 類與物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝類
- Python - 列舉
- Python - 反射
- Python 錯誤與異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except 塊
- Python - try-finally 塊
- Python - 丟擲異常
- Python - 異常鏈
- Python - 巢狀 try 塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python 多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 加入執行緒
- Python - 執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python 同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python 網路程式設計
- Python - 網路程式設計
- Python - Socket 程式設計
- Python - URL 處理
- Python - 泛型
- Python 庫
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 雜項
- Python - 日期與時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI 程式設計
- Python - XML 處理
- Python - GUI 程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送郵件
- Python - 擴充套件
- Python - 工具/實用程式
- Python - GUIs
- Python 高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - 猴子補丁
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python 有用資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 有用資源
- Python - 討論
- Python 編譯器
- NumPy 編譯器
- Matplotlib 編譯器
- SciPy 編譯器
Python - 序列化
Python中的序列化
序列化是指將物件轉換為易於儲存、傳輸或稍後重建的格式的過程。在Python中,這涉及將複雜的資料結構(例如物件或字典)轉換為位元組流。
我們為什麼要使用序列化?
序列化允許資料輕鬆儲存到磁碟或透過網路傳輸,然後恢復到其原始形式。這對於儲存遊戲狀態、儲存使用者首選項或在不同系統之間交換資料等任務非常重要。
Python中的序列化庫
Python 提供了多個序列化庫,每個庫都有其自身的優勢。以下是Python中一些常用序列化庫的詳細概述:
Pickle − 這是Python內建的用於序列化和反序列化Python物件的模組。它易於使用,但特定於Python,如果用於不受信任的資料,則可能存在安全隱患。
JSON − JSON(JavaScript 物件表示法)是一種輕量級的資料交換格式,它是人類可讀的並且易於解析。它非常適合 Web API 和跨平臺通訊。
YAML − YAML:(YAML Ain't Markup Language)是一種人類可讀的資料序列化標準,易於人類和機器讀取和寫入。它支援複雜的資料結構,通常用於配置檔案。
使用Pickle模組進行序列化
Python 中的 pickle 模組用於序列化和反序列化物件。序列化,也稱為 **pickling**,涉及將 Python 物件轉換為位元組流,然後可以將其儲存在檔案中或透過網路傳輸。
反序列化或 **unpickling** 是相反的過程,即將位元組流轉換回 Python 物件。
序列化物件
我們可以使用 `dump()` 函式序列化物件並將其寫入檔案。檔案必須以二進位制寫入模式 ('wb') 開啟。
示例
在下面的示例中,一個字典被序列化並寫入名為“data.pkl”的檔案:
import pickle data = {'name': 'Alice', 'age': 30, 'city': 'New York'} # Open a file in binary write mode with open('data.pkl', 'wb') as file: # Serialize the data and write it to the file pickle.dump(data, file) print ("File created!!")
執行上述程式碼時,字典物件的位元組表示將儲存在 data.pkl 檔案中。
反序列化物件
要反序列化或解包物件,可以使用 `load()` 函式。檔案必須以二進位制讀取模式 ('rb') 開啟,如下所示:
import pickle # Open the file in binary read mode with open('data.pkl', 'rb') as file: # Deserialize the data data = pickle.load(file) print(data)
這將從“data.pkl”讀取位元組流並將其轉換回原始字典,如下所示:
{'name': 'Alice', 'age': 30, 'city': 'New York'}
Pickle 協議
協議是用於在將 Python 物件構造/解構為/從二進位制資料中使用的約定。
該 `pickle` 模組支援不同的序列化協議,更高的協議通常提供更多功能和更好的效能。目前,`pickle` 模組定義了 6 種不同的協議,如下所示:
序號 | 協議和描述 |
---|---|
1 | 協議版本 0 原始“人類可讀”協議,與早期版本向後相容。 |
2 | 協議版本 1 舊的二進位制格式也與早期版本的 Python 相容。 |
3 | 協議版本 2 在 Python 2.3 中引入,提供對新式類的有效序列化。 |
4 | 協議版本 3 在 Python 3.0 中新增。當需要與其他 Python 3 版本相容時推薦使用。 |
5 | 協議版本 4 在 Python 3.4 版本中引入。它增加了對超大物件的支援。 |
6 | 協議版本 5 在 Python 3.8 版本中引入。它增加了對帶外資料(out-of-band data)的支援。 |
可以透過將其作為引數傳遞給 `pickle.dump()` 函式來指定協議。
要了解 Python 安裝的最高和預設協議版本,可以使用 `pickle` 模組中定義的以下常量:
>>> import pickle >>> pickle.HIGHEST_PROTOCOL 5 >>> pickle.DEFAULT_PROTOCOL 4
Pickler 和 Unpickler 類
Python 中的 `pickle` 模組還定義了 `Pickler` 和 `Unpickler` 類,以便更詳細地控制序列化和反序列化過程。“Pickler”類將 pickle 資料寫入檔案,而“Unpickler”類從檔案讀取二進位制資料並重建原始 Python 物件。
使用 Pickler 類
要使用 Pickler 類序列化 Python 物件,可以按照以下步驟操作:
from pickle import Pickler # Open a file in binary write mode with open("data.txt", "wb") as f: # Create a dictionary dct = {'name': 'Ravi', 'age': 23, 'Gender': 'M', 'marks': 75} # Create a Pickler object and write the dictionary to the file Pickler(f).dump(dct) print ("Success!!")
執行上述程式碼後,字典物件的位元組表示將儲存在 "data.txt" 檔案中。
使用 Unpickler 類
要使用 Unpickler 類從二進位制檔案反序列化資料,可以執行以下操作:
from pickle import Unpickler # Open the file in binary read mode with open("data.txt", "rb") as f: # Create an Unpickler object and load the dictionary from the file dct = Unpickler(f).load() # Print the dictionary print(dct)
輸出結果如下:
{'name': 'Ravi', 'age': 23, 'Gender': 'M', 'marks': 75}
序列化自定義類物件
pickle 模組還可以序列化和反序列化自定義類。在序列化和反序列化時,必須提供類的定義。
示例
在此示例中,"Person" 類的例項被序列化然後反序列化,從而保持物件的狀態:
import pickle class Person: def __init__(self, name, age, city): self.name = name self.age = age self.city = city # Create an instance of the Person class person = Person('Alice', 30, 'New York') # Serialize the person object with open('person.pkl', 'wb') as file: pickle.dump(person, file) # Deserialize the person object with open('person.pkl', 'rb') as file: person = pickle.load(file) print(person.name, person.age, person.city)
執行上述程式碼後,輸出結果如下:
Alice 30 New York
Python 標準庫還包括 `marshal` 模組,該模組用於 Python 物件的內部序列化。與旨在用於通用用途的 pickle 不同,marshal 主要用於 Python 本身(例如,用於編寫 .pyc 檔案)。
由於 Python 版本之間可能存在相容性問題,因此通常不建議將其用於通用序列化。
使用 JSON 進行序列化
JSON(JavaScript 物件表示法)是一種流行的資料交換格式。它易於閱讀、易於編寫且與語言無關,非常適合序列化。
Python 透過 `json` 模組提供對 JSON 的內建支援,該模組允許您將資料序列化和反序列化為 JSON 格式。
序列化
序列化是將 Python 物件轉換為 JSON 字串或將其寫入檔案的過程。
示例:將資料序列化為 JSON 字串
在下面的示例中,我們使用 `json.dumps()` 函式將 Python 字典轉換為 JSON 字串:
import json # Create a dictionary data = {"name": "Alice", "age": 25, "city": "San Francisco"} # Serialize the dictionary to a JSON string json_string = json.dumps(data) print(json_string)
上述程式碼的輸出如下:
{"name": "Alice", "age": 25, "city": "San Francisco"}
示例:序列化資料並寫入檔案
在這裡,我們使用 `json.dump()` 函式將序列化的 JSON 資料直接寫入檔案:
import json # Create a dictionary data = {"name": "Alice", "age": 25, "city": "San Francisco"} # Serialize the dictionary and write it to a file with open("data.json", "w") as f: json.dump(data, f) print ("Success!!")
反序列化
反序列化是將 JSON 字串轉換回 Python 物件或從檔案讀取它的過程。
示例:反序列化 JSON 字串
在以下示例中,我們使用 `json.loads()` 函式將 JSON 字串轉換回 Python 字典:
import json # JSON string json_string = '{"name": "Alice", "age": 25, "city": "San Francisco"}' # Deserialize the JSON string into a Python dictionary loaded_data = json.loads(json_string) print(loaded_data)
輸出結果如下:
{'name': 'Alice', 'age': 25, 'city': 'San Francisco'}
示例:從檔案反序列化資料
在這裡,我們使用 `json.load()` 函式從檔案讀取 JSON 資料並將其轉換為 Python 字典:
import json # Open the file and load the JSON data into a Python dictionary with open("data.json", "r") as f: loaded_data = json.load(f) print(loaded_data)
獲得的輸出如下:
{'name': 'Alice', 'age': 25, 'city': 'San Francisco'}
使用 YAML 進行序列化
YAML(YAML Ain't Markup Language)是一種易於閱讀的資料序列化標準,通常用於配置檔案和資料交換。
Python 透過 `pyyaml` 包支援 YAML 序列化和反序列化,需要先安裝如下所示:
pip install pyyaml
示例:序列化資料並寫入 YAML 檔案
在下面的示例中,`yaml.dump()` 函式將 Python 字典資料轉換為 YAML 字串並將其寫入 "data.yaml" 檔案。
`default_flow_style` 引數確保 YAML 輸出更易於閱讀,並具有擴充套件格式:
import yaml # Create a Python dictionary data = {"name": "Emily", "age": 35, "city": "Seattle"} # Serialize the dictionary and write it to a YAML file with open("data.yaml", "w") as f: yaml.dump(data, f, default_flow_style=False) print("Success!!")
示例:從 YAML 檔案反序列化資料
在這裡,`yaml.safe_load()` 函式用於安全地從 "data.yaml" 載入 YAML 資料並將其轉換為 Python 字典 (loaded_data):
出於安全原因,建議使用 `safe_load()`,因為它只允許基本 Python 資料型別,並避免從 YAML 檔案執行任意程式碼。
import yaml # Deserialize data from a YAML file with open("data.yaml", "r") as f: loaded_data = yaml.safe_load(f) print(loaded_data)
產生的輸出如下所示:
{'age': 35, 'city': 'Seattle', 'name': 'Emily'}