{"id":962127,"date":"2024-12-27T04:05:23","date_gmt":"2024-12-26T20:05:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/962127.html"},"modified":"2024-12-27T04:05:25","modified_gmt":"2024-12-26T20:05:25","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e5%87%ba%e8%a1%a8%e5%a4%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/962127.html","title":{"rendered":"python\u5982\u4f55\u6253\u5370\u51fa\u8868\u5934"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104157\/853cc69f-8411-4b27-b4ea-769cd617726c.webp\" alt=\"python\u5982\u4f55\u6253\u5370\u51fa\u8868\u5934\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6253\u5370\u51fa\u8868\u5934\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u76f4\u63a5\u6253\u5370\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u5229\u7528Pandas\u5e93\u3001\u4ee5\u53ca\u901a\u8fc7PrettyTable\u5e93\u8fdb\u884c\u6253\u5370\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u548cPrettyTable\u5e93\u662f\u4e24\u79cd\u8f83\u4e3a\u4e13\u4e1a\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684\u8868\u683c\u6570\u636e\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u76f4\u63a5\u6253\u5370\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u6253\u5370\u5b57\u7b26\u4e32\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u53ef\u4ee5\u901a\u8fc7Python\u5185\u7f6e\u7684<code>print()<\/code>\u51fd\u6570\u76f4\u63a5\u6253\u5370\u51fa\u8868\u5934\u53ca\u5176\u4e0b\u7684\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u3001\u6570\u636e\u91cf\u8f83\u5c0f\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>print(&quot;Name\\tAge\\tCity&quot;)<\/p>\n<p>print(&quot;Alice\\t30\\tNew York&quot;)<\/p>\n<p>print(&quot;Bob\\t25\\tLos Angeles&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u89c2\uff0c\u4f46\u7f3a\u70b9\u662f\u5bf9\u4e8e\u6570\u636e\u8f83\u591a\u6216\u8005\u683c\u5f0f\u8981\u6c42\u8f83\u9ad8\u7684\u60c5\u51b5\u4e0d\u592a\u9002\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4f7f\u5f97\u8868\u5934\u6253\u5370\u66f4\u52a0\u6574\u9f50\u7f8e\u89c2\uff0c\u901a\u8fc7Python\u7684\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u53ef\u4ee5\u5bf9\u8f93\u51fa\u5185\u5bb9\u8fdb\u884c\u5bf9\u9f50\u3001\u586b\u5145\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>header = &quot;{:&lt;10} {:&lt;5} {:&lt;15}&quot;.format(&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;)<\/p>\n<p>row1 = &quot;{:&lt;10} {:&lt;5} {:&lt;15}&quot;.format(&quot;Alice&quot;, &quot;30&quot;, &quot;New York&quot;)<\/p>\n<p>row2 = &quot;{:&lt;10} {:&lt;5} {:&lt;15}&quot;.format(&quot;Bob&quot;, &quot;25&quot;, &quot;Los Angeles&quot;)<\/p>\n<p>print(header)<\/p>\n<p>print(row1)<\/p>\n<p>print(row2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u5bf9\u9f50\u8f93\u51fa\u7684\u573a\u666f<\/strong>\uff0c\u53ef\u4ee5\u6839\u636e\u6570\u636e\u957f\u5ea6\u8c03\u6574\u683c\u5f0f\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\uff0c\u4ee5\u5b9e\u73b0\u4e0d\u540c\u7684\u5bf9\u9f50\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Pandas\u5e93<\/p>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u3001\u5904\u7406\u548c\u6253\u5370\u6570\u636e\u8868\u683c\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>data = {<\/p>\n<p>    &quot;Name&quot;: [&quot;Alice&quot;, &quot;Bob&quot;],<\/p>\n<p>    &quot;Age&quot;: [30, 25],<\/p>\n<p>    &quot;City&quot;: [&quot;New York&quot;, &quot;Los Angeles&quot;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6253\u5370\u8868\u5934<\/strong><\/h2>\n<p>print(df.columns.tolist())<\/p>\n<h2><strong>\u6253\u5370\u6574\u4e2a\u8868\u683c<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pandas\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6570\u636e\u8868\u683c\uff0c<strong>\u9002\u5408\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u4efb\u52a1<\/strong>\u3002\u901a\u8fc7<code>DataFrame<\/code>\u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u64cd\u4f5c\u548c\u6253\u5370\u8868\u5934\u53ca\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528PrettyTable\u5e93<\/p>\n<\/p>\n<p><p>PrettyTable\u5e93\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u6253\u5370\u7f8e\u89c2\u8868\u683c\u7684Python\u5e93\uff0c\u5b83\u53ef\u4ee5\u81ea\u52a8\u8c03\u6574\u5217\u5bbd\u5e76\u5bf9\u9f50\u6570\u636e\uff0c\u4f7f\u5f97\u8868\u683c\u6253\u5370\u66f4\u52a0\u7f8e\u89c2\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>\u8bbe\u7f6e\u8868\u5934<\/strong><\/h2>\n<p>table.field_names = [&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;]<\/p>\n<h2><strong>\u6dfb\u52a0\u884c\u6570\u636e<\/strong><\/h2>\n<p>table.add_row([&quot;Alice&quot;, 30, &quot;New York&quot;])<\/p>\n<p>table.add_row([&quot;Bob&quot;, 25, &quot;Los Angeles&quot;])<\/p>\n<h2><strong>\u6253\u5370\u8868\u683c<\/strong><\/h2>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>PrettyTable\u5e93<strong>\u975e\u5e38\u9002\u5408\u6253\u5370\u9700\u8981\u7f8e\u89c2\u5c55\u793a\u7684\u8868\u683c<\/strong>\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u8f93\u51fa\u5230\u7ec8\u7aef\u6216\u6587\u672c\u6587\u4ef6\u65f6\u3002\u901a\u8fc7\u7b80\u5355\u7684API\uff0c\u53ef\u4ee5\u5feb\u901f\u521b\u5efa\u548c\u6253\u5370\u6f02\u4eae\u7684\u8868\u683c\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6253\u5370\u8868\u5934\u53ca\u5176\u6570\u636e\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u573a\u5408\uff0c\u76f4\u63a5\u6253\u5370\u5b57\u7b26\u4e32\u6216\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5373\u53ef\u6ee1\u8db3\u9700\u6c42\uff1b\u800c\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u8f83\u5927\u6570\u636e\u96c6\u6216\u9700\u8981\u7f8e\u89c2\u8f93\u51fa\u7684\u60c5\u51b5\uff0c\u4f7f\u7528Pandas\u5e93\u6216PrettyTable\u5e93\u5219\u66f4\u52a0\u5408\u9002\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\uff0c\u5c24\u5176\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4efb\u52a1\u4e2d\uff0cPandas\u5e93\u56e0\u5176\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u800c\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5370\u8868\u5934\uff1f<\/strong><br \/>\u5728Python\u4e2d\u6253\u5370\u8868\u5934\u901a\u5e38\u6d89\u53ca\u4f7f\u7528\u5217\u8868\u6216\u5b57\u5178\u6765\u7ec4\u7ec7\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u76f4\u63a5\u8f93\u51fa\u8868\u5934\u5185\u5bb9\uff0c\u6216\u8005\u4f7f\u7528<code>pandas<\/code>\u5e93\u6765\u66f4\u4f18\u96c5\u5730\u5904\u7406\u548c\u6253\u5370\u8868\u683c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">header = [&#39;\u59d3\u540d&#39;, &#39;\u5e74\u9f84&#39;, &#39;\u57ce\u5e02&#39;]\nprint(&#39; | &#39;.join(header))\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5c06\u8868\u5934\u683c\u5f0f\u5316\u4e3a\u66f4\u6613\u8bfb\u7684\u5f62\u5f0f\u3002<\/p>\n<p><strong>\u4f7f\u7528pandas\u5e93\u6253\u5370\u8868\u5934\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4f7f\u7528<code>pandas<\/code>\u5e93\u53ef\u4ee5\u66f4\u52a0\u65b9\u4fbf\u5730\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u521b\u5efa\u4e00\u4e2aDataFrame\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528<code>.columns<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u8868\u5934\u3002\u4f8b\u5982\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;: [25, 30], &#39;\u57ce\u5e02&#39;: [&#39;\u5317\u4eac&#39;, &#39;\u4e0a\u6d77&#39;]}\ndf = pd.DataFrame(data)\nprint(df.columns.tolist())\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u83b7\u5f97\u8868\u5934\u7684\u5217\u8868\u5f62\u5f0f\uff0c\u6709\u52a9\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u8868\u5934\u7684\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\u81ea\u5b9a\u4e49\u8868\u5934\u7684\u683c\u5f0f\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.format()<\/code>\u6216f-string\u6765\u8c03\u6574\u8868\u5934\u7684\u5bf9\u9f50\u65b9\u5f0f\u6216\u6837\u5f0f\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">header = [&#39;\u59d3\u540d&#39;, &#39;\u5e74\u9f84&#39;, &#39;\u57ce\u5e02&#39;]\nprint(&#39;{:&lt;10} {:&lt;5} {:&lt;10}&#39;.format(*header))\n<\/code><\/pre>\n<p>\u6b64\u65b9\u6cd5\u5141\u8bb8\u4f60\u7075\u6d3b\u5730\u8bbe\u7f6e\u5217\u5bbd\u548c\u5bf9\u9f50\u65b9\u5f0f\uff0c\u4ee5\u4fbf\u4e8e\u9605\u8bfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6253\u5370\u51fa\u8868\u5934\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u76f4\u63a5\u6253\u5370\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u5229\u7528Pandas\u5e93\u3001\u4ee5\u53ca\u901a\u8fc7Pr [&hellip;]","protected":false},"author":3,"featured_media":962134,"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\/962127"}],"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=962127"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962127\/revisions"}],"predecessor-version":[{"id":962137,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962127\/revisions\/962137"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/962134"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=962127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=962127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=962127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}