{"id":1104886,"date":"2025-01-08T16:25:35","date_gmt":"2025-01-08T08:25:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1104886.html"},"modified":"2025-01-08T16:25:35","modified_gmt":"2025-01-08T08:25:35","slug":"python%e4%b8%ad%e8%be%93%e5%87%ba%e5%ad%97%e7%ac%a6%e5%a6%82%e4%bd%95%e5%9e%82%e7%9b%b4%e5%af%b9%e9%bd%90","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1104886.html","title":{"rendered":"python\u4e2d\u8f93\u51fa\u5b57\u7b26\u5982\u4f55\u5782\u76f4\u5bf9\u9f50"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070033\/3f6a0663-5c2b-438c-b6cc-ca2b7a2f351e.webp\" alt=\"python\u4e2d\u8f93\u51fa\u5b57\u7b26\u5982\u4f55\u5782\u76f4\u5bf9\u9f50\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8f93\u51fa\u5b57\u7b26\u5782\u76f4\u5bf9\u9f50\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001tabulate\u6a21\u5757\u3001pandas\u6a21\u5757\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662f\u6700\u5e38\u89c1\u4e14\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5bbd\u5ea6\u548c\u5bf9\u9f50\u65b9\u5f0f\u6765\u5b9e\u73b0\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528f-string\u3001format()\u65b9\u6cd5\u6216\u4f20\u7edf\u7684\u767e\u5206\u53f7 (%) \u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5bbd\u5ea6\u548c\u5bf9\u9f50\u65b9\u5f0f\u6765\u5b9e\u73b0\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528f-string<\/h4>\n<\/p>\n<p><p>f-string\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5b83\u5177\u6709\u7b80\u5355\u3001\u76f4\u89c2\u3001\u53ef\u8bfb\u6027\u5f3a\u7684\u7279\u70b9\u3002\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\uff0c\u5e76\u4f7f\u7528\u5192\u53f7\u6307\u5b9a\u5bf9\u9f50\u65b9\u5f0f\u548c\u5bbd\u5ea6\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;Los Angeles&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Chicago&quot;}<\/p>\n<p>]<\/p>\n<p>for item in data:<\/p>\n<p>    print(f&quot;{item[&#39;name&#39;]:&lt;10} {item[&#39;age&#39;]:&lt;5} {item[&#39;city&#39;]:&lt;15}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>:&lt;10<\/code> \u8868\u793a\u5de6\u5bf9\u9f50\u5e76\u4e14\u5bbd\u5ea6\u4e3a10\uff0c<code>:&lt;5<\/code> \u8868\u793a\u5de6\u5bf9\u9f50\u5e76\u4e14\u5bbd\u5ea6\u4e3a5\uff0c<code>:&lt;15<\/code> \u8868\u793a\u5de6\u5bf9\u9f50\u5e76\u4e14\u5bbd\u5ea6\u4e3a15\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u8f93\u51fa\u7684\u5b57\u7b26\u5728\u5782\u76f4\u65b9\u5411\u4e0a\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528format()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>format()<\/code>\u65b9\u6cd5\u662fPython 2.7\u548c3.0\u5f15\u5165\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u4e0ef-string\u7c7b\u4f3c\uff0c\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\uff0c\u5e76\u4f7f\u7528\u5192\u53f7\u6307\u5b9a\u5bf9\u9f50\u65b9\u5f0f\u548c\u5bbd\u5ea6\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;Los Angeles&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Chicago&quot;}<\/p>\n<p>]<\/p>\n<p>for item in data:<\/p>\n<p>    print(&quot;{:&lt;10} {:&lt;5} {:&lt;15}&quot;.format(item[&#39;name&#39;], item[&#39;age&#39;], item[&#39;city&#39;]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>{:&lt;10}<\/code> \u8868\u793a\u5de6\u5bf9\u9f50\u5e76\u4e14\u5bbd\u5ea6\u4e3a10\uff0c<code>{:&lt;5}<\/code> \u8868\u793a\u5de6\u5bf9\u9f50\u5e76\u4e14\u5bbd\u5ea6\u4e3a5\uff0c<code>{:&lt;15}<\/code> \u8868\u793a\u5de6\u5bf9\u9f50\u5e76\u4e14\u5bbd\u5ea6\u4e3a15\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u8f93\u51fa\u7684\u5b57\u7b26\u5728\u5782\u76f4\u65b9\u5411\u4e0a\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<p><h4>1.3 \u4f7f\u7528\u767e\u5206\u53f7 (%) \u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>\u767e\u5206\u53f7 (%) \u683c\u5f0f\u5316\u662fPython\u4e2d\u6700\u65e9\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u5360\u4f4d\u7b26\uff0c\u5e76\u4f7f\u7528\u767e\u5206\u53f7\u6307\u5b9a\u5bf9\u9f50\u65b9\u5f0f\u548c\u5bbd\u5ea6\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;Los Angeles&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Chicago&quot;}<\/p>\n<p>]<\/p>\n<p>for item in data:<\/p>\n<p>    print(&quot;%-10s %-5d %-15s&quot; % (item[&#39;name&#39;], item[&#39;age&#39;], item[&#39;city&#39;]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>%-10s<\/code> \u8868\u793a\u5de6\u5bf9\u9f50\u5e76\u4e14\u5bbd\u5ea6\u4e3a10\uff0c<code>%-5d<\/code> \u8868\u793a\u5de6\u5bf9\u9f50\u5e76\u4e14\u5bbd\u5ea6\u4e3a5\uff0c<code>%-15s<\/code> \u8868\u793a\u5de6\u5bf9\u9f50\u5e76\u4e14\u5bbd\u5ea6\u4e3a15\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u8f93\u51fa\u7684\u5b57\u7b26\u5728\u5782\u76f4\u65b9\u5411\u4e0a\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528tabulate\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>tabulate<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u8868\u683c\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u5bf9\u9f50\u65b9\u5f0f\u3002\u901a\u8fc7\u5b89\u88c5\u5e76\u4f7f\u7528<code>tabulate<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from tabulate import tabulate<\/p>\n<p>data = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;Los Angeles&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Chicago&quot;}<\/p>\n<p>]<\/p>\n<p>headers = [&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;]<\/p>\n<p>table = [[item[&#39;name&#39;], item[&#39;age&#39;], item[&#39;city&#39;]] for item in data]<\/p>\n<p>print(tabulate(table, headers, tablefmt=&quot;grid&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b89\u88c5\u5e76\u5bfc\u5165\u4e86<code>tabulate<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u6570\u636e\u548c\u8868\u5934\uff0c\u5e76\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u683c\u5f0f\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528<code>tabulate()<\/code>\u51fd\u6570\u5e76\u6307\u5b9a\u8868\u683c\u683c\u5f0f<code>tablefmt=&quot;grid&quot;<\/code>\uff0c\u5b9e\u73b0\u4e86\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pandas\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u8868\u683c\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u5bf9\u9f50\u65b9\u5f0f\u3002\u901a\u8fc7\u5b89\u88c5\u5e76\u4f7f\u7528<code>pandas<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;Los Angeles&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Chicago&quot;}<\/p>\n<p>]<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df.to_string(index=False))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b89\u88c5\u5e76\u5bfc\u5165\u4e86<code>pandas<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u6570\u636e\uff0c\u5e76\u5c06\u6570\u636e\u8f6c\u6362\u4e3a<code>DataFrame<\/code>\u683c\u5f0f\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528<code>to_string()<\/code>\u51fd\u6570\u5e76\u6307\u5b9a\u53c2\u6570<code>index=False<\/code>\uff0c\u5b9e\u73b0\u4e86\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528prettytable\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>prettytable<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u8868\u683c\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u5bf9\u9f50\u65b9\u5f0f\u3002\u901a\u8fc7\u5b89\u88c5\u5e76\u4f7f\u7528<code>prettytable<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from prettytable import PrettyTable<\/p>\n<p>data = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;Los Angeles&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Chicago&quot;}<\/p>\n<p>]<\/p>\n<p>table = PrettyTable()<\/p>\n<p>table.field_names = [&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;]<\/p>\n<p>for item in data:<\/p>\n<p>    table.add_row([item[&#39;name&#39;], item[&#39;age&#39;], item[&#39;city&#39;]])<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b89\u88c5\u5e76\u5bfc\u5165\u4e86<code>prettytable<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u6570\u636e\u548c\u8868\u5934\uff0c\u5e76\u5c06\u6570\u636e\u6dfb\u52a0\u5230\u8868\u683c\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528<code>print()<\/code>\u51fd\u6570\uff0c\u8f93\u51fa\u4e86\u5b57\u7b26\u5782\u76f4\u5bf9\u9f50\u7684\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528csv\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>csv<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406CSV\u6587\u4ef6\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u5bf9\u9f50\u65b9\u5f0f\u3002\u901a\u8fc7\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;Los Angeles&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Chicago&quot;}<\/p>\n<p>]<\/p>\n<p>with open(&quot;output.csv&quot;, &quot;w&quot;, newline=&quot;&quot;) as file:<\/p>\n<p>    writer = csv.DictWriter(file, fieldnames=[&quot;name&quot;, &quot;age&quot;, &quot;city&quot;])<\/p>\n<p>    writer.writeheader()<\/p>\n<p>    writer.writerows(data)<\/p>\n<p>with open(&quot;output.csv&quot;, &quot;r&quot;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(&quot;{:&lt;10} {:&lt;5} {:&lt;15}&quot;.format(*row))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>csv<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u6570\u636e\uff0c\u5e76\u5c06\u6570\u636e\u5199\u5165\u5230CSV\u6587\u4ef6\u4e2d\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4e86\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528texttable\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>texttable<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u8868\u683c\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u5bf9\u9f50\u65b9\u5f0f\u3002\u901a\u8fc7\u5b89\u88c5\u5e76\u4f7f\u7528<code>texttable<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from texttable import Texttable<\/p>\n<p>data = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;Los Angeles&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Chicago&quot;}<\/p>\n<p>]<\/p>\n<p>table = Texttable()<\/p>\n<p>table.add_rows([[&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;]] + [[item[&#39;name&#39;], item[&#39;age&#39;], item[&#39;city&#39;]] for item in data])<\/p>\n<p>print(table.draw())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b89\u88c5\u5e76\u5bfc\u5165\u4e86<code>texttable<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u6570\u636e\u548c\u8868\u5934\uff0c\u5e76\u5c06\u6570\u636e\u6dfb\u52a0\u5230\u8868\u683c\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528<code>draw()<\/code>\u51fd\u6570\uff0c\u8f93\u51fa\u4e86\u5b57\u7b26\u5782\u76f4\u5bf9\u9f50\u7684\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528terminaltables\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>terminaltables<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u8868\u683c\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u5bf9\u9f50\u65b9\u5f0f\u3002\u901a\u8fc7\u5b89\u88c5\u5e76\u4f7f\u7528<code>terminaltables<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from terminaltables import AsciiTable<\/p>\n<p>data = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;Los Angeles&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Chicago&quot;}<\/p>\n<p>]<\/p>\n<p>table_data = [[&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;]] + [[item[&#39;name&#39;], item[&#39;age&#39;], item[&#39;city&#39;]] for item in data]<\/p>\n<p>table = AsciiTable(table_data)<\/p>\n<p>print(table.table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b89\u88c5\u5e76\u5bfc\u5165\u4e86<code>terminaltables<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u6570\u636e\u548c\u8868\u5934\uff0c\u5e76\u5c06\u6570\u636e\u6dfb\u52a0\u5230\u8868\u683c\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528<code>table<\/code>\u5c5e\u6027\uff0c\u8f93\u51fa\u4e86\u5b57\u7b26\u5782\u76f4\u5bf9\u9f50\u7684\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528tabulate\u6a21\u5757\u7684\u9ad8\u7ea7\u529f\u80fd<\/h3>\n<\/p>\n<p><p><code>tabulate<\/code>\u6a21\u5757\u4e0d\u4ec5\u53ef\u4ee5\u5b9e\u73b0\u57fa\u672c\u7684\u8868\u683c\u683c\u5f0f\u5316\uff0c\u8fd8\u652f\u6301\u591a\u79cd\u9ad8\u7ea7\u529f\u80fd\uff0c\u4f8b\u5982\u8c03\u6574\u5217\u5bbd\u3001\u6307\u5b9a\u5bf9\u9f50\u65b9\u5f0f\u3001\u6dfb\u52a0\u8fb9\u6846\u7b49\u3002\u901a\u8fc7\u4f7f\u7528<code>tabulate<\/code>\u6a21\u5757\u7684\u9ad8\u7ea7\u529f\u80fd\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u5b57\u7b26\u5782\u76f4\u5bf9\u9f50\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from tabulate import tabulate<\/p>\n<p>data = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;Los Angeles&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Chicago&quot;}<\/p>\n<p>]<\/p>\n<p>headers = [&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;]<\/p>\n<p>table = [[item[&#39;name&#39;], item[&#39;age&#39;], item[&#39;city&#39;]] for item in data]<\/p>\n<p>print(tabulate(table, headers, tablefmt=&quot;fancy_grid&quot;, colalign=(&quot;left&quot;, &quot;center&quot;, &quot;right&quot;)))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b89\u88c5\u5e76\u5bfc\u5165\u4e86<code>tabulate<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u6570\u636e\u548c\u8868\u5934\uff0c\u5e76\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u683c\u5f0f\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u8c03\u7528<code>tabulate()<\/code>\u51fd\u6570\u5e76\u6307\u5b9a\u8868\u683c\u683c\u5f0f<code>tablefmt=&quot;fancy_grid&quot;<\/code>\u548c\u5217\u5bf9\u9f50\u65b9\u5f0f<code>colalign=(&quot;left&quot;, &quot;center&quot;, &quot;right&quot;)<\/code>\uff0c\u5b9e\u73b0\u4e86\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528prettytable\u6a21\u5757\u7684\u9ad8\u7ea7\u529f\u80fd<\/h3>\n<\/p>\n<p><p><code>prettytable<\/code>\u6a21\u5757\u4e0d\u4ec5\u53ef\u4ee5\u5b9e\u73b0\u57fa\u672c\u7684\u8868\u683c\u683c\u5f0f\u5316\uff0c\u8fd8\u652f\u6301\u591a\u79cd\u9ad8\u7ea7\u529f\u80fd\uff0c\u4f8b\u5982\u8c03\u6574\u5217\u5bbd\u3001\u6307\u5b9a\u5bf9\u9f50\u65b9\u5f0f\u3001\u6dfb\u52a0\u8fb9\u6846\u7b49\u3002\u901a\u8fc7\u4f7f\u7528<code>prettytable<\/code>\u6a21\u5757\u7684\u9ad8\u7ea7\u529f\u80fd\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u5b57\u7b26\u5782\u76f4\u5bf9\u9f50\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from prettytable import PrettyTable<\/p>\n<p>data = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;Los Angeles&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Chicago&quot;}<\/p>\n<p>]<\/p>\n<p>table = PrettyTable()<\/p>\n<p>table.field_names = [&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;]<\/p>\n<p>for item in data:<\/p>\n<p>    table.add_row([item[&#39;name&#39;], item[&#39;age&#39;], item[&#39;city&#39;]])<\/p>\n<p>table.align[&quot;Name&quot;] = &quot;l&quot;<\/p>\n<p>table.align[&quot;Age&quot;] = &quot;c&quot;<\/p>\n<p>table.align[&quot;City&quot;] = &quot;r&quot;<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b89\u88c5\u5e76\u5bfc\u5165\u4e86<code>prettytable<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u6570\u636e\u548c\u8868\u5934\uff0c\u5e76\u5c06\u6570\u636e\u6dfb\u52a0\u5230\u8868\u683c\u4e2d\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u8bbe\u7f6e\u5217\u5bf9\u9f50\u65b9\u5f0f<code>table.align[&quot;Name&quot;] = &quot;l&quot;<\/code>\u3001<code>table.align[&quot;Age&quot;] = &quot;c&quot;<\/code>\u3001<code>table.align[&quot;City&quot;] = &quot;r&quot;<\/code>\uff0c\u5b9e\u73b0\u4e86\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001tabulate\u6a21\u5757\u3001pandas\u6a21\u5757\u3001prettytable\u6a21\u5757\u3001csv\u6a21\u5757\u3001texttable\u6a21\u5757\u3001terminaltables\u6a21\u5757\u7b49\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0Python\u4e2d\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u8f93\u51fa\u7684\u7f8e\u89c2\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u5316\u529f\u80fd\u6765\u5b9e\u73b0\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.ljust()<\/code>\u3001<code>str.rjust()<\/code>\u548c<code>str.center()<\/code>\u65b9\u6cd5\u6765\u8c03\u6574\u6bcf\u4e00\u884c\u7684\u5b57\u7b26\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b9e\u73b0\u7eb5\u5411\u5bf9\u9f50\u3002\u901a\u8fc7\u9002\u5f53\u7684\u5faa\u73af\u548c\u5217\u8868\u751f\u6210\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5e76\u8f93\u51fa\u4e3a\u5782\u76f4\u5bf9\u9f50\u7684\u5f62\u5f0f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u591a\u884c\u6587\u672c\u7684\u5bf9\u9f50\u95ee\u9898\uff1f<\/strong><br \/>\u5904\u7406\u591a\u884c\u6587\u672c\u7684\u5bf9\u9f50\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5206\u5272\u548c\u8fde\u63a5\u529f\u80fd\u3002\u53ef\u4ee5\u5c06\u6587\u672c\u6309\u884c\u5206\u5272\u6210\u5217\u8868\uff0c\u7136\u540e\u9488\u5bf9\u6bcf\u884c\u4f7f\u7528\u76f8\u540c\u7684\u5bf9\u9f50\u65b9\u6cd5\u3002\u8fd9\u6837\u5c31\u80fd\u786e\u4fdd\u6bcf\u884c\u7684\u5b57\u7b26\u5728\u8f93\u51fa\u65f6\u80fd\u591f\u6574\u9f50\u6392\u5217\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>pandas<\/code>\u5e93\u4e2d\u7684DataFrame\u6765\u5904\u7406\u66f4\u590d\u6742\u7684\u5bf9\u9f50\u9700\u6c42\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8868\u683c\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\u6765\u6f14\u793a\u5b57\u7b26\u7684\u5782\u76f4\u5bf9\u9f50\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u5982\u4f55\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u5782\u76f4\u5bf9\u9f50\uff1a<\/p>\n<pre><code class=\"language-python\">strings = [&quot;\u82f9\u679c&quot;, &quot;\u9999\u8549&quot;, &quot;\u6a31\u6843&quot;]\nmax_length = max(len(s) for s in strings)\n\nfor s in strings:\n    print(s.ljust(max_length))  # \u5de6\u5bf9\u9f50\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6240\u6709\u5b57\u7b26\u4e32\u90fd\u88ab\u5de6\u5bf9\u9f50\u5e76\u8f93\u51fa\u5728\u540c\u4e00\u5217\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u5782\u76f4\u5bf9\u9f50\u7684\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8f93\u51fa\u5b57\u7b26\u5782\u76f4\u5bf9\u9f50\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001tabulate\u6a21\u5757\u3001pandas\u6a21\u5757\u7b49\u3002 \u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104886"}],"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=1104886"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104886\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}