{"id":1033539,"date":"2024-12-31T11:42:47","date_gmt":"2024-12-31T03:42:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1033539.html"},"modified":"2024-12-31T11:42:48","modified_gmt":"2024-12-31T03:42:48","slug":"python%e4%b8%ad%e5%ad%97%e5%85%b8%e5%a6%82%e4%bd%95%e6%a0%bc%e5%bc%8f%e5%8c%96%e8%be%93%e5%87%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1033539.html","title":{"rendered":"python\u4e2d\u5b57\u5178\u5982\u4f55\u683c\u5f0f\u5316\u8f93\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/9e9825f6-f069-432a-8bea-014af28a697f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5b57\u5178\u5982\u4f55\u683c\u5f0f\u5316\u8f93\u51fa\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684print\u51fd\u6570\u3001pprint\u6a21\u5757\u3001json\u6a21\u5757\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982PrettyTable\u548ctabulate\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u5b57\u5178<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6eprint\u51fd\u6570\u548cpprint\u6a21\u5757\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<strong>pprint<\/strong>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u4e14\u53ef\u8bfb\u6027\u9ad8\u7684\u65b9\u5f0f\u6765\u8f93\u51fa\u590d\u6742\u7684\u5b57\u5178\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684print\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5185\u7f6e\u7684print\u51fd\u6570\u662f\u6700\u57fa\u672c\u7684\u8f93\u51fa\u65b9\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528<strong>print\u51fd\u6570\u7ed3\u5408f-string\u3001str()\u6216repr()<\/strong>\u7b49\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528f-string<\/h4>\n<\/p>\n<p><p>f-string\u662fPython 3.6\u5f15\u5165\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\uff0c\u80fd\u591f\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<p>print(f&quot;My dictionary: {my_dict}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u5d4c\u5957\u7684\u5b57\u5178\u7ed3\u6784\uff0c\u5176\u53ef\u8bfb\u6027\u8f83\u5dee\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528str()\u6216repr()<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528str()\u6216repr()\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u5178\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<p>print(&quot;My dictionary: &quot; + str(my_dict))<\/p>\n<p>print(&quot;My dictionary: &quot; + repr(my_dict))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>str()\u65b9\u6cd5\u8f93\u51fa\u66f4\u53cb\u597d\uff0c\u800crepr()\u65b9\u6cd5\u8f93\u51fa\u66f4\u63a5\u8fd1\u539f\u59cb\u6570\u636e\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pprint\u6a21\u5757<\/h3>\n<\/p>\n<p><p>pprint\u6a21\u5757\u662fPython\u5185\u7f6e\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u683c\u5f0f\u5316\u590d\u6742\u7684\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>1\u3001pprint\u51fd\u6570<\/h4>\n<\/p>\n<p><p>pprint\u51fd\u6570\u53ef\u4ee5\u81ea\u52a8\u8c03\u6574\u8f93\u51fa\u683c\u5f0f\uff0c\u4f7f\u5176\u66f4\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pprint<\/p>\n<p>my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;, &quot;hobbies&quot;: [&quot;reading&quot;, &quot;hiking&quot;, &quot;coding&quot;]}<\/p>\n<p>pprint.pprint(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u590d\u6742\u7684\u5d4c\u5957\u5b57\u5178\u975e\u5e38\u6709\u6548\uff0c\u8f93\u51fa\u7ed3\u679c\u6574\u9f50\u3001\u7f8e\u89c2\u3002<\/p>\n<\/p>\n<p><h4>2\u3001PrettyPrinter\u7c7b<\/h4>\n<\/p>\n<p><p>PrettyPrinter\u7c7b\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u6bd4\u5982\u6307\u5b9a\u7f29\u8fdb\u3001\u5bbd\u5ea6\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pprint import PrettyPrinter<\/p>\n<p>my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;, &quot;hobbies&quot;: [&quot;reading&quot;, &quot;hiking&quot;, &quot;coding&quot;]}<\/p>\n<p>pp = PrettyPrinter(indent=4)<\/p>\n<p>pp.pprint(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8c03\u6574\u8f93\u51fa\u683c\u5f0f\uff0c\u4f7f\u5176\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528json\u6a21\u5757<\/h3>\n<\/p>\n<p><p>json\u6a21\u5757\u53ef\u4ee5\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u5b57\u7b26\u4e32\u8fdb\u884c\u8f93\u51fa\uff0c\u8fd9\u79cd\u65b9\u6cd5\u8f93\u51fa\u7684\u7ed3\u679c\u66f4\u52a0\u7ed3\u6784\u5316\u3002<\/p>\n<\/p>\n<p><h4>1\u3001json.dumps\u51fd\u6570<\/h4>\n<\/p>\n<p><p>json.dumps\u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u5e76\u53ef\u4ee5\u6307\u5b9a\u7f29\u8fdb\u7ea7\u522b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;, &quot;hobbies&quot;: [&quot;reading&quot;, &quot;hiking&quot;, &quot;coding&quot;]}<\/p>\n<p>json_str = json.dumps(my_dict, indent=4)<\/p>\n<p>print(json_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u8f93\u51fa\u7684\u7ed3\u679c\u5c42\u6b21\u5206\u660e\uff0c\u6613\u4e8e\u9605\u8bfb\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5185\u7f6e\u6a21\u5757\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u5b57\u5178\uff0c\u6bd4\u5982PrettyTable\u548ctabulate\u3002<\/p>\n<\/p>\n<p><h4>1\u3001PrettyTable<\/h4>\n<\/p>\n<p><p>PrettyTable\u5e93\u53ef\u4ee5\u7528\u6765\u751f\u6210\u7f8e\u89c2\u7684\u8868\u683c\uff0c\u9002\u7528\u4e8e\u5b57\u5178\u7684\u8868\u683c\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from prettytable import PrettyTable<\/p>\n<p>my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<p>table = PrettyTable()<\/p>\n<p>table.field_names = [&quot;Key&quot;, &quot;Value&quot;]<\/p>\n<p>for key, value in my_dict.items():<\/p>\n<p>    table.add_row([key, value])<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5c06\u5b57\u5178\u8f93\u51fa\u4e3a\u8868\u683c\u683c\u5f0f\uff0c\u4f7f\u5f97\u6570\u636e\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h4>2\u3001tabulate<\/h4>\n<\/p>\n<p><p>tabulate\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u8868\u683c\u683c\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u5b57\u5178\u8f6c\u6362\u4e3a\u8868\u683c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from tabulate import tabulate<\/p>\n<p>my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<p>table = tabulate(my_dict.items(), headers=[&quot;Key&quot;, &quot;Value&quot;])<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u9002\u5408\u5c06\u5b57\u5178\u8f93\u51fa\u4e3a\u8868\u683c\u683c\u5f0f\uff0c\u5e76\u4e14\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u8868\u683c\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u683c\u5f0f\u5316\u8f93\u51fa\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u5185\u7f6e\u7684print\u51fd\u6570\u3001pprint\u6a21\u5757\u3001json\u6a21\u5757\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982PrettyTable\u548ctabulate<\/strong>\u90fd\u53ef\u4ee5\u5b9e\u73b0\u5b57\u5178\u7684\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u5bf9\u4e8e\u4e00\u822c\u60c5\u51b5\uff0c\u4f7f\u7528<strong>pprint\u6a21\u5757<\/strong>\u662f\u6700\u7b80\u5355\u9ad8\u6548\u7684\u65b9\u5f0f\u3002\u5bf9\u4e8e\u9700\u8981\u7ed3\u6784\u5316\u8f93\u51fa\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>json\u6a21\u5757<\/strong>\u3002\u800c\u5bf9\u4e8e\u9700\u8981\u8868\u683c\u5316\u8f93\u51fa\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>PrettyTable\u6216tabulate<\/strong>\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u5b57\u5178\u8fdb\u884c\u7f8e\u5316\u8f93\u51fa\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u4e2d\u7684<code>dumps<\/code>\u65b9\u6cd5\u6765\u7f8e\u5316\u5b57\u5178\u7684\u8f93\u51fa\u3002\u901a\u8fc7\u8bbe\u7f6e<code>indent<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u8f93\u51fa\u7684\u5b57\u5178\u66f4\u6613\u8bfb\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\nmy_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}\nformatted_output = json.dumps(my_dict, indent=4)\nprint(formatted_output)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u4e00\u4e2a\u683c\u5f0f\u5316\u7684\u5b57\u5178\uff0c\u5176\u4e2d\u6bcf\u4e2a\u952e\u503c\u5bf9\u90fd\u5728\u65b0\u7684\u4e00\u884c\uff0c\u5e76\u4e14\u7f29\u8fdb\u6e05\u6670\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u683c\u5f0f\u5316\u5b57\u5178\u8f93\u51fa\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>json<\/code>\u6a21\u5757\uff0cPython\u7684<code>pprint<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u683c\u5f0f\u5316\u8f93\u51fa\u5b57\u5178\u3002<code>pprint<\/code>\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u8f93\u51fa\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\uff0c\u4f7f\u5176\u66f4\u52a0\u6613\u4e8e\u7406\u89e3\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from pprint import pprint\n\nmy_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;, &#39;hobbies&#39;: [&#39;reading&#39;, &#39;traveling&#39;]}\npprint(my_dict)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u4ee5\u66f4\u5177\u53ef\u8bfb\u6027\u7684\u65b9\u5f0f\u8f93\u51fa\u5b57\u5178\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5d4c\u5957\u5b57\u5178\u6216\u5217\u8868\u65f6\uff0c\u975e\u5e38\u6709\u6548\u3002<\/p>\n<p><strong>\u5728\u5b57\u5178\u4e2d\u5982\u4f55\u683c\u5f0f\u5316\u7279\u5b9a\u952e\u7684\u8f93\u51fa\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5bf9\u5b57\u5178\u4e2d\u7279\u5b9a\u952e\u7684\u503c\u8fdb\u884c\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u63d0\u53d6\u7279\u5b9a\u952e\u7684\u503c\uff0c\u5e76\u4ee5\u81ea\u5b9a\u4e49\u683c\u5f0f\u6253\u5370\uff1a<\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}\nprint(f&quot;Name: {my_dict[&#39;name&#39;]}, Age: {my_dict[&#39;age&#39;]}, City: {my_dict[&#39;city&#39;]}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u8f93\u51fa\u683c\u5f0f\uff0c\u4f7f\u5176\u66f4\u52a0\u7b26\u5408\u7279\u5b9a\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684print\u51fd\u6570\u3001pprint\u6a21\u5757\u3001json\u6a21\u5757\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982PrettyTa [&hellip;]","protected":false},"author":3,"featured_media":1033551,"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\/1033539"}],"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=1033539"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1033539\/revisions"}],"predecessor-version":[{"id":1033552,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1033539\/revisions\/1033552"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1033551"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1033539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1033539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1033539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}