{"id":1120989,"date":"2025-01-08T19:04:15","date_gmt":"2025-01-08T11:04:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1120989.html"},"modified":"2025-01-08T19:04:17","modified_gmt":"2025-01-08T11:04:17","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e6%8d%ae%e8%be%93%e5%87%ba%e4%b8%ba%e8%a1%a8%e6%a0%bc%e5%bd%a2%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1120989.html","title":{"rendered":"python\u5982\u4f55\u5c06\u6570\u636e\u8f93\u51fa\u4e3a\u8868\u683c\u5f62\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083428\/a9ee4c02-851c-466b-92b4-ce1ac8d0b6ee.webp\" alt=\"python\u5982\u4f55\u5c06\u6570\u636e\u8f93\u51fa\u4e3a\u8868\u683c\u5f62\u5f0f\" \/><\/p>\n<p><p> <strong>Python\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u636e\u8f93\u51fa\u4e3a\u8868\u683c\u5f62\u5f0f\uff0c\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u3001PrettyTable\u5e93\u3001Tabulate\u5e93\u3001\u4ee5\u53ca\u76f4\u63a5\u4f7f\u7528CSV\u6a21\u5757\u7b49\u3002<\/strong>\u5176\u4e2d\uff0cPandas\u5e93\u56e0\u4e3a\u5176\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u529f\u80fd\uff0c\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u6570\u636e\u8f93\u51fa\u4e3a\u8868\u683c\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528Pandas\u5e93<\/h2>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\u4e4b\u4e00\u3002\u5b83\u63d0\u4f9b\u4e86DataFrame\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u8868\u683c\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u5b89\u88c5Pandas<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Pandas\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.2 \u521b\u5efaDataFrame<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aDataFrame\u662f\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u8868\u683c\u5f62\u5f0f\u7684\u7b2c\u4e00\u6b65\u3002\u4f60\u53ef\u4ee5\u4ece\u591a\u79cd\u6570\u636e\u6e90\u521b\u5efaDataFrame\uff0c\u5305\u62ec\u5b57\u5178\u3001\u5217\u8868\u3001Excel\u6587\u4ef6\u548cSQL\u6570\u636e\u5e93\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4ece\u5b57\u5178\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [24, 27, 22],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;San Francisco&#39;, &#39;Los Angeles&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.3 \u8f93\u51fa\u4e3aCSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5c06DataFrame\u8f93\u51fa\u4e3a\u4e0d\u540c\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fCSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u51fa\u4e3aCSV\u6587\u4ef6<\/p>\n<p>df.to_csv(&#39;output.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.4 \u8f93\u51fa\u4e3aExcel\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>Pandas\u4e5f\u53ef\u4ee5\u5c06DataFrame\u8f93\u51fa\u4e3aExcel\u6587\u4ef6\uff0c\u524d\u63d0\u662f\u9700\u8981\u5b89\u88c5<code>openpyxl<\/code>\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\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><h2>\u4e8c\u3001\u4f7f\u7528PrettyTable\u5e93<\/h2>\n<\/p>\n<p><p>PrettyTable\u5e93\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u6f02\u4eae\u7684\u8868\u683c\u5f62\u5f0f\uff0c\u9002\u5408\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u5b89\u88c5PrettyTable<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5PrettyTable\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install prettytable<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 \u521b\u5efaPrettyTable\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u521b\u5efaPrettyTable\u5bf9\u8c61\u5e76\u6dfb\u52a0\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from prettytable import PrettyTable<\/p>\n<h2><strong>\u521b\u5efaPrettyTable\u5bf9\u8c61<\/strong><\/h2>\n<p>table = PrettyTable()<\/p>\n<h2><strong>\u6dfb\u52a0\u5217<\/strong><\/h2>\n<p>table.field_names = [&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;]<\/p>\n<p>table.add_row([&quot;Alice&quot;, 24, &quot;New York&quot;])<\/p>\n<p>table.add_row([&quot;Bob&quot;, 27, &quot;San Francisco&quot;])<\/p>\n<p>table.add_row([&quot;Charlie&quot;, 22, &quot;Los Angeles&quot;])<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528Tabulate\u5e93<\/h2>\n<\/p>\n<p><p>Tabulate\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u8868\u683c\u6837\u5f0f\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u4e0d\u540c\u98ce\u683c\u7684\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u5b89\u88c5Tabulate<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Tabulate\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tabulate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u4f7f\u7528Tabulate\u683c\u5f0f\u5316\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Tabulate\u5e93\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u8868\u683c\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from tabulate import tabulate<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [&quot;Alice&quot;, 24, &quot;New York&quot;],<\/p>\n<p>    [&quot;Bob&quot;, 27, &quot;San Francisco&quot;],<\/p>\n<p>    [&quot;Charlie&quot;, 22, &quot;Los Angeles&quot;]<\/p>\n<p>]<\/p>\n<h2><strong>\u8868\u5934<\/strong><\/h2>\n<p>headers = [&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;]<\/p>\n<p>print(tabulate(data, headers, tablefmt=&quot;grid&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528CSV\u6a21\u5757<\/h2>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6eCSV\u6a21\u5757\u4e5f\u53ef\u4ee5\u5c06\u6570\u636e\u8f93\u51fa\u4e3a\u8868\u683c\u5f62\u5f0f\uff0c\u9002\u5408\u7b80\u5355\u7684CSV\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u5bfc\u5165CSV\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528CSV\u6a21\u5757\u5c06\u6570\u636e\u8f93\u51fa\u4e3aCSV\u6587\u4ef6\u3002<\/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>    [&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;],<\/p>\n<p>    [&quot;Alice&quot;, 24, &quot;New York&quot;],<\/p>\n<p>    [&quot;Bob&quot;, 27, &quot;San Francisco&quot;],<\/p>\n<p>    [&quot;Charlie&quot;, 22, &quot;Los Angeles&quot;]<\/p>\n<p>]<\/p>\n<h2><strong>\u8f93\u51fa\u4e3aCSV\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><h2>\u4e94\u3001\u7efc\u5408\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u7efc\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u636e\u8f93\u51fa\u4e3a\u8868\u683c\u5f62\u5f0f\u3002\u6bd4\u5982\uff0c\u4f60\u53ef\u4ee5\u5148\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\uff0c\u7136\u540e\u4f7f\u7528PrettyTable\u6216Tabulate\u5e93\u5c06\u7ed3\u679c\u683c\u5f0f\u5316\u4e3a\u6f02\u4eae\u7684\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Pandas\u5904\u7406\u6570\u636e\uff0c\u5e76\u4f7f\u7528Tabulate\u5e93\u5c06\u7ed3\u679c\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from tabulate import tabulate<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [24, 27, 22],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;San Francisco&#39;, &#39;Los Angeles&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6570\u636e\u5904\u7406\uff08\u4f8b\u5982\u7b5b\u9009\u5e74\u9f84\u5927\u4e8e23\u7684\u4eba\uff09<\/strong><\/h2>\n<p>filtered_df = df[df[&#39;Age&#39;] &gt; 23]<\/p>\n<h2><strong>\u4f7f\u7528Tabulate\u683c\u5f0f\u5316\u8f93\u51fa<\/strong><\/h2>\n<p>print(tabulate(filtered_df, headers=&#39;keys&#39;, tablefmt=&#39;grid&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u5c06\u6570\u636e\u8f93\u51fa\u4e3a\u8868\u683c\u5f62\u5f0f\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u683c\u5f0f\u5316\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6570\u636e\u8f93\u51fa\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u548c\u62a5\u544a\u751f\u6210\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u636e\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aCSV\u683c\u5f0f\uff0c\u4f8b\u5982<code>pandas<\/code>\u5e93\u3002\u4f7f\u7528<code>pandas<\/code>\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06DataFrame\u5bf9\u8c61\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u3002\u53ea\u9700\u4f7f\u7528<code>to_csv()<\/code>\u65b9\u6cd5\uff0c\u5e76\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u5176\u4ed6\u53c2\u6570\uff0c\u5982\u5206\u9694\u7b26\u548c\u662f\u5426\u5305\u542b\u7d22\u5f15\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;\u59d3\u540d&#39;: [&#39;\u5f20\u4e09&#39;, &#39;\u674e\u56db&#39;], &#39;\u5e74\u9f84&#39;: [28, 22]}\ndf = pd.DataFrame(data)\ndf.to_csv(&#39;output.csv&#39;, index=False)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u6570\u636e\u8f6c\u6362\u4e3aExcel\u683c\u5f0f\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5c06\u6570\u636e\u8f93\u51fa\u4e3aExcel\u6587\u4ef6\uff0c<code>pandas<\/code>\u5e93\u540c\u6837\u53ef\u4ee5\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\u3002\u4f7f\u7528<code>to_excel()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06DataFrame\u76f4\u63a5\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\u3002\u786e\u4fdd\u5b89\u88c5\u4e86<code>openpyxl<\/code>\u6216<code>xlsxwriter<\/code>\u7b49\u5e93\u4f5c\u4e3aExcel\u6587\u4ef6\u7684\u5f15\u64ce\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;\u59d3\u540d&#39;: [&#39;\u5f20\u4e09&#39;, &#39;\u674e\u56db&#39;], &#39;\u5e74\u9f84&#39;: [28, 22]}\ndf = pd.DataFrame(data)\ndf.to_excel(&#39;output.xlsx&#39;, index=False)\n<\/code><\/pre>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5728Python\u4e2d\u521b\u5efa\u8868\u683c\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>pandas<\/code>\uff0c\u8fd8\u53ef\u4ee5\u501f\u52a9<code>PrettyTable<\/code>\u5e93\u6765\u5728\u63a7\u5236\u53f0\u4e2d\u4ee5\u8868\u683c\u5f62\u5f0f\u8f93\u51fa\u6570\u636e\u3002<code>PrettyTable<\/code>\u5141\u8bb8\u7528\u6237\u521b\u5efa\u7f8e\u89c2\u7684ASCII\u8868\u683c\uff0c\u5e76\u652f\u6301\u591a\u79cd\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">from prettytable import PrettyTable\n\ntable = PrettyTable()\ntable.field_names = [&quot;\u59d3\u540d&quot;, &quot;\u5e74\u9f84&quot;]\ntable.add_row([&quot;\u5f20\u4e09&quot;, 28])\ntable.add_row([&quot;\u674e\u56db&quot;, 22])\nprint(table)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u7528\u6237\u53ef\u4ee5\u7075\u6d3b\u5730\u5c06\u6570\u636e\u4ee5\u8868\u683c\u5f62\u5f0f\u8f93\u51fa\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u636e\u8f93\u51fa\u4e3a\u8868\u683c\u5f62\u5f0f\uff0c\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u3001PrettyTable\u5e93\u3001Tabula [&hellip;]","protected":false},"author":3,"featured_media":1121000,"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\/1120989"}],"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=1120989"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120989\/revisions"}],"predecessor-version":[{"id":1121001,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120989\/revisions\/1121001"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1121000"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1120989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1120989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1120989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}