PYTHON 幫助 PyYAML(開發人員工作原理) Curtis Chau 更新:6月 22, 2025 下載 IronPDF pip 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 PyYAML是一個 Python 函式庫,它既可以作為 YAML 解析器,也可以作為 YAML 產生器。 YAML(YAML Ain't Markup Language,YAML 不是標記語言)是一種人類可讀的資料序列化格式,它與 Python 應用程式整合良好,具有強大的錯誤支援、功能強大的擴充 API 等特性。 YAML 通常用於設定檔以及不同資料結構語言之間的資料交換,其設計理念是便於人類閱讀。 本文稍後將介紹IronPDF ,這是Iron Software出品的一款用於產生 PDF 的 Python 軟體包。 PyYAML 的主要特性 1.人類可讀格式:YAML 的設計宗旨是易於閱讀和編寫,使其成為複雜設定檔和資料序列化的理想選擇。 2.完全支援 YAML 1.1 :PyYAML 支援完整的 YAML 1.1 規範,包括 Unicode 支援和自訂資料類型。 3.與 Python 整合:PyYAML 提供 Python 特有的標籤,允許表示任意 Python 對象,使其適用於各種應用程式。 4.錯誤處理:PyYAML 提供合理的錯誤訊息,這在偵錯過程中非常有用。 安裝 要安裝 YAML 包,可以使用 pip: pip install pyyaml pip install pyyaml SHELL 基本用法 以下是一個簡單的範例,說明如何使用 PyYAML 將 YAML 文件載入到任意 Python 物件以及從任意 Python 物件載入和轉儲 YAML 文件。 import yaml # Sample YAML data yaml_data = """ name: John Doe age: 30 children: - name: Jane Doe age: 10 - name: Jim Doe age: 8 """ # Load YAML data into a Python dictionary data = yaml.safe_load(yaml_data) print(data) # Dump Python data back to formatted YAML yaml_output = yaml.dump(data, default_flow_style=False) print(yaml_output) import yaml # Sample YAML data yaml_data = """ name: John Doe age: 30 children: - name: Jane Doe age: 10 - name: Jim Doe age: 8 """ # Load YAML data into a Python dictionary data = yaml.safe_load(yaml_data) print(data) # Dump Python data back to formatted YAML yaml_output = yaml.dump(data, default_flow_style=False) print(yaml_output) PYTHON 輸出 PyYAML(開發者使用指南):圖 1 進階功能 1.自訂資料類型:PyYAML 可讓您定義自訂建構函式和表示器,以處理規範 YAML 格式的複雜資料類型。 import yaml # Define a custom Python object class Person: def __init__(self, name, age): self.name = name self.age = age # Function to convert a Person object to a YAML representation def person_representer(dumper, data): return dumper.represent_mapping('!Person', {'name': data.name, 'age': data.age}) # Function to create a Person object from YAML representation def person_constructor(loader, node): values = loader.construct_mapping(node) return Person(**values) # Register custom representer and constructor for Person yaml.add_representer(Person, person_representer) yaml.add_constructor('!Person', person_constructor) # Object Serialization person = Person(name='John Doe', age=30) yaml_data = yaml.dump(person) print(yaml_data) # Deserialize YAML to a Person object loaded_person = yaml.load(yaml_data, Loader=yaml.FullLoader) print(loaded_person.name, loaded_person.age) import yaml # Define a custom Python object class Person: def __init__(self, name, age): self.name = name self.age = age # Function to convert a Person object to a YAML representation def person_representer(dumper, data): return dumper.represent_mapping('!Person', {'name': data.name, 'age': data.age}) # Function to create a Person object from YAML representation def person_constructor(loader, node): values = loader.construct_mapping(node) return Person(**values) # Register custom representer and constructor for Person yaml.add_representer(Person, person_representer) yaml.add_constructor('!Person', person_constructor) # Object Serialization person = Person(name='John Doe', age=30) yaml_data = yaml.dump(person) print(yaml_data) # Deserialize YAML to a Person object loaded_person = yaml.load(yaml_data, Loader=yaml.FullLoader) print(loaded_person.name, loaded_person.age) PYTHON 輸出 PyYAML(開發者使用指南):圖 2 2.處理大型文件:PyYAML 可以透過基於流的載入和轉儲有效地處理多個 YAML 文件或大型 YAML 文件。 import yaml # Load a large YAML file with open('large_file.yaml', 'r') as file: data = yaml.safe_load(file) # Dump data to a large YAML file with open('output_file.yaml', 'w') as file: yaml.dump(data, file) import yaml # Load a large YAML file with open('large_file.yaml', 'r') as file: data = yaml.safe_load(file) # Dump data to a large YAML file with open('output_file.yaml', 'w') as file: yaml.dump(data, file) PYTHON 輸出 PyYAML(開發者使用指南):圖 3 介紹 IronPDF。 PyYAML(開發者使用指南):圖 4 IronPDF是一個功能強大的 Python 庫,旨在利用 HTML、CSS、圖像和 JavaScript 創建、編輯和簽署 PDF 文件。 它提供商業級的效能與低記憶體佔用量。 主要功能包括 HTML 轉 PDF 轉換:將 HTML 檔案、HTML 字串和 URL 轉換為 PDF。 例如,使用 Chrome PDF 渲染器將網頁渲染為 PDF。 -跨平台支援:相容於各種 .NET 平台,包括 .NET Core、.NET Standard 和 .NET Framework。 它支援 Windows、Linux 和 macOS。 -編輯和簽名:設定屬性,使用密碼和權限添加安全性,並將數位簽名套用到您的 PDF 檔案。 -頁面範本和設定:自訂 PDF,包括頁首、頁尾、頁碼和可調整的邊距。 IronPDF 支援響應式佈局和自訂紙張尺寸。 -標準合規性:IronPDF 遵循 PDF/A 和 PDF/UA 等 PDF 標準。 它支援 UTF-8 字元編碼,並能處理圖片、CSS 和字型等資產。 使用 IronPDF 和 PyYAML 產生 PDF 文檔 import yaml import json from ironpdf import * # Apply your license key License.LicenseKey = "your license" # Sample YAML data yaml_data = """ name: IronPDF User1 age: 25 children: - name: IronPDF User2 age: 23 - name: IronPDF User3 age: 24 """ # Load YAML data into Python structures data = yaml.safe_load(yaml_data) print(data) # Dump Python data back to YAML yaml_output = yaml.dump(data, default_flow_style=False) print(yaml_output) # Write YAML to File with open('output_file.yaml', 'w') as file: yaml.dump(yaml_output, file) # Write YAML data as JSON with open('output_file.json', 'w') as json_file: json.dump(data, json_file) # Read JSON and format with indentation for readability output = json.dumps(json.load(open('output_file.json')), indent=2) print(output) # Create PDF renderer renderer = ChromePdfRenderer() # Create a PDF from HTML containing YAML data content = "<h1>Awesome Iron PDF with PyYAML</h1>" content += "<p>YAML data: " + yaml_output + "</p>" pdf = renderer.RenderHtmlAsPdf(content) # Save the PDF document to a file pdf.SaveAs("awesome.pdf") import yaml import json from ironpdf import * # Apply your license key License.LicenseKey = "your license" # Sample YAML data yaml_data = """ name: IronPDF User1 age: 25 children: - name: IronPDF User2 age: 23 - name: IronPDF User3 age: 24 """ # Load YAML data into Python structures data = yaml.safe_load(yaml_data) print(data) # Dump Python data back to YAML yaml_output = yaml.dump(data, default_flow_style=False) print(yaml_output) # Write YAML to File with open('output_file.yaml', 'w') as file: yaml.dump(yaml_output, file) # Write YAML data as JSON with open('output_file.json', 'w') as json_file: json.dump(data, json_file) # Read JSON and format with indentation for readability output = json.dumps(json.load(open('output_file.json')), indent=2) print(output) # Create PDF renderer renderer = ChromePdfRenderer() # Create a PDF from HTML containing YAML data content = "<h1>Awesome Iron PDF with PyYAML</h1>" content += "<p>YAML data: " + yaml_output + "</p>" pdf = renderer.RenderHtmlAsPdf(content) # Save the PDF document to a file pdf.SaveAs("awesome.pdf") PYTHON 程式碼解釋 1.進口: 導入必要的函式庫: yaml用於 YAML 操作, json用於 JSON 操作, ironpdf用於 PDF 產生。 2.設定許可證密鑰: 設定 IronPDF 許可證金鑰,以便合法、有效地存取該庫。 YAML 資料範例: 定義範例 YAML 資料以示範 YAML 操作。 YAML 操作: 使用yaml.safe_load()將 YAML 資料轉換為 Python 物件進行操作。 5.匯出為 YAML 檔案: 使用yaml.dump()將 Python 物件轉換回 YAML 格式以進行輸出。 6.寫入文件: 將 YAML 資料匯出到 YAML 文件,將 JSON 資料匯出到 JSON 文件,以便儲存或傳輸。 7.讀取和格式化 JSON: 從檔案中讀取 JSON 數據,並使用json.dumps()將其格式化為可讀格式。 8.使用 IronPDF 產生 PDF: 使用 IronPDF 將 HTML 字串渲染成 PDF 文檔,包括嵌入的 YAML 資料。 9.保存 PDF 檔: 將產生的 PDF 檔案儲存到檔案系統中,示範如何透過程式建立 PDF 檔案。 輸出 PyYAML(開發者使用指南):圖 5 PDF。 PyYAML(開發者使用指南):圖 6 IronPDF 授權。 IronPDF需要 Python 許可證金鑰才能運作。 IronPDF for Python 提供免費試用許可證金鑰,使用戶能夠在購買前體驗其豐富的功能。 在使用 IronPDF 軟體包之前,請將許可證密鑰放在腳本的開頭: from ironpdf import * # Apply your license key License.LicenseKey = "key" from ironpdf import * # Apply your license key License.LicenseKey = "key" PYTHON 結論 PyYAML 是一個功能強大且靈活的 Python YAML 處理函式庫。 它具有人類可讀的格式、完全支援 YAML 1.1 以及與 Python 的集成,使其成為配置檔案、資料序列化等的絕佳選擇。 無論你處理的是簡單的設定還是複雜的資料結構,PyYAML 都能提供有效處理 YAML 資料所需的工具。 IronPDF 是一個 Python 包,可以輕鬆地將 HTML 內容轉換為 PDF 文件。 它為開發者提供了一個簡單的 API (ChromePdfRenderer),可以從 HTML 產生高品質的 PDF,包括支援 CSS 和 JavaScript 等現代 Web 標準。 這使其成為直接從 Python 應用程式動態建立和保存 PDF 文件的有效工具。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新6月 22, 2025 deepstream io(開發人員的工作原理) 在這篇文章中,我們將學習如何使用開放即時伺服器 deepstream 和 IronPDF 生成 PDF。 閱讀更多 更新6月 22, 2025 imageio python(開發人員如何工作) 我們將探討如何使用 Imageio 讀寫圖像,然後我們還將研究 IronPDF 從 Iron Software 生產的生成 PDF 文檔 閱讀更多 更新6月 22, 2025 igraph python(開發人員如何工作) 在這篇文章中,我們使用 igraph 展示了如何能夠生成網絡圖,並打印到 PDF 文件,使用靈活和可靠的 IronPDF 庫。 閱讀更多 msgpack python(開發人員工作原理)sqlite utils python(開發人員...
更新6月 22, 2025 imageio python(開發人員如何工作) 我們將探討如何使用 Imageio 讀寫圖像,然後我們還將研究 IronPDF 從 Iron Software 生產的生成 PDF 文檔 閱讀更多
更新6月 22, 2025 igraph python(開發人員如何工作) 在這篇文章中,我們使用 igraph 展示了如何能夠生成網絡圖,並打印到 PDF 文件,使用靈活和可靠的 IronPDF 庫。 閱讀更多