{"id":1000551,"date":"2024-12-27T09:51:05","date_gmt":"2024-12-27T01:51:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1000551.html"},"modified":"2024-12-27T09:51:10","modified_gmt":"2024-12-27T01:51:10","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%b8%8a%e4%b8%8b%e5%af%b9%e9%bd%90","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1000551.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u4e0a\u4e0b\u5bf9\u9f50"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075110\/1b30e316-c49b-4fa5-ad4d-e29b33eb2137.webp\" alt=\"python\u4e2d\u5982\u4f55\u4e0a\u4e0b\u5bf9\u9f50\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4e0a\u4e0b\u5bf9\u9f50\u6587\u672c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u683c\u5f0f\u5316\u5de5\u5177\u548c\u5e93\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982<code>str.center()<\/code>\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001<code>textwrap<\/code>\u6a21\u5757\u7b49\u3002\u4f7f\u7528<code>str.center()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7b80\u5355\u5730\u5b9e\u73b0\u4e0a\u4e0b\u5bf9\u9f50\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u53ef\u4ee5\u63d0\u4f9b\u66f4\u7075\u6d3b\u7684\u5bf9\u9f50\u65b9\u5f0f\u3001\u800c<code>textwrap<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u591a\u884c\u6587\u672c\u7684\u5bf9\u9f50\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>str.center()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>str.center()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u5c45\u4e2d\uff0c\u5e76\u5728\u4e24\u4fa7\u586b\u5145\u6307\u5b9a\u7684\u5b57\u7b26\u6765\u5b9e\u73b0\u4e0a\u4e0b\u5bf9\u9f50\u3002\u8be5\u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string.center(width, fillchar)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>width<\/code>\uff1a\u6307\u5b9a\u8981\u5bf9\u9f50\u7684\u5bbd\u5ea6\u3002<\/li>\n<li><code>fillchar<\/code>\uff1a\u6307\u5b9a\u586b\u5145\u5b57\u7b26\uff0c\u9ed8\u8ba4\u4e3a\u7a7a\u683c\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello&quot;<\/p>\n<p>aligned_text = text.center(20, &#39;*&#39;)<\/p>\n<p>print(aligned_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>&lt;strong&gt;&lt;\/strong&gt;&lt;strong&gt;*Hello&lt;\/strong&gt;&lt;strong&gt;&lt;\/strong&gt;*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u7528\u4e8e\u5bf9\u5355\u884c\u6587\u672c\u8fdb\u884c\u7b80\u5355\u7684\u4e0a\u4e0b\u5bf9\u9f50\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6587\u672c\u5bf9\u9f50\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7<code>format()<\/code>\u65b9\u6cd5\u6216f\u5b57\u7b26\u4e32\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>format()<\/code>\u65b9\u6cd5<\/li>\n<\/ol>\n<p><p><code>format()<\/code>\u65b9\u6cd5\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\u5e76\u63a7\u5236\u5176\u683c\u5f0f\uff0c\u4ee5\u4e0b\u662f\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">&quot;{:^20}&quot;.format(&quot;Hello&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5c06<code>&quot;Hello&quot;<\/code>\u5c45\u4e2d\u5bf9\u9f50\u572820\u4e2a\u5b57\u7b26\u5bbd\u5ea6\u7684\u533a\u57df\u5185\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528f\u5b57\u7b26\u4e32<\/li>\n<\/ol>\n<p><p>Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u652f\u6301f\u5b57\u7b26\u4e32\uff0c\u8fd9\u79cd\u65b9\u5f0f\u66f4\u52a0\u7b80\u6d01\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello&quot;<\/p>\n<p>aligned_text = f&quot;{text:^20}&quot;<\/p>\n<p>print(aligned_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e5f\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>       Hello       <\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>textwrap<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>textwrap<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u591a\u884c\u6587\u672c\u7684\u5bf9\u9f50\uff0c\u7279\u522b\u9002\u5408\u4e8e\u5904\u7406\u957f\u6587\u672c\u6216\u6bb5\u843d\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>textwrap.fill()<\/code><\/li>\n<\/ol>\n<p><p><code>textwrap.fill()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6587\u672c\u5206\u5272\u6210\u6307\u5b9a\u5bbd\u5ea6\u7684\u6bb5\u843d\uff0c\u540c\u65f6\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u5b57\u7b26\u4e32\u65b9\u6cd5\u5b9e\u73b0\u5bf9\u9f50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import textwrap<\/p>\n<p>text = &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&quot;<\/p>\n<p>wrapped_text = textwrap.fill(text, width=50)<\/p>\n<p>print(wrapped_text.center(60))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5c06\u6587\u672c\u5206\u5272\u4e3a\u5bbd\u5ea6\u4e3a50\u7684\u6bb5\u843d\uff0c\u5e76\u572860\u4e2a\u5b57\u7b26\u5bbd\u5ea6\u5185\u5c45\u4e2d\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528<code>textwrap.dedent()<\/code><\/li>\n<\/ol>\n<p><p><code>textwrap.dedent()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u53bb\u9664\u591a\u884c\u6587\u672c\u4e2d\u7684\u516c\u5171\u7f29\u8fdb\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u6574\u9f50\u7684\u5bf9\u9f50\u6548\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import textwrap<\/p>\n<p>text = &quot;&quot;&quot;<\/p>\n<p>    This is a paragraph that will be dedented.<\/p>\n<p>    Each line will have the same left margin.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>dedented_text = textwrap.dedent(text).strip()<\/p>\n<p>print(dedented_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u7528\u4e8e\u9700\u8981\u53bb\u9664\u591a\u884c\u6587\u672c\u4e2d\u4e0d\u5fc5\u8981\u7684\u7f29\u8fdb\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7ed3\u5408<code>numpy<\/code>\u548c<code>pandas<\/code>\u8fdb\u884c\u6570\u636e\u5bf9\u9f50<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u636e\u5904\u7406\u548c\u5206\u6790\uff0c\u7279\u522b\u662f\u6d89\u53ca\u5230\u8868\u683c\u6570\u636e\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u548c<code>pandas<\/code>\u5e93\u8fdb\u884c\u4e0a\u4e0b\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>numpy<\/code>\u5bf9\u9f50<\/li>\n<\/ol>\n<p><p><code>numpy<\/code>\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u5bf9\u9f50\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([1, 2, 3, 4, 5])<\/p>\n<p>aligned_data = np.pad(data, (5, 5), &#39;constant&#39;, constant_values=(0, 0))<\/p>\n<p>print(aligned_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5728\u6570\u7ec4\u7684\u4e24\u7aef\u586b\u5145\u96f6\uff0c\u4ee5\u5b9e\u73b0\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528<code>pandas<\/code>\u5bf9\u9f50<\/li>\n<\/ol>\n<p><p><code>pandas<\/code>\u63d0\u4f9b\u4e86\u5bf9\u6570\u636e\u6846\u8fdb\u884c\u5bf9\u9f50\u7684\u529f\u80fd\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8e\u6570\u636e\u5206\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6]<\/p>\n<p>})<\/p>\n<p>aligned_df = df.align(df, join=&#39;outer&#39;, axis=0)<\/p>\n<p>print(aligned_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5c06\u4e24\u4e2a\u6570\u636e\u6846\u5728\u884c\u6216\u5217\u4e0a\u8fdb\u884c\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5e94\u7528\u573a\u666f\u4e0e\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u6587\u672c\u5bf9\u9f50\u5728\u591a\u4e2a\u573a\u666f\u4e0b\u90fd\u6709\u5e94\u7528\uff0c\u5305\u62ec\u751f\u6210\u62a5\u544a\u3001\u6253\u5370\u8868\u683c\u3001\u65e5\u5fd7\u8bb0\u5f55\u7b49\u3002\u5728\u9009\u62e9\u5bf9\u9f50\u65b9\u6cd5\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u6765\u9009\u62e9\u5408\u9002\u7684\u65b9\u6848\u3002\u4f8b\u5982\uff0c\u5904\u7406\u5355\u884c\u6587\u672c\u65f6\uff0c\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u53ef\u80fd\u5c31\u8db3\u591f\uff1b\u5904\u7406\u591a\u884c\u6587\u672c\u65f6\uff0c<code>textwrap<\/code>\u6a21\u5757\u4f1a\u66f4\u4e3a\u5408\u9002\uff1b\u800c\u5728\u5904\u7406\u6570\u636e\u8868\u683c\u65f6\uff0c<code>numpy<\/code>\u548c<code>pandas<\/code>\u5219\u80fd\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u6765\u5b9e\u73b0\u6587\u672c\u548c\u6570\u636e\u7684\u4e0a\u4e0b\u5bf9\u9f50\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e0a\u4e0b\u5bf9\u9f50\u7684\u8f93\u51fa\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u5b9e\u73b0\u4e0a\u4e0b\u5bf9\u9f50\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>str.ljust()<\/code>\u548c<code>str.rjust()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5206\u522b\u5b9e\u73b0\u5de6\u5bf9\u9f50\u548c\u53f3\u5bf9\u9f50\uff0c<code>str.center()<\/code>\u53ef\u4ee5\u5b9e\u73b0\u5c45\u4e2d\u5bf9\u9f50\u3002\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5bbd\u5ea6\u53c2\u6570\u6765\u63a7\u5236\u5bf9\u9f50\u6548\u679c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">text = &quot;Hello&quot;\nprint(text.ljust(10))  # \u5de6\u5bf9\u9f50\nprint(text.rjust(10))  # \u53f3\u5bf9\u9f50\nprint(text.center(10)) # \u5c45\u4e2d\u5bf9\u9f50\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728\u6253\u5370\u8868\u683c\u65f6\u5b9e\u73b0\u4e0a\u4e0b\u5bf9\u9f50\uff1f<\/strong><br \/>\u5728\u6253\u5370\u8868\u683c\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u5faa\u73af\u7ed3\u6784\u6765\u5b9e\u73b0\u4e0a\u4e0b\u5bf9\u9f50\u3002\u901a\u8fc7\u8c03\u6574\u6bcf\u5217\u7684\u5bbd\u5ea6\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5728\u89c6\u89c9\u4e0a\u6574\u9f50\u6392\u5217\u3002\u53ef\u4ee5\u4f7f\u7528<code>format()<\/code>\u65b9\u6cd5\u6216f-string\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">data = [(&quot;Name&quot;, &quot;Age&quot;), (&quot;Alice&quot;, 30), (&quot;Bob&quot;, 25)]\nfor row in data:\n    print(&quot;{:&lt;10} {:&lt;10}&quot;.format(*row))  # \u5de6\u5bf9\u9f50\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528Pandas\u5e93\u5bf9\u6570\u636e\u8fdb\u884c\u4e0a\u4e0b\u5bf9\u9f50\uff1f<\/strong><br \/>Pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6570\u636e\u7684\u4e0a\u4e0b\u5bf9\u9f50\u3002\u4f7f\u7528<code>DataFrame<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5217\u5bbd\u548c\u5bf9\u9f50\u65b9\u5f0f\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u53ef\u4ee5\u5229\u7528<code>to_string()<\/code>\u65b9\u6cd5\u4e2d\u7684<code>justify<\/code>\u53c2\u6570\u6765\u8c03\u6574\u5217\u7684\u5bf9\u9f50\u65b9\u5f0f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;], &#39;Age&#39;: [30, 25]}\ndf = pd.DataFrame(data)\nprint(df.to_string(index=False, justify=&#39;left&#39;))  # \u5de6\u5bf9\u9f50\u8f93\u51fa\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4e0a\u4e0b\u5bf9\u9f50\u6587\u672c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u683c\u5f0f\u5316\u5de5\u5177\u548c\u5e93\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982str.center()\u3001\u683c\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":1000563,"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\/1000551"}],"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=1000551"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000551\/revisions"}],"predecessor-version":[{"id":1000566,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000551\/revisions\/1000566"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1000563"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1000551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1000551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1000551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}