{"id":1188517,"date":"2025-01-15T20:18:21","date_gmt":"2025-01-15T12:18:21","guid":{"rendered":""},"modified":"2025-01-15T20:18:25","modified_gmt":"2025-01-15T12:18:25","slug":"python%e5%a6%82%e4%bd%95%e7%bc%96%e7%a8%8b%e6%89%93%e5%8d%b0%e8%a1%a8%e6%a0%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1188517.html","title":{"rendered":"python\u5982\u4f55\u7f16\u7a0b\u6253\u5370\u8868\u683c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140829\/8a069343-6166-467c-abf4-436ee1316cd3.webp\" alt=\"python\u5982\u4f55\u7f16\u7a0b\u6253\u5370\u8868\u683c\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u6253\u5370\u8868\u683c\uff0c\u4f8b\u5982\u4f7f\u7528\u5185\u7f6e\u6a21\u5757<code>prettytable<\/code>\u3001<code>pandas<\/code>\uff0c\u4ee5\u53ca\u624b\u52a8\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7b49\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528<code>prettytable<\/code>\u6a21\u5757\u662f\u6700\u5e38\u89c1\u548c\u7b80\u4fbf\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>prettytable<\/code>\u6a21\u5757\u6765\u521b\u5efa\u548c\u6253\u5370\u8868\u683c<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165<code>prettytable<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>prettytable<\/code>\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u6a21\u5757\uff0c\u7528\u4e8e\u521b\u5efa\u7b80\u5355\u800c\u7f8e\u89c2\u7684ASCII\u8868\u683c\u3002\u9996\u5148\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u8be5\u6a21\u5757\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 prettytable<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u8be5\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from prettytable import PrettyTable<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u5e76\u6253\u5370\u8868\u683c<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>prettytable<\/code>\u6a21\u5757\u521b\u5efa\u8868\u683c\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u51e0\u884c\u4ee3\u7801\u5373\u53ef\u5b8c\u6210<\/strong>\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5b66\u751f\u6210\u7ee9\u7684\u8868\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from prettytable import PrettyTable<\/p>\n<h2><strong>\u521b\u5efa\u8868\u683c\u5bf9\u8c61<\/strong><\/h2>\n<p>table = PrettyTable()<\/p>\n<h2><strong>\u6dfb\u52a0\u5217\u6807\u9898<\/strong><\/h2>\n<p>table.field_names = [&quot;Student Name&quot;, &quot;Math&quot;, &quot;Science&quot;, &quot;English&quot;]<\/p>\n<h2><strong>\u6dfb\u52a0\u884c\u6570\u636e<\/strong><\/h2>\n<p>table.add_row([&quot;Alice&quot;, 85, 92, 88])<\/p>\n<p>table.add_row([&quot;Bob&quot;, 78, 85, 90])<\/p>\n<p>table.add_row([&quot;Charlie&quot;, 92, 88, 84])<\/p>\n<h2><strong>\u6253\u5370\u8868\u683c<\/strong><\/h2>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a<code>PrettyTable<\/code>\u5bf9\u8c61\uff0c\u7136\u540e\u901a\u8fc7<code>field_names<\/code>\u5c5e\u6027\u8bbe\u7f6e\u8868\u683c\u7684\u5217\u6807\u9898\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7<code>add_row<\/code>\u65b9\u6cd5\u6dfb\u52a0\u6bcf\u4e00\u884c\u7684\u6570\u636e\uff0c\u6700\u540e\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u8f93\u51fa\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b9a\u5236\u8868\u683c\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u521b\u5efa\u57fa\u672c\u7684\u8868\u683c\uff0c<code>prettytable<\/code>\u8fd8\u63d0\u4f9b\u4e86\u591a\u79cd\u5b9a\u5236\u9009\u9879\uff0c\u53ef\u4ee5\u8c03\u6574\u8868\u683c\u7684\u6837\u5f0f\u3001\u5bf9\u9f50\u65b9\u5f0f\u3001\u8fb9\u6846\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5b9a\u5236\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from prettytable import PrettyTable<\/p>\n<h2><strong>\u521b\u5efa\u8868\u683c\u5bf9\u8c61<\/strong><\/h2>\n<p>table = PrettyTable()<\/p>\n<h2><strong>\u6dfb\u52a0\u5217\u6807\u9898<\/strong><\/h2>\n<p>table.field_names = [&quot;Student Name&quot;, &quot;Math&quot;, &quot;Science&quot;, &quot;English&quot;]<\/p>\n<h2><strong>\u6dfb\u52a0\u884c\u6570\u636e<\/strong><\/h2>\n<p>table.add_row([&quot;Alice&quot;, 85, 92, 88])<\/p>\n<p>table.add_row([&quot;Bob&quot;, 78, 85, 90])<\/p>\n<p>table.add_row([&quot;Charlie&quot;, 92, 88, 84])<\/p>\n<h2><strong>\u8bbe\u7f6e\u5bf9\u9f50\u65b9\u5f0f<\/strong><\/h2>\n<p>table.align[&quot;Student Name&quot;] = &quot;l&quot;  # \u5de6\u5bf9\u9f50<\/p>\n<p>table.align[&quot;Math&quot;] = &quot;r&quot;          # \u53f3\u5bf9\u9f50<\/p>\n<p>table.align[&quot;Science&quot;] = &quot;r&quot;       # \u53f3\u5bf9\u9f50<\/p>\n<p>table.align[&quot;English&quot;] = &quot;r&quot;       # \u53f3\u5bf9\u9f50<\/p>\n<h2><strong>\u8bbe\u7f6e\u8868\u683c\u8fb9\u6846<\/strong><\/h2>\n<p>table.border = True<\/p>\n<h2><strong>\u6253\u5370\u8868\u683c<\/strong><\/h2>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u4e86<code>align<\/code>\u5c5e\u6027\u8bbe\u7f6e\u5404\u5217\u7684\u5bf9\u9f50\u65b9\u5f0f\uff0c<code>l<\/code>\u8868\u793a\u5de6\u5bf9\u9f50\uff0c<code>r<\/code>\u8868\u793a\u53f3\u5bf9\u9f50\u3002\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>border<\/code>\u5c5e\u6027\u6765\u63a7\u5236\u662f\u5426\u663e\u793a\u8868\u683c\u7684\u8fb9\u6846\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>pandas<\/code>\u521b\u5efa\u548c\u6253\u5370\u8868\u683c<\/h3>\n<\/p>\n<p><p><strong>\u9664\u4e86<code>prettytable<\/code>\uff0c<code>pandas<\/code>\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u6253\u5370\u8868\u683c<\/strong>\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>pandas<\/code>\u521b\u5efa\u548c\u6253\u5370\u8868\u683c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &quot;Student Name&quot;: [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;],<\/p>\n<p>    &quot;Math&quot;: [85, 78, 92],<\/p>\n<p>    &quot;Science&quot;: [92, 85, 88],<\/p>\n<p>    &quot;English&quot;: [88, 90, 84]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6253\u5370\u8868\u683c<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u6570\u636e\u7684\u5b57\u5178\uff0c\u7136\u540e\u4f7f\u7528<code>pandas<\/code>\u7684<code>DataFrame<\/code>\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6846\uff0c\u6700\u540e\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u8f93\u51fa\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u624b\u52a8\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u521b\u5efa\u8868\u683c<\/h3>\n<\/p>\n<p><p><strong>\u5982\u679c\u4e0d\u60f3\u4f9d\u8d56\u5916\u90e8\u6a21\u5757\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u521b\u5efa\u8868\u683c<\/strong>\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7a0d\u5fae\u9ebb\u70e6\u4e00\u4e9b\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u7075\u6d3b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u8868\u683c\u6570\u636e<\/p>\n<p>data = [<\/p>\n<p>    [&quot;Student Name&quot;, &quot;Math&quot;, &quot;Science&quot;, &quot;English&quot;],<\/p>\n<p>    [&quot;Alice&quot;, 85, 92, 88],<\/p>\n<p>    [&quot;Bob&quot;, 78, 85, 90],<\/p>\n<p>    [&quot;Charlie&quot;, 92, 88, 84]<\/p>\n<p>]<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u5217\u7684\u6700\u5927\u5bbd\u5ea6<\/strong><\/h2>\n<p>col_widths = [max(len(str(item)) for item in col) for col in zip(*data)]<\/p>\n<h2><strong>\u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>format_str = &quot; | &quot;.join([&quot;{:&lt;&quot; + str(width) + &quot;}&quot; for width in col_widths])<\/p>\n<h2><strong>\u6253\u5370\u8868\u683c<\/strong><\/h2>\n<p>for row in data:<\/p>\n<p>    print(format_str.format(*row))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u5b9a\u4e49\u4e86\u8868\u683c\u6570\u636e\uff0c\u7136\u540e\u8ba1\u7b97\u6bcf\u5217\u7684\u6700\u5927\u5bbd\u5ea6\uff0c\u63a5\u4e0b\u6765\u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u6700\u540e\u4f7f\u7528\u8be5\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u9010\u884c\u6253\u5370\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86Python\u4e2d\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u548c\u6253\u5370\u8868\u683c\uff0c\u5305\u62ec\u4f7f\u7528<code>prettytable<\/code>\u3001<code>pandas<\/code>\u4ee5\u53ca\u624b\u52a8\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u63a8\u8350\u4f7f\u7528<code>prettytable<\/code>\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u7528\uff0c\u529f\u80fd\u5f3a\u5927\uff0c\u80fd\u591f\u6ee1\u8db3\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7684\u9700\u6c42<\/strong>\u3002\u5982\u679c\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\uff0c<code>pandas<\/code>\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u9009\u62e9\u3002\u624b\u52a8\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u867d\u7136\u9ebb\u70e6\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u7075\u6d3b\u3002\u5e0c\u671b\u4ee5\u4e0a\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u6253\u5370\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u672c\u8868\u683c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u529f\u80fd\u6765\u521b\u5efa\u548c\u6253\u5370\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u672c\u8868\u683c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u7ed3\u5408\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6765\u5bf9\u9f50\u5404\u5217\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>str.format()<\/code>\u6216f-string\uff08Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\uff09\u6765\u5b9a\u4e49\u5217\u5bbd\u548c\u5bf9\u9f50\u65b9\u5f0f\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u66f4\u8f7b\u677e\u5730\u6253\u5370\u8868\u683c\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u6709\u51e0\u4e2a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u65b9\u4fbf\u5730\u6253\u5370\u8868\u683c\u3002\u4f8b\u5982\uff0c<code>PrettyTable<\/code>\u548c<code>Pandas<\/code>\u90fd\u662f\u975e\u5e38\u6d41\u884c\u7684\u5e93\u3002<code>PrettyTable<\/code>\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u6f02\u4eae\u7684ASCII\u8868\u683c\uff0c\u800c<code>Pandas<\/code>\u5219\u975e\u5e38\u5f3a\u5927\uff0c\u9002\u5408\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u683c\u5f0f\u826f\u597d\u7684\u8868\u683c\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5370\u5305\u542b\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u8868\u683c\uff1f<\/strong><br \/>\u5728Python\u4e2d\u6253\u5370\u5305\u542b\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u8868\u683c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u5982\u5217\u8868\u6216\u5b57\u5178\u6765\u5b58\u50a8\u8fd9\u4e9b\u6570\u636e\u3002\u7136\u540e\uff0c\u4f7f\u7528\u5faa\u73af\u904d\u5386\u8fd9\u4e9b\u7ed3\u6784\u5e76\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u786e\u4fdd\u5728\u683c\u5f0f\u5316\u65f6\u8003\u8651\u5230\u6bcf\u5217\u7684\u5bbd\u5ea6\u548c\u5bf9\u9f50\u65b9\u5f0f\uff0c\u4ee5\u786e\u4fdd\u8868\u683c\u7684\u53ef\u8bfb\u6027\u3002\u53ef\u4ee5\u5229\u7528<code>str()<\/code>\u65b9\u6cd5\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u4ee5\u4fbf\u8fdb\u884c\u6253\u5370\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u6253\u5370\u8868\u683c\uff0c\u4f8b\u5982\u4f7f\u7528\u5185\u7f6e\u6a21\u5757prettytable\u3001pandas\uff0c\u4ee5\u53ca\u624b\u52a8\u683c\u5f0f\u5316 [&hellip;]","protected":false},"author":3,"featured_media":1188524,"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\/1188517"}],"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=1188517"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1188517\/revisions"}],"predecessor-version":[{"id":1188527,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1188517\/revisions\/1188527"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1188524"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1188517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1188517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1188517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}