{"id":924364,"date":"2024-12-26T15:10:43","date_gmt":"2024-12-26T07:10:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/924364.html"},"modified":"2024-12-26T15:10:45","modified_gmt":"2024-12-26T07:10:45","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%87%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/924364.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212036\/8f7245eb-d588-4eb4-abaa-18183f70c85b.webp\" alt=\"python\u5982\u4f55\u5bfc\u51fa\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5bfc\u51fa\u6570\u636e\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u5e93\u6216\u7b2c\u4e09\u65b9\u5e93\u6765\u5b9e\u73b0\uff0c\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528CSV\u6a21\u5757\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\u3001\u4f7f\u7528Pandas\u5e93\u5bfc\u51fa\u4e3a\u591a\u79cd\u683c\u5f0f\u3001\u4f7f\u7528JSON\u6a21\u5757\u5bfc\u51fa\u4e3aJSON\u6587\u4ef6\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u70b9\uff0c\u5176\u4e2dPandas\u5e93\u56e0\u4e3a\u5176\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u800c\u5e7f\u53d7\u6b22\u8fce\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u5bfc\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528CSV\u6a21\u5757\u5bfc\u51fa\u6570\u636e<\/p>\n<\/p>\n<p><p>CSV\uff08Comma Separated Values\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u683c\u5f0f\uff0c\u53ef\u4ee5\u88ab\u5927\u591a\u6570\u6570\u636e\u5206\u6790\u8f6f\u4ef6\u548c\u7535\u5b50\u8868\u683c\u7a0b\u5e8f\u8bc6\u522b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>CSV\u6a21\u5757\u7b80\u4ecb<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684\u6807\u51c6\u5e93\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a\u540d\u4e3acsv\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884cCSV\u683c\u5f0f\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u3002\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u4e24\u4e2a\u4e3b\u8981\u7684\u7c7b\uff1a<code>csv.reader<\/code>\u7528\u4e8e\u8bfb\u53d6CSV\u6587\u4ef6\uff0c<code>csv.writer<\/code>\u7528\u4e8e\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528CSV\u6a21\u5757\u5bfc\u51fa\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528csv\u6a21\u5757\u5bfc\u51fa\u6570\u636e\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u6570\u636e\u793a\u4f8b<\/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\u5165CSV\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>    writer.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>writerows()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u591a\u4e2a\u884c\u5199\u5165CSV\u6587\u4ef6\u3002<code>newline=&#39;&#39;<\/code>\u53c2\u6570\u7528\u4e8e\u9632\u6b62\u5728Windows\u5e73\u53f0\u4e0a\u51fa\u73b0\u591a\u4f59\u7684\u7a7a\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528Pandas\u5e93\u5bfc\u51fa\u6570\u636e<\/p>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u5e93\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u7684\u5bfc\u5165\u548c\u5bfc\u51fa\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Pandas\u5e93\u7b80\u4ecb<\/strong><\/p>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86DataFrame\u548cSeries\u4e24\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u524d\u8005\u7528\u4e8e\u5b58\u50a8\u4e8c\u7ef4\u8868\u683c\u6570\u636e\uff0c\u540e\u8005\u7528\u4e8e\u5b58\u50a8\u4e00\u7ef4\u6570\u7ec4\u3002Pandas\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Pandas\u5bfc\u51fa\u6570\u636e\u4e3aCSV\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>Pandas\u5e93\u7684<code>to_csv()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5c06DataFrame\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [30, 25, 35],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]<\/p>\n<p>})<\/p>\n<h2><strong>\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;output.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>index=False<\/code>\u53c2\u6570\u7528\u4e8e\u907f\u514d\u5c06DataFrame\u7684\u7d22\u5f15\u5bfc\u51fa\u5230CSV\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Pandas\u5bfc\u51fa\u6570\u636e\u4e3aExcel\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>Pandas\u7684<code>to_excel()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06DataFrame\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bfc\u51fa\u4e3aExcel\u6587\u4ef6<\/p>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\u9700\u8981\u5b89\u88c5<code>openpyxl<\/code>\u6216<code>xlsxwriter<\/code>\u5e93\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Pandas\u5bfc\u51fa\u6570\u636e\u4e3aJSON\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>Pandas\u7684<code>to_json()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06DataFrame\u5bfc\u51fa\u4e3aJSON\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bfc\u51fa\u4e3aJSON\u6587\u4ef6<\/p>\n<p>df.to_json(&#39;output.json&#39;, orient=&#39;records&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>orient=&#39;records&#39;<\/code>\u53c2\u6570\u6307\u5b9a\u4e86JSON\u7684\u683c\u5f0f\uff0c\u8fd9\u91cc\u8868\u793a\u6bcf\u4e00\u884c\u6570\u636e\u4f5c\u4e3a\u4e00\u4e2aJSON\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528JSON\u6a21\u5757\u5bfc\u51fa\u6570\u636e<\/p>\n<\/p>\n<p><p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u6613\u4e8e\u4eba\u548c\u673a\u5668\u9605\u8bfb\u548c\u7f16\u5199\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>JSON\u6a21\u5757\u7b80\u4ecb<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684\u6807\u51c6\u5e93\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a\u540d\u4e3ajson\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884cJSON\u683c\u5f0f\u6570\u636e\u7684\u7f16\u7801\u548c\u89e3\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528JSON\u6a21\u5757\u5bfc\u51fa\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528json\u6a21\u5757\u5bfc\u51fa\u6570\u636e\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u6570\u636e\u793a\u4f8b<\/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>\u5199\u5165JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(data, file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>json.dump()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06Python\u5bf9\u8c61\u7f16\u7801\u4e3aJSON\u683c\u5f0f\u5e76\u5199\u5165\u6587\u4ef6\u3002<code>indent=4<\/code>\u53c2\u6570\u7528\u4e8e\u8bbe\u7f6eJSON\u5b57\u7b26\u4e32\u7684\u7f29\u8fdb\u7ea7\u522b\uff0c\u4f7f\u5176\u66f4\u6613\u4e8e\u9605\u8bfb\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528SQLAlchemy\u5bfc\u51fa\u6570\u636e<\/p>\n<\/p>\n<p><p>SQLAlchemy\u662f\u4e00\u4e2aPython\u7684SQL\u5de5\u5177\u5305\u548c\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5668\uff08ORM\uff09\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>SQLAlchemy\u7b80\u4ecb<\/strong><\/p>\n<\/p>\n<p><p>SQLAlchemy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5168\u529f\u80fd\u7684SQL\u5de5\u5177\u5305\u548c\u4e00\u4e2a\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5668\u3002\u5b83\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u4f7f\u7528Python\u7c7b\u6765\u8868\u793a\u6570\u636e\u5e93\u8868\uff0c\u5e76\u901a\u8fc7Python\u5bf9\u8c61\u6765\u64cd\u4f5c\u6570\u636e\u5e93\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528SQLAlchemy\u5bfc\u51fa\u6570\u636e\u5230\u6570\u636e\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5c06DataFrame\u5bfc\u51fa\u5230SQLite\u6570\u636e\u5e93\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [30, 25, 35],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]<\/p>\n<p>})<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>engine = create_engine(&#39;sqlite:\/\/\/example.db&#39;)<\/p>\n<h2><strong>\u5bfc\u51faDataFrame\u5230SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>df.to_sql(&#39;people&#39;, con=engine, if_exists=&#39;replace&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>create_engine()<\/code>\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\uff0c<code>to_sql()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06DataFrame\u5bfc\u51fa\u5230\u6570\u636e\u5e93\u8868\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u4f7f\u7528Matplotlib\u5bfc\u51fa\u56fe\u8868<\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2aPython\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u751f\u6210\u9ad8\u8d28\u91cf\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Matplotlib\u7b80\u4ecb<\/strong><\/p>\n<\/p>\n<p><p>Matplotlib\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9762\u5411\u5bf9\u8c61\u7684\u7ed8\u56feAPI\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5982\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Matplotlib\u5bfc\u51fa\u56fe\u8868\u4e3a\u56fe\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5c06\u56fe\u8868\u5bfc\u51fa\u4e3aPNG\u56fe\u7247\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6570\u636e\u793a\u4f8b<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [10, 15, 13, 20, 18]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u5bfc\u51fa\u56fe\u8868\u4e3aPNG\u56fe\u7247<\/strong><\/h2>\n<p>plt.savefig(&#39;output.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>savefig()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u56fe\u8868\u4fdd\u5b58\u4e3a\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u5bfc\u51fa\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u7b80\u5355\u7684CSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684csv\u6a21\u5757\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u548c\u591a\u79cd\u683c\u5f0f\u7684\u5bfc\u51fa\uff0cPandas\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff1b\u5bf9\u4e8e\u4e0e\u6570\u636e\u5e93\u7684\u4ea4\u4e92\uff0cSQLAlchemy\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u5bf9\u4e8e\u751f\u6210\u56fe\u8868\u5e76\u5bfc\u51fa\u4e3a\u56fe\u7247\uff0cMatplotlib\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u6570\u636e\u5bfc\u51fa\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u51fa\u6570\u636e\u5230CSV\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5bfc\u51fa\u6570\u636e\u5230CSV\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u3002\u9996\u5148\u9700\u8981\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u5e76\u521b\u5efa\u4e00\u4e2a<code>csv.writer<\/code>\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u901a\u8fc7<code>writerow()<\/code>\u6216<code>writerows()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import csv\n\ndata = [[&#39;Name&#39;, &#39;Age&#39;], [&#39;Alice&#39;, 30], [&#39;Bob&#39;, 25]]\nwith open(&#39;output.csv&#39;, mode=&#39;w&#39;, newline=&#39;&#39;) as file:\n    writer = csv.writer(file)\n    writer.writerows(data)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u4ee5\u4e0a\u4ee3\u7801\u540e\uff0c\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u751f\u6210\u4e00\u4e2a\u540d\u4e3a<code>output.csv<\/code>\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>Python\u53ef\u4ee5\u5bfc\u51fa\u5230\u54ea\u4e9b\u683c\u5f0f\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u9664\u4e86CSV\u6587\u4ef6\uff0cPython\u8fd8\u652f\u6301\u5bfc\u51fa\u5230\u591a\u79cd\u6587\u4ef6\u683c\u5f0f\uff0c\u5305\u62ecExcel\uff08\u4f7f\u7528<code>pandas<\/code>\u5e93\uff09\u3001JSON\u3001TXT\u7b49\u3002\u5bf9\u4e8eExcel\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>to_excel()<\/code>\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;], &#39;Age&#39;: [30, 25]}\ndf = pd.DataFrame(data)\ndf.to_excel(&#39;output.xlsx&#39;, index=False)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u5bfc\u51fa\u8868\u683c\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5bfc\u51fa\u6570\u636e\u5e93\u5185\u5bb9\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5bfc\u51fa\u6570\u636e\u5e93\u5185\u5bb9\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u5de5\u5177\u5982SQLAlchemy\uff0c\u914d\u5408<code>pandas<\/code>\u5e93\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002\u901a\u8fc7ORM\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u800c<code>pandas<\/code>\u5219\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528SQLAlchemy\u67e5\u8be2\u6570\u636e\u5e93\u540e\uff0c\u53ef\u4ee5\u5c06\u7ed3\u679c\u76f4\u63a5\u8f6c\u4e3aDataFrame\uff0c\u5e76\u4f7f\u7528<code>to_csv()<\/code>\u7b49\u65b9\u6cd5\u5bfc\u51fa\uff1a<\/p>\n<pre><code class=\"language-python\">from sqlalchemy import create_engine\nimport pandas as pd\n\nengine = create_engine(&#39;sqlite:\/\/\/example.db&#39;)\ndf = pd.read_sql(&#39;SELECT * FROM users&#39;, con=engine)\ndf.to_csv(&#39;users.csv&#39;, index=False)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u80fd\u6709\u6548\u5730\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5bfc\u51fa\u6570\u636e\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u5e93\u6216\u7b2c\u4e09\u65b9\u5e93\u6765\u5b9e\u73b0\uff0c\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528CSV\u6a21\u5757\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\u3001 [&hellip;]","protected":false},"author":3,"featured_media":924366,"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\/924364"}],"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=924364"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/924364\/revisions"}],"predecessor-version":[{"id":924367,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/924364\/revisions\/924367"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/924366"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=924364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=924364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=924364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}