{"id":1145149,"date":"2025-01-08T23:05:47","date_gmt":"2025-01-08T15:05:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1145149.html"},"modified":"2025-01-08T23:05:50","modified_gmt":"2025-01-08T15:05:50","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%af%bc%e5%87%ba%e6%95%b0%e6%8d%ae%e5%ba%93%e6%95%b0%e6%8d%ae%e5%ba%93%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1145149.html","title":{"rendered":"\u5982\u4f55\u7528python\u5bfc\u51fa\u6570\u636e\u5e93\u6570\u636e\u5e93\u6570\u636e\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181858\/f2edb1a4-87e4-4985-9d18-9b180c17e1ab.webp\" alt=\"\u5982\u4f55\u7528python\u5bfc\u51fa\u6570\u636e\u5e93\u6570\u636e\u5e93\u6570\u636e\u5e93\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u5bfc\u51fa\u6570\u636e\u5e93<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u5bfc\u51fa\u6570\u636e\u5e93\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u8fde\u63a5\u6570\u636e\u5e93\u3001\u9009\u62e9\u6570\u636e\u3001\u5bfc\u51fa\u6570\u636e\u3001\u5904\u7406\u5f02\u5e38\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong>\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u5982\u679c\u6570\u636e\u5e93\u8fde\u63a5\u4e0d\u6210\u529f\uff0c\u540e\u7eed\u7684\u64cd\u4f5c\u5c06\u65e0\u6cd5\u8fdb\u884c\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Python\u5bfc\u51fa\u6570\u636e\u5e93\u7684\u5177\u4f53\u6b65\u9aa4\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8fde\u63a5\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u8fde\u63a5\u6570\u636e\u5e93\u7684\u65b9\u6cd5\uff0c\u5e38\u89c1\u7684\u6570\u636e\u5e93\u7c7b\u578b\u5305\u62ecMySQL\u3001PostgreSQL\u3001SQLite\u7b49\u3002\u4e0d\u540c\u7684\u6570\u636e\u5e93\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u8fdb\u884c\u8fde\u63a5\u3002\u4f8b\u5982\uff0cMySQL\u901a\u5e38\u4f7f\u7528<code>mysql-connector-python<\/code>\u6216<code>PyMySQL<\/code>\uff0cPostgreSQL\u901a\u5e38\u4f7f\u7528<code>psycopg2<\/code>\uff0cSQLite\u5219\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528Python\u81ea\u5e26\u7684<code>sqlite3<\/code>\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u5b89\u88c5\u6240\u9700\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8eMySQL\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5<code>mysql-connector-python<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install mysql-connector-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2\u3001\u8fde\u63a5MySQL\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u8fde\u63a5MySQL\u6570\u636e\u5e93\u7684\u57fa\u672c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>def connect_to_db():<\/p>\n<p>    try:<\/p>\n<p>        connection = mysql.connector.connect(<\/p>\n<p>            host=&#39;your_host&#39;,<\/p>\n<p>            user=&#39;your_user&#39;,<\/p>\n<p>            password=&#39;your_password&#39;,<\/p>\n<p>            database=&#39;your_database&#39;<\/p>\n<p>        )<\/p>\n<p>        if connection.is_connected():<\/p>\n<p>            print(&quot;Connected to MySQL database&quot;)<\/p>\n<p>            return connection<\/p>\n<p>    except mysql.connector.Error as err:<\/p>\n<p>        print(f&quot;Error: {err}&quot;)<\/p>\n<p>        return None<\/p>\n<p>connection = connect_to_db()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u9009\u62e9\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u8fde\u63a5\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528SQL\u67e5\u8be2\u8bed\u53e5\u4ece\u6570\u636e\u5e93\u4e2d\u9009\u62e9\u6570\u636e\u3002\u901a\u5e38\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528<code>SELECT<\/code>\u8bed\u53e5\u6765\u9009\u62e9\u9700\u8981\u5bfc\u51fa\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u6267\u884cSQL\u67e5\u8be2<\/h4>\n<\/p>\n<p><p>\u6267\u884cSQL\u67e5\u8be2\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def fetch_data(connection):<\/p>\n<p>    cursor = connection.cursor()<\/p>\n<p>    query = &quot;SELECT * FROM your_table&quot;<\/p>\n<p>    cursor.execute(query)<\/p>\n<p>    result = cursor.fetchall()<\/p>\n<p>    return result<\/p>\n<p>data = fetch_data(connection)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5bfc\u51fa\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5bfc\u51fa\u6570\u636e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\u3001Excel\u6587\u4ef6\u6216JSON\u6587\u4ef6\u7b49\u3002\u8fd9\u91cc\u6211\u4eec\u4ee5\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\u4e3a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684<code>csv<\/code>\u6a21\u5757\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>def export_to_csv(data, filename=&#39;output.csv&#39;):<\/p>\n<p>    with open(filename, mode=&#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>        writer = csv.writer(file)<\/p>\n<p>        writer.writerow([&#39;Column1&#39;, &#39;Column2&#39;, &#39;Column3&#39;])  # \u6dfb\u52a0\u8868\u5934<\/p>\n<p>        writer.writerows(data)<\/p>\n<p>    print(f&quot;Data exported to {filename}&quot;)<\/p>\n<p>export_to_csv(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2\u3001\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pandas<\/code>\u5e93\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def export_to_excel(data, filename=&#39;output.xlsx&#39;):<\/p>\n<p>    df = pd.DataFrame(data, columns=[&#39;Column1&#39;, &#39;Column2&#39;, &#39;Column3&#39;])<\/p>\n<p>    df.to_excel(filename, index=False)<\/p>\n<p>    print(f&quot;Data exported to {filename}&quot;)<\/p>\n<p>export_to_excel(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\u3001\u67e5\u8be2\u8bed\u53e5\u9519\u8bef\u3001\u6587\u4ef6\u5199\u5165\u5931\u8d25\u7b49\u3002\u56e0\u6b64\uff0c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u6570\u636e\u5e93\u8fde\u63a5\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5728\u8fde\u63a5\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6355\u83b7\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def connect_to_db():<\/p>\n<p>    try:<\/p>\n<p>        connection = mysql.connector.connect(<\/p>\n<p>            host=&#39;your_host&#39;,<\/p>\n<p>            user=&#39;your_user&#39;,<\/p>\n<p>            password=&#39;your_password&#39;,<\/p>\n<p>            database=&#39;your_database&#39;<\/p>\n<p>        )<\/p>\n<p>        if connection.is_connected():<\/p>\n<p>            print(&quot;Connected to MySQL database&quot;)<\/p>\n<p>            return connection<\/p>\n<p>    except mysql.connector.Error as err:<\/p>\n<p>        print(f&quot;Error: {err}&quot;)<\/p>\n<p>        return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2\u3001\u67e5\u8be2\u548c\u5bfc\u51fa\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5728\u6267\u884c\u67e5\u8be2\u548c\u5bfc\u51fa\u64cd\u4f5c\u65f6\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6355\u83b7\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def fetch_data(connection):<\/p>\n<p>    try:<\/p>\n<p>        cursor = connection.cursor()<\/p>\n<p>        query = &quot;SELECT * FROM your_table&quot;<\/p>\n<p>        cursor.execute(query)<\/p>\n<p>        result = cursor.fetchall()<\/p>\n<p>        return result<\/p>\n<p>    except mysql.connector.Error as err:<\/p>\n<p>        print(f&quot;Error: {err}&quot;)<\/p>\n<p>        return []<\/p>\n<p>def export_to_csv(data, filename=&#39;output.csv&#39;):<\/p>\n<p>    try:<\/p>\n<p>        with open(filename, mode=&#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>            writer = csv.writer(file)<\/p>\n<p>            writer.writerow([&#39;Column1&#39;, &#39;Column2&#39;, &#39;Column3&#39;])  # \u6dfb\u52a0\u8868\u5934<\/p>\n<p>            writer.writerows(data)<\/p>\n<p>        print(f&quot;Data exported to {filename}&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5bfc\u51fa\u6570\u636e\u5e93\u6570\u636e\u7684\u8fc7\u7a0b\u53ef\u4ee5\u5206\u4e3a\u8fde\u63a5\u6570\u636e\u5e93\u3001\u9009\u62e9\u6570\u636e\u3001\u5bfc\u51fa\u6570\u636e\u548c\u5904\u7406\u5f02\u5e38\u56db\u4e2a\u4e3b\u8981\u6b65\u9aa4\u3002\u6bcf\u4e2a\u6b65\u9aa4\u90fd\u81f3\u5173\u91cd\u8981\uff0c\u786e\u4fdd\u6bcf\u4e00\u6b65\u90fd\u987a\u5229\u8fdb\u884c\u53ef\u4ee5\u4fdd\u8bc1\u6570\u636e\u5bfc\u51fa\u7684\u6210\u529f\u3002\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u95ee\u9898\u548c\u5f02\u5e38\uff0c\u63d0\u524d\u5904\u7406\u597d\u8fd9\u4e9b\u5f02\u5e38\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u4f7f\u7528Python\u5bfc\u51fa\u6570\u636e\u5e93\u6570\u636e\uff0c\u4e3a\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u63d0\u4f9b\u4fbf\u5229\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fde\u63a5\u5230\u6570\u636e\u5e93\u4ee5\u5bfc\u51fa\u6570\u636e\uff1f<\/strong><br \/>\u8981\u7528Python\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\uff0c\u5982<code>sqlite3<\/code>\uff08\u7528\u4e8eSQLite\u6570\u636e\u5e93\uff09\u3001<code>pymysql<\/code>\uff08\u7528\u4e8eMySQL\u6570\u636e\u5e93\uff09\u6216<code>psycopg2<\/code>\uff08\u7528\u4e8ePostgreSQL\u6570\u636e\u5e93\uff09\u3002\u8fde\u63a5\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7SQL\u67e5\u8be2\u63d0\u53d6\u6570\u636e\u5e76\u5c06\u5176\u5bfc\u51fa\u5230\u6587\u4ef6\u4e2d\uff0c\u5982CSV\u6216Excel\u683c\u5f0f\u3002\u786e\u4fdd\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\u5e76\u4f7f\u7528\u6b63\u786e\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5bfc\u51fa\u6570\u636e\u5e93\u6570\u636e\u65f6\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u6570\u636e\u683c\u5f0f\u9009\u62e9\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u5bfc\u51fa\u6570\u636e\u5e93\u6570\u636e\u65f6\uff0c\u5e38\u89c1\u7684\u6570\u636e\u683c\u5f0f\u5305\u62ecCSV\u3001JSON\u3001Excel\uff08XLSX\uff09\u3001\u548c\u6587\u672c\u6587\u4ef6\u7b49\u3002\u9009\u62e9\u6570\u636e\u683c\u5f0f\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0cCSV\u683c\u5f0f\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6570\u636e\u4ea4\u6362\uff0c\u800cJSON\u683c\u5f0f\u5219\u9002\u5408\u9700\u8981\u5d4c\u5957\u7ed3\u6784\u7684\u6570\u636e\u3002Excel\u683c\u5f0f\u5219\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u53ef\u8bfb\u6027\u548c\u683c\u5f0f\u5316\u9009\u9879\u3002<\/p>\n<p><strong>\u5728\u5bfc\u51fa\u6570\u636e\u65f6\u5982\u4f55\u5904\u7406\u5927\u578b\u6570\u636e\u5e93\u4ee5\u907f\u514d\u5185\u5b58\u95ee\u9898\uff1f<\/strong><br \/>\u5904\u7406\u5927\u578b\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u5206\u6279\u5bfc\u51fa\u6570\u636e\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u5230\u5185\u5b58\u4e2d\u3002\u4f7f\u7528\u6e38\u6807\uff08cursor\uff09\u8fdb\u884c\u9010\u884c\u8bfb\u53d6\u6216\u4f7f\u7528\u751f\u6210\u5668\u53ef\u4ee5\u6709\u6548\u964d\u4f4e\u5185\u5b58\u4f7f\u7528\u3002\u6b64\u5916\uff0c\u5229\u7528\u6570\u636e\u5e93\u7684\u5206\u9875\u529f\u80fd\uff0c\u5c06\u6570\u636e\u5206\u6210\u591a\u4e2a\u5c0f\u5757\u8fdb\u884c\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u5bfc\u51fa\u8fc7\u7a0b\u66f4\u52a0\u9ad8\u6548\u548c\u7a33\u5b9a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u5bfc\u51fa\u6570\u636e\u5e93 \u4f7f\u7528Python\u5bfc\u51fa\u6570\u636e\u5e93\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u8fde\u63a5\u6570\u636e\u5e93\u3001\u9009\u62e9\u6570\u636e\u3001\u5bfc\u51fa\u6570\u636e\u3001\u5904\u7406\u5f02\u5e38\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1145161,"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\/1145149"}],"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=1145149"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145149\/revisions"}],"predecessor-version":[{"id":1145163,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145149\/revisions\/1145163"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1145161"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1145149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1145149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1145149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}