{"id":1074291,"date":"2025-01-08T11:33:16","date_gmt":"2025-01-08T03:33:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1074291.html"},"modified":"2025-01-08T11:33:19","modified_gmt":"2025-01-08T03:33:19","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python%e8%be%93%e5%87%ba%e7%9a%84%e7%bb%93%e6%9e%9c%e4%bf%9d%e5%ad%98-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1074291.html","title":{"rendered":"\u5982\u4f55\u5c06python\u8f93\u51fa\u7684\u7ed3\u679c\u4fdd\u5b58"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103937\/e111ba18-675f-4e4b-9f6b-b223c411ee46.webp\" alt=\"\u5982\u4f55\u5c06python\u8f93\u51fa\u7684\u7ed3\u679c\u4fdd\u5b58\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Python\u8f93\u51fa\u7684\u7ed3\u679c\u4fdd\u5b58\uff0c\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u3001\u6570\u636e\u5e93\u5b58\u50a8\u3001\u65e5\u5fd7\u8bb0\u5f55\u7b49\u65b9\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0c\u6587\u4ef6\u64cd\u4f5c\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u5305\u62ec\u5c06\u6570\u636e\u4fdd\u5b58\u5230\u6587\u672c\u6587\u4ef6\u3001CSV\u6587\u4ef6\u3001JSON\u6587\u4ef6\u7b49\u683c\u5f0f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5c06Python\u8f93\u51fa\u7684\u7ed3\u679c\u4fdd\u5b58\u5230\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u672c\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>1. \u4f7f\u7528 <code>open()<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684 <code>open()<\/code> \u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 <code>write()<\/code> \u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u6587\u4ef6<\/p>\n<p>file = open(&#39;output.txt&#39;, &#39;w&#39;)<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e<\/strong><\/h2>\n<p>file.write(&#39;Hello, World!\\n&#39;)<\/p>\n<p>file.write(&#39;This is a test.\\n&#39;)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u4ef6<\/strong><\/h2>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u4ee5\u5199\u5165\u6a21\u5f0f\uff08<code>&#39;w&#39;<\/code>\uff09\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a <code>output.txt<\/code> \u7684\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0cPython \u5c06\u81ea\u52a8\u521b\u5efa\u6587\u4ef6\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>write()<\/code> \u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\uff0c\u6700\u540e\u5173\u95ed\u6587\u4ef6\u4ee5\u786e\u4fdd\u6570\u636e\u88ab\u6b63\u786e\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528 <code>with<\/code> \u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\u53ef\u4ee5\u7b80\u5316\u6587\u4ef6\u64cd\u4f5c\uff0c\u5e76\u786e\u4fdd\u6587\u4ef6\u5728\u5b8c\u6210\u64cd\u4f5c\u540e\u81ea\u52a8\u5173\u95ed\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!\\n&#39;)<\/p>\n<p>    file.write(&#39;This is a test.\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\u7b80\u5316\u4e86\u4ee3\u7801\uff0c\u5e76\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230CSV\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>CSV\uff08Comma-Separated Values\uff09\u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\uff0c\u9002\u7528\u4e8e\u8868\u683c\u6570\u636e\u3002Python \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684 <code>csv<\/code> \u6a21\u5757\u6765\u5904\u7406CSV\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;],<\/p>\n<p>    [&#39;Alice&#39;, 30, &#39;New York&#39;],<\/p>\n<p>    [&#39;Bob&#39;, 25, &#39;Los Angeles&#39;],<\/p>\n<p>    [&#39;Charlie&#39;, 35, &#39;Chicago&#39;]<\/p>\n<p>]<\/p>\n<h2><strong>\u6253\u5f00CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    # \u5199\u5165\u6570\u636e<\/p>\n<p>    writer.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165 <code>csv<\/code> \u6a21\u5757\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u8868\u683c\u6570\u636e\u7684\u5217\u8868\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u4ee5\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a <code>output.csv<\/code> \u7684\u6587\u4ef6\u3002\u63a5\u7740\uff0c\u4f7f\u7528 <code>csv.writer()<\/code> \u521b\u5efa\u4e00\u4e2a CSV \u5199\u5165\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528 <code>writerows()<\/code> \u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230JSON\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u9002\u7528\u4e8e\u5b58\u50a8\u548c\u4f20\u8f93\u7ed3\u6784\u5316\u6570\u636e\u3002Python \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684 <code>json<\/code> \u6a21\u5757\u6765\u5904\u7406JSON\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: &#39;Alice&#39;,<\/p>\n<p>    &#39;Age&#39;: 30,<\/p>\n<p>    &#39;City&#39;: &#39;New York&#39;<\/p>\n<p>}<\/p>\n<h2><strong>\u6253\u5f00JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    # \u5199\u5165\u6570\u636e<\/p>\n<p>    json.dump(data, file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165 <code>json<\/code> \u6a21\u5757\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u7ed3\u6784\u5316\u6570\u636e\u7684\u5b57\u5178\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u4ee5\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a <code>output.json<\/code> \u7684\u6587\u4ef6\u3002\u63a5\u7740\uff0c\u4f7f\u7528 <code>json.dump()<\/code> \u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 <code>indent<\/code> \u53c2\u6570\u8fdb\u884c\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u6570\u636e\u5e93<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u6587\u4ef6\u64cd\u4f5c\uff0cPython \u8fd8\u53ef\u4ee5\u5c06\u6570\u636e\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 SQLite \u6570\u636e\u5e93\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93\uff08\u5982\u679c\u6570\u636e\u5e93\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u6570\u636e\u5e93\uff09<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;output.db&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;<\/p>\n<p>CREATE TABLE IF NOT EXISTS users (<\/p>\n<p>    id INTEGER PRIMARY KEY,<\/p>\n<p>    name TEXT,<\/p>\n<p>    age INTEGER,<\/p>\n<p>    city TEXT<\/p>\n<p>)<\/p>\n<p>&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;<\/p>\n<p>INSERT INTO users (name, age, city)<\/p>\n<p>VALUES (&#39;Alice&#39;, 30, &#39;New York&#39;)<\/p>\n<p>&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165 <code>sqlite3<\/code> \u6a21\u5757\uff0c\u5e76\u8fde\u63a5\u5230\u4e00\u4e2a\u540d\u4e3a <code>output.db<\/code> \u7684 SQLite \u6570\u636e\u5e93\uff08\u5982\u679c\u6570\u636e\u5e93\u4e0d\u5b58\u5728\uff0cPython \u5c06\u81ea\u52a8\u521b\u5efa\u6570\u636e\u5e93\uff09\u3002\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61\uff0c\u5e76\u6267\u884c SQL \u8bed\u53e5\u4ee5\u521b\u5efa\u8868\u548c\u63d2\u5165\u6570\u636e\u3002\u6700\u540e\uff0c\u63d0\u4ea4\u4e8b\u52a1\u5e76\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u65e5\u5fd7\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u9700\u8981\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u65e5\u5fd7\uff0c\u53ef\u4ee5\u4f7f\u7528 Python \u7684\u5185\u7f6e <code>logging<\/code> \u6a21\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/h2>\n<p>logging.basicConfig(filename=&#39;output.log&#39;, level=logging.INFO, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\n<p>logging.info(&#39;This is an info message.&#39;)<\/p>\n<p>logging.warning(&#39;This is a warning message.&#39;)<\/p>\n<p>logging.error(&#39;This is an error message.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165 <code>logging<\/code> \u6a21\u5757\uff0c\u5e76\u4f7f\u7528 <code>logging.basicConfig()<\/code> \u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>logging.info()<\/code>\u3001<code>logging.warning()<\/code> \u548c <code>logging.error()<\/code> \u65b9\u6cd5\u8bb0\u5f55\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230Excel\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>Excel \u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\uff0c\u9002\u7528\u4e8e\u8868\u683c\u6570\u636e\u3002Python \u63d0\u4f9b\u4e86\u7b2c\u4e09\u65b9\u5e93 <code>openpyxl<\/code> \u6765\u5904\u7406Excel\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import Workbook<\/p>\n<h2><strong>\u521b\u5efa\u5de5\u4f5c\u7c3f<\/strong><\/h2>\n<p>wb = Workbook()<\/p>\n<h2><strong>\u83b7\u53d6\u6d3b\u52a8\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws = wb.active<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;],<\/p>\n<p>    [&#39;Alice&#39;, 30, &#39;New York&#39;],<\/p>\n<p>    [&#39;Bob&#39;, 25, &#39;Los Angeles&#39;],<\/p>\n<p>    [&#39;Charlie&#39;, 35, &#39;Chicago&#39;]<\/p>\n<p>]<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e<\/strong><\/h2>\n<p>for row in data:<\/p>\n<p>    ws.append(row)<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>wb.save(&#39;output.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165 <code>openpyxl<\/code> \u5e93\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7c3f\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u83b7\u53d6\u6d3b\u52a8\u5de5\u4f5c\u8868\uff0c\u5e76\u4f7f\u7528 <code>append()<\/code> \u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u5de5\u4f5c\u8868\u3002\u6700\u540e\uff0c\u4f7f\u7528 <code>save()<\/code> \u65b9\u6cd5\u4fdd\u5b58\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230Pickle\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>Pickle \u662f Python \u7684\u5e8f\u5217\u5316\u6a21\u5757\uff0c\u9002\u7528\u4e8e\u4fdd\u5b58\u548c\u52a0\u8f7d Python \u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: &#39;Alice&#39;, &#39;Age&#39;: 30, &#39;City&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u4fdd\u5b58\u6570\u636e<\/strong><\/h2>\n<p>with open(&#39;output.pkl&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    pickle.dump(data, file)<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>with open(&#39;output.pkl&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    loaded_data = pickle.load(file)<\/p>\n<p>    print(loaded_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165 <code>pickle<\/code> \u6a21\u5757\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u7ed3\u6784\u5316\u6570\u636e\u7684\u5b57\u5178\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u4ee5\u4e8c\u8fdb\u5236\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a <code>output.pkl<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 <code>pickle.dump()<\/code> \u65b9\u6cd5\u5c06\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u3002\u63a5\u7740\uff0c\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u4ee5\u4e8c\u8fdb\u5236\u8bfb\u53d6\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 <code>pickle.load()<\/code> \u65b9\u6cd5\u52a0\u8f7d\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230XML\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>XML\uff08eXtensible Markup Language\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\uff0c\u9002\u7528\u4e8e\u5b58\u50a8\u548c\u4f20\u8f93\u7ed3\u6784\u5316\u6570\u636e\u3002Python \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684 <code>xml.etree.ElementTree<\/code> \u6a21\u5757\u6765\u5904\u7406XML\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<h2><strong>\u521b\u5efa\u6839\u5143\u7d20<\/strong><\/h2>\n<p>root = ET.Element(&#39;users&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7528\u6237\u5143\u7d20<\/strong><\/h2>\n<p>user = ET.SubElement(root, &#39;user&#39;)<\/p>\n<p>name = ET.SubElement(user, &#39;name&#39;)<\/p>\n<p>name.text = &#39;Alice&#39;<\/p>\n<p>age = ET.SubElement(user, &#39;age&#39;)<\/p>\n<p>age.text = &#39;30&#39;<\/p>\n<p>city = ET.SubElement(user, &#39;city&#39;)<\/p>\n<p>city.text = &#39;New York&#39;<\/p>\n<h2><strong>\u521b\u5efa\u6811\u5bf9\u8c61<\/strong><\/h2>\n<p>tree = ET.ElementTree(root)<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>tree.write(&#39;output.xml&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165 <code>xml.etree.ElementTree<\/code> \u6a21\u5757\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u6839\u5143\u7d20\u3002\u7136\u540e\uff0c\u521b\u5efa\u5b50\u5143\u7d20\u5e76\u8bbe\u7f6e\u6587\u672c\u5185\u5bb9\u3002\u63a5\u7740\uff0c\u521b\u5efa\u4e00\u4e2a\u6811\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528 <code>write()<\/code> \u65b9\u6cd5\u4fdd\u5b58\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230HTML\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>HTML\uff08HyperText Markup Language\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6807\u8bb0\u8bed\u8a00\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u7f51\u9875\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u6570\u636e\u4fdd\u5b58\u5230HTML\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = &#39;&#39;&#39;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Output&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Hello, World!&lt;\/h1&gt;<\/p>\n<p>    &lt;p&gt;This is a test.&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.html&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542bHTML\u5185\u5bb9\u7684\u5b57\u7b26\u4e32\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u4ee5\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a <code>output.html<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 <code>write()<\/code> \u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u3001\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230PDF\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>PDF\uff08Portable Document Format\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6587\u6863\u683c\u5f0f\u3002Python \u63d0\u4f9b\u4e86\u7b2c\u4e09\u65b9\u5e93 <code>reportlab<\/code> \u6765\u5904\u7406PDF\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from reportlab.lib.pagesizes import letter<\/p>\n<p>from reportlab.pdfgen import canvas<\/p>\n<h2><strong>\u521b\u5efaPDF\u6587\u4ef6<\/strong><\/h2>\n<p>c = canvas.Canvas(&#39;output.pdf&#39;, pagesize=letter)<\/p>\n<p>width, height = letter<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e<\/strong><\/h2>\n<p>c.drawString(100, height - 100, &#39;Hello, World!&#39;)<\/p>\n<p>c.drawString(100, height - 120, &#39;This is a test.&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>c.save()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165 <code>reportlab<\/code> \u5e93\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684PDF\u6587\u4ef6\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>drawString()<\/code> \u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165PDF\u6587\u4ef6\u3002\u6700\u540e\uff0c\u4f7f\u7528 <code>save()<\/code> \u65b9\u6cd5\u4fdd\u5b58\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06Python\u8f93\u51fa\u7684\u7ed3\u679c\u4fdd\u5b58\u5230\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u4e2d\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u4fdd\u5b58\u65b9\u5f0f\uff0c\u786e\u4fdd\u6570\u636e\u80fd\u591f\u6301\u4e45\u5316\u5b58\u50a8\u5e76\u65b9\u4fbf\u540e\u7eed\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u8f93\u51fa\u7684\u7ed3\u679c\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5c06Python\u8f93\u51fa\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u7ed3\u5408<code>write()<\/code>\u65b9\u6cd5\u3002\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u672c\u6587\u4ef6\u5e76\u6253\u5f00\u5b83\uff0c\u7136\u540e\u5c06\u9700\u8981\u4fdd\u5b58\u7684\u7ed3\u679c\u5199\u5165\u6587\u4ef6\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">result = &quot;\u8fd9\u662f\u8981\u4fdd\u5b58\u7684\u7ed3\u679c&quot;\nwith open(&quot;output.txt&quot;, &quot;w&quot;) as file:\n    file.write(result)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>output.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u5185\u5bb9\u5199\u5165\u5176\u4e2d\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5c06Python\u8f93\u51fa\u4fdd\u5b58\u4e3a\u5176\u4ed6\u683c\u5f0f\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u8f93\u51fa\u4fdd\u5b58\u3002\u9664\u4e86\u6587\u672c\u6587\u4ef6\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5c06\u7ed3\u679c\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u3001JSON\u6587\u4ef6\u6216\u8005Excel\u6587\u4ef6\u3002\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5730\u4fdd\u5b58\u4e3aCSV\u683c\u5f0f\uff0c\u800c\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u5219\u53ef\u4ee5\u5c06\u6570\u636e\u4fdd\u5b58\u4e3aJSON\u683c\u5f0f\u3002\u5bf9\u4e8eExcel\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5982\u4f55\u4fdd\u5b58Python\u8f93\u51fa\u7684\u7ed3\u679c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\uff1f<\/strong><br \/>\u4e3a\u4e86\u4fbf\u4e8e\u540e\u7eed\u5206\u6790\uff0c\u53ef\u4ee5\u5c06Python\u8f93\u51fa\u7ed3\u679c\u4fdd\u5b58\u4e3a\u7ed3\u6784\u5316\u7684\u6570\u636e\u683c\u5f0f\uff0c\u5982CSV\u3001JSON\u6216\u6570\u636e\u5e93\u3002\u7ed3\u6784\u5316\u683c\u5f0f\u4f7f\u5f97\u6570\u636e\u66f4\u5bb9\u6613\u88ab\u89e3\u6790\u548c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u5c06DataFrame\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;\u52171&#39;: [1, 2, 3], &#39;\u52172&#39;: [4, 5, 6]}\ndf = pd.DataFrame(data)\ndf.to_csv(&quot;output.csv&quot;, index=False)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u8f93\u51fa\u7684\u7ed3\u679c\u4fdd\u5b58\uff0c\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u3001\u6570\u636e\u5e93\u5b58\u50a8\u3001\u65e5\u5fd7\u8bb0\u5f55\u7b49\u65b9\u5f0f\u3002\u5176\u4e2d\uff0c\u6587\u4ef6\u64cd\u4f5c\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83 [&hellip;]","protected":false},"author":3,"featured_media":1074298,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074291"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1074291"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074291\/revisions"}],"predecessor-version":[{"id":1074303,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074291\/revisions\/1074303"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1074298"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1074291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1074291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1074291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}