{"id":1058109,"date":"2024-12-31T15:13:34","date_gmt":"2024-12-31T07:13:34","guid":{"rendered":""},"modified":"2024-12-31T15:13:36","modified_gmt":"2024-12-31T07:13:36","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python%e7%9a%84%e8%be%93%e5%87%ba%e6%95%b0%e6%8d%ae%e6%89%93%e5%87%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1058109.html","title":{"rendered":"\u5982\u4f55\u5c06python\u7684\u8f93\u51fa\u6570\u636e\u6253\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/8c58905c-9d9b-4bd2-9df5-442a2e87296f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5c06python\u7684\u8f93\u51fa\u6570\u636e\u6253\u51fa\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528print\u51fd\u6570\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u8f93\u51fa\u3001\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u8f93\u51fa\u6570\u636e\u6253\u5370\u51fa\u6765\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528print()\u51fd\u6570\u3002print()\u51fd\u6570\u53ef\u4ee5\u5c06\u6570\u636e\u76f4\u63a5\u6253\u5370\u5230\u63a7\u5236\u53f0\u4e0a\uff0c\u975e\u5e38\u9002\u5408\u8c03\u8bd5\u548c\u7b80\u5355\u7684\u8f93\u51fa\u9700\u6c42\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u63a7\u5236\u8f93\u51fa\u6570\u636e\u7684\u683c\u5f0f\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\uff0c\u4ee5\u4fbf\u5728\u540e\u7eed\u64cd\u4f5c\u4e2d\u8fdb\u884c\u4fdd\u5b58\u548c\u8bfb\u53d6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>print()\u51fd\u6570\u662fPython\u4e2d\u6700\u7b80\u5355\u548c\u6700\u5e38\u7528\u7684\u8f93\u51fa\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u76f4\u63a5\u5c06\u53d8\u91cf\u3001\u5b57\u7b26\u4e32\u548c\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u6253\u5370\u5230\u63a7\u5236\u53f0\u4e0a\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>height = 1.75<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528print()\u51fd\u6570\u5c06\u8fd9\u4e9b\u6570\u636e\u6253\u5370\u51fa\u6765\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Name:&quot;, name)<\/p>\n<p>print(&quot;Age:&quot;, age)<\/p>\n<p>print(&quot;Height:&quot;, height)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice<\/p>\n<p>Age: 30<\/p>\n<p>Height: 1.75<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5927\u591a\u6570\u8f93\u51fa\u9700\u6c42\u3002\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u66f4\u590d\u6742\u7684\u8f93\u51fa\u683c\u5f0f\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u8f93\u51fa\u6216\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528print\u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>print()\u51fd\u6570\u662fPython\u4e2d\u6700\u57fa\u7840\u7684\u8f93\u51fa\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u6570\u636e\u6253\u5370\u5230\u63a7\u5236\u53f0\u4e0a\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u7b80\u5355\u7684\u8f93\u51fa\u9700\u6c42\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>height = 1.75<\/p>\n<p>print(&quot;Name:&quot;, name)<\/p>\n<p>print(&quot;Age:&quot;, age)<\/p>\n<p>print(&quot;Height:&quot;, height)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e09\u4e2a\u53d8\u91cfname\u3001age\u548cheight\uff0c\u7136\u540e\u4f7f\u7528print()\u51fd\u6570\u5206\u522b\u5c06\u5b83\u4eec\u7684\u503c\u6253\u5370\u51fa\u6765\u3002print()\u51fd\u6570\u4f1a\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u4e0a\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice<\/p>\n<p>Age: 30<\/p>\n<p>Height: 1.75<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>print()\u51fd\u6570\u6709\u5f88\u591a\u53ef\u9009\u53c2\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9a\u5236\u8f93\u51fa\u683c\u5f0f\uff0c\u4f8b\u5982\u6307\u5b9a\u5206\u9694\u7b26\u3001\u7ed3\u675f\u7b26\u548c\u8f93\u51fa\u76ee\u6807\u7b49\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u548c\u7ed3\u675f\u7b26<\/p>\n<p>print(name, age, height, sep=&quot;, &quot;, end=&quot;.\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528sep\u53c2\u6570\u6307\u5b9a\u4e86\u5206\u9694\u7b26\u4e3a\u9017\u53f7\u548c\u7a7a\u683c\uff0c\u4f7f\u7528end\u53c2\u6570\u6307\u5b9a\u4e86\u7ed3\u675f\u7b26\u4e3a\u53e5\u53f7\u548c\u6362\u884c\u7b26\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>Alice, 30, 1.75.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>print()\u51fd\u6570\u8fd8\u53ef\u4ee5\u5c06\u8f93\u51fa\u6570\u636e\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u4ee5\u5907\u540e\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u91cd\u5b9a\u5411\u8f93\u51fa\u5230\u6587\u4ef6<\/p>\n<p>with open(&quot;output.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    print(&quot;Name:&quot;, name, file=file)<\/p>\n<p>    print(&quot;Age:&quot;, age, file=file)<\/p>\n<p>    print(&quot;Height:&quot;, height, file=file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528open()\u51fd\u6570\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a&quot;output.txt&quot;\u7684\u6587\u4ef6\uff0c\u5e76\u5c06print()\u51fd\u6570\u7684\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u8be5\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\uff0c\u6570\u636e\u5c06\u88ab\u5199\u5165\u6587\u4ef6\u800c\u4e0d\u662f\u6253\u5370\u5230\u63a7\u5236\u53f0\u4e0a\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u8f93\u51fa<\/h3>\n<\/p>\n<p><p><strong>\u683c\u5f0f\u5316\u8f93\u51fa\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u63a7\u5236\u8f93\u51fa\u6570\u636e\u7684\u683c\u5f0f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5b9a\u5236\u8f93\u51fa\u683c\u5f0f\u7684\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u5316\u8f93\u51fa\u7684\u65b9\u6cd5\uff0c\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-strings\uff09\u548cformat()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-strings\uff09<\/h4>\n<\/p>\n<p><p><strong>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-strings\uff09\u662f\u5728Python 3.6\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u683c\u5f0f\u5316\u8f93\u51fa\u65b9\u6cd5\uff0c\u5b83\u4f7f\u7528\u82b1\u62ec\u53f7{}\u6765\u8868\u793a\u53d8\u91cf\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcdf\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>height = 1.75<\/p>\n<p>print(f&quot;Name: {name}&quot;)<\/p>\n<p>print(f&quot;Age: {age}&quot;)<\/p>\n<p>print(f&quot;Height: {height:.2f}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528f-strings\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u6570\u636e\u3002\u5176\u4e2d\uff0c{name}\u3001{age}\u548c{height:.2f}\u5206\u522b\u8868\u793a\u53d8\u91cfname\u3001age\u548cheight\u7684\u503c\u3002height:.2f\u8868\u793a\u5c06height\u7684\u503c\u683c\u5f0f\u5316\u4e3a\u4e24\u4f4d\u5c0f\u6570\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice<\/p>\n<p>Age: 30<\/p>\n<p>Height: 1.75<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528format()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><strong>format()\u65b9\u6cd5\u662f\u5728Python 2.6\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u683c\u5f0f\u5316\u8f93\u51fa\u65b9\u6cd5\uff0c\u5b83\u4f7f\u7528\u82b1\u62ec\u53f7{}\u6765\u8868\u793a\u53d8\u91cf\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u540e\u8c03\u7528format()\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>height = 1.75<\/p>\n<p>print(&quot;Name: {}&quot;.format(name))<\/p>\n<p>print(&quot;Age: {}&quot;.format(age))<\/p>\n<p>print(&quot;Height: {:.2f}&quot;.format(height))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528format()\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u6570\u636e\u3002\u82b1\u62ec\u53f7{}\u8868\u793a\u5360\u4f4d\u7b26\uff0cformat()\u65b9\u6cd5\u4e2d\u7684\u53c2\u6570\u5c06\u4f9d\u6b21\u66ff\u6362\u5360\u4f4d\u7b26\u3002height:.2f\u8868\u793a\u5c06height\u7684\u503c\u683c\u5f0f\u5316\u4e3a\u4e24\u4f4d\u5c0f\u6570\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice<\/p>\n<p>Age: 30<\/p>\n<p>Height: 1.75<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\u548c\u547d\u540d\u53c2\u6570\u6765\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f4d\u7f6e\u53c2\u6570<\/p>\n<p>print(&quot;Name: {0}, Age: {1}, Height: {2:.2f}&quot;.format(name, age, height))<\/p>\n<h2><strong>\u547d\u540d\u53c2\u6570<\/strong><\/h2>\n<p>print(&quot;Name: {name}, Age: {age}, Height: {height:.2f}&quot;.format(name=name, age=age, height=height))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\u548c\u547d\u540d\u53c2\u6570\u6765\u6307\u5b9a\u53d8\u91cf\u7684\u4f4d\u7f6e\u548c\u540d\u79f0\uff0c\u4ece\u800c\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><strong>\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5c06\u8f93\u51fa\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u64cd\u4f5c\u548c\u5206\u6790\u3002<\/strong><\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528write()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><strong>write()\u65b9\u6cd5\u662fPython\u4e2d\u6700\u57fa\u7840\u7684\u6587\u4ef6\u5199\u5165\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u4e2d\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>height = 1.75<\/p>\n<p>with open(&quot;output.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;Name: {}\\n&quot;.format(name))<\/p>\n<p>    file.write(&quot;Age: {}\\n&quot;.format(age))<\/p>\n<p>    file.write(&quot;Height: {:.2f}\\n&quot;.format(height))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528open()\u51fd\u6570\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a&quot;output.txt&quot;\u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528write()\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u4e2d\u3002\u8f93\u51fa\u7ed3\u679c\u5c06\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u800c\u4e0d\u662f\u6253\u5370\u5230\u63a7\u5236\u53f0\u4e0a\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528writelines()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><strong>writelines()\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u5199\u5165\u6587\u4ef6\u4e2d\uff0c\u6bcf\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    &quot;Name: Alice\\n&quot;,<\/p>\n<p>    &quot;Age: 30\\n&quot;,<\/p>\n<p>    &quot;Height: 1.75\\n&quot;<\/p>\n<p>]<\/p>\n<p>with open(&quot;output.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.writelines(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868data\uff0c\u5e76\u4f7f\u7528writelines()\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\uff0c\u6bcf\u4e2a\u5b57\u7b26\u4e32\u5c06\u4f5c\u4e3a\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528csv\u6a21\u5757<\/h4>\n<\/p>\n<p><p><strong>csv\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bfb\u53d6\u548c\u5199\u5165CSV\uff08Comma-Separated Values\uff09\u6587\u4ef6\u7684\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [<\/p>\n<p>    [&quot;Name&quot;, &quot;Age&quot;, &quot;Height&quot;],<\/p>\n<p>    [&quot;Alice&quot;, 30, 1.75],<\/p>\n<p>    [&quot;Bob&quot;, 28, 1.80],<\/p>\n<p>    [&quot;Charlie&quot;, 25, 1.65]<\/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.writer(file)<\/p>\n<p>    writer.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868data\uff0c\u5e76\u4f7f\u7528csv\u6a21\u5757\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u4e2d\u3002\u6bcf\u4e2a\u5b50\u5217\u8868\u5c06\u4f5c\u4e3a\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p><strong>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528Python\u7684logging\u6a21\u5757\u6765\u8bb0\u5f55\u548c\u8f93\u51fa\u6570\u636e\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8bb0\u5f55\u65e5\u5fd7\u7684\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logging.basicConfig(level=logging.INFO, filename=&quot;app.log&quot;, filemode=&quot;w&quot;, format=&quot;%(name)s - %(levelname)s - %(message)s&quot;)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7\u4fe1\u606f<\/strong><\/h2>\n<p>logging.info(&quot;Name: Alice&quot;)<\/p>\n<p>logging.info(&quot;Age: 30&quot;)<\/p>\n<p>logging.info(&quot;Height: 1.75&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528logging\u6a21\u5757\u914d\u7f6e\u4e86\u4e00\u4e2a\u65e5\u5fd7\u8bb0\u5f55\u5668\uff0c\u5e76\u5c06\u65e5\u5fd7\u4fe1\u606f\u8bb0\u5f55\u5230\u540d\u4e3a&quot;app.log&quot;\u7684\u6587\u4ef6\u4e2d\u3002logging\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65e5\u5fd7\u7ea7\u522b\u548c\u683c\u5f0f\u5316\u9009\u9879\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u65e5\u5fd7\u8bb0\u5f55\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p><strong>Python\u751f\u6001\u7cfb\u7edf\u4e2d\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u65b9\u4fbf\u5730\u8f93\u51fa\u6570\u636e\uff0c\u4f8b\u5982pandas\u3001numpy\u548cprettytable\u7b49\u3002<\/strong><\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528pandas\u5e93<\/h4>\n<\/p>\n<p><p><strong>pandas\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u5206\u6790\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5904\u7406\u548c\u8f93\u51fa\u8868\u683c\u6570\u636e\u3002<\/strong><\/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;Bob&quot;, &quot;Charlie&quot;],<\/p>\n<p>    &quot;Age&quot;: [30, 28, 25],<\/p>\n<p>    &quot;Height&quot;: [1.75, 1.80, 1.65]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pandas\u5e93\u521b\u5efa\u4e86\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u5e76\u5c06\u6570\u636e\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u4e0a\u3002pandas\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u8f93\u51fa\u683c\u5f0f\u548c\u6587\u4ef6\u4fdd\u5b58\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528numpy\u5e93<\/h4>\n<\/p>\n<p><p><strong>numpy\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5904\u7406\u548c\u8f93\u51fa\u6570\u503c\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([<\/p>\n<p>    [&quot;Name&quot;, &quot;Age&quot;, &quot;Height&quot;],<\/p>\n<p>    [&quot;Alice&quot;, 30, 1.75],<\/p>\n<p>    [&quot;Bob&quot;, 28, 1.80],<\/p>\n<p>    [&quot;Charlie&quot;, 25, 1.65]<\/p>\n<p>])<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528numpy\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5e76\u5c06\u6570\u636e\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u4e0a\u3002numpy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u548c\u8f93\u51fa\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u79d1\u5b66\u8ba1\u7b97\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528prettytable\u5e93<\/h4>\n<\/p>\n<p><p><strong>prettytable\u5e93\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684\u8868\u683c\u8f93\u51fa\u5e93\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u6f02\u4eae\u7684\u8868\u683c\u8f93\u51fa\u3002<\/strong><\/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;Age&quot;, &quot;Height&quot;],<\/p>\n<p>    [&quot;Alice&quot;, 30, 1.75],<\/p>\n<p>    [&quot;Bob&quot;, 28, 1.80],<\/p>\n<p>    [&quot;Charlie&quot;, 25, 1.65]<\/p>\n<p>]<\/p>\n<p>table = PrettyTable()<\/p>\n<p>table.field_names = data[0]<\/p>\n<p>for row in data[1:]:<\/p>\n<p>    table.add_row(row)<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528prettytable\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u8868\u683c\u5bf9\u8c61\uff0c\u5e76\u5c06\u6570\u636e\u8f93\u51fa\u4e3a\u6f02\u4eae\u7684\u8868\u683c\u683c\u5f0f\u3002prettytable\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u5316\u9009\u9879\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u8868\u683c\u8f93\u51fa\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u8f93\u51fa\u6570\u636e\u6253\u5370\u51fa\u6765\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528print()\u51fd\u6570\uff0c\u5b83\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5927\u591a\u6570\u8f93\u51fa\u9700\u6c42\u3002\u5bf9\u4e8e\u9700\u8981\u5b9a\u5236\u8f93\u51fa\u683c\u5f0f\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u8f93\u51fa\u65b9\u6cd5\uff0c\u4f8b\u5982\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-strings\uff09\u548cformat()\u65b9\u6cd5\u3002\u5982\u679c\u9700\u8981\u5c06\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528write()\u3001writelines()\u65b9\u6cd5\u6216csv\u6a21\u5757\u3002\u5bf9\u4e8e\u8bb0\u5f55\u65e5\u5fd7\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528logging\u6a21\u5757\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u4f8b\u5982pandas\u3001numpy\u548cprettytable\u7b49\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u8f93\u51fa\u6570\u636e\u7684\u683c\u5f0f\u548c\u4fdd\u5b58\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u8f93\u51fa\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open<\/code>\u51fd\u6570\u7ed3\u5408\u6587\u4ef6\u5199\u5165\u6a21\u5f0f\u6765\u5c06\u8f93\u51fa\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u6253\u5f00\u6587\u4ef6\uff08\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u4f1a\u521b\u5efa\uff09\uff0c\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\uff0c\u6700\u540e\u8bb0\u5f97\u5173\u95ed\u6587\u4ef6\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u4fdd\u5b58\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;) as file:\n    file.write(&#39;\u8fd9\u662f\u8981\u4fdd\u5b58\u7684\u8f93\u51fa\u6570\u636e&#39;)\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6253\u5370\u8f93\u51fa\u5230\u63a7\u5236\u53f0\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u6253\u5370\u8f93\u51fa\u65b9\u6cd5\u662f\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u5e76\u652f\u6301\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528f-string\u3001<code>str.format()<\/code>\u65b9\u6cd5\u6216\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3002\u4e0b\u9762\u662f\u51e0\u4e2a\u4f8b\u5b50\uff1a  <\/p>\n<pre><code class=\"language-python\"># \u4f7f\u7528 f-string\nname = &quot;Alice&quot;\nprint(f&quot;Hello, {name}!&quot;)\n\n# \u4f7f\u7528 str.format()\nprint(&quot;Hello, {}!&quot;.format(name))\n\n# \u4f7f\u7528\u767e\u5206\u53f7\u683c\u5f0f\u5316\nprint(&quot;Hello, %s!&quot; % name)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6570\u636e\u7684\u5b9e\u65f6\u8f93\u51fa\uff1f<\/strong><br \/>\u8981\u5b9e\u73b0\u6570\u636e\u7684\u5b9e\u65f6\u8f93\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c<code>print()<\/code>\u51fd\u6570\u7ed3\u5408<code>flush=True<\/code>\u53c2\u6570\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6bcf\u6b21\u8f93\u51fa\u540e\u7acb\u5373\u5237\u65b0\u5230\u63a7\u5236\u53f0\uff0c\u800c\u4e0d\u662f\u7f13\u51b2\u533a\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import time\n\nfor i in range(5):\n    print(f&quot;\u5b9e\u65f6\u8f93\u51fa: {i}&quot;, flush=True)\n    time.sleep(1)  # \u6682\u505c\u4e00\u79d2\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u4e8e\u9700\u8981\u5b9e\u65f6\u76d1\u63a7\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528print\u51fd\u6570\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u8f93\u51fa\u3001\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6 \u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u8f93\u51fa\u6570\u636e\u6253\u5370\u51fa\u6765\u3002\u6700\u5e38\u89c1 [&hellip;]","protected":false},"author":3,"featured_media":1058116,"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\/1058109"}],"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=1058109"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058109\/revisions"}],"predecessor-version":[{"id":1058117,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058109\/revisions\/1058117"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1058116"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1058109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1058109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1058109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}