{"id":1089521,"date":"2025-01-08T13:54:13","date_gmt":"2025-01-08T05:54:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1089521.html"},"modified":"2025-01-08T13:54:16","modified_gmt":"2025-01-08T05:54:16","slug":"%e5%a6%82%e4%bd%95%e6%8e%a7%e5%88%b6python%e8%be%93%e5%87%batxt%e6%96%87%e4%bb%b6%e7%9a%84%e6%a0%bc%e5%bc%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1089521.html","title":{"rendered":"\u5982\u4f55\u63a7\u5236python\u8f93\u51fatxt\u6587\u4ef6\u7684\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202415\/47dd2361-3f14-456f-98ef-0232fe951c1b.webp\" alt=\"\u5982\u4f55\u63a7\u5236python\u8f93\u51fatxt\u6587\u4ef6\u7684\u683c\u5f0f\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u63a7\u5236\u8f93\u51fatxt\u6587\u4ef6\u7684\u683c\u5f0f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u8f6c\u4e49\u5b57\u7b26\u3001CSV\u6a21\u5757\u7b49\u3002\u5176\u4e2d\uff0c<strong>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u3001\u4f7f\u7528CSV\u6a21\u5757\u3001\u4f7f\u7528pandas\u5e93<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u63a7\u5236\u8f93\u51fatxt\u6587\u4ef6\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662fPython\u4e2d\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u5de5\u5177\u3002\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u63a7\u5236\u8f93\u51fa\u5185\u5bb9\u7684\u5e03\u5c40\u548c\u6837\u5f0f\u3002Python\u63d0\u4f9b\u4e86\u51e0\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u767e\u5206\u53f7\uff08%\uff09\u683c\u5f0f\u5316\u3001str.format()\u65b9\u6cd5\u4ee5\u53caf-strings\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u503c\uff09\u3002<\/p>\n<\/p>\n<p><h4>1. \u767e\u5206\u53f7\uff08%\uff09\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7c7b\u4f3c\u4e8eC\u8bed\u8a00\u4e2d\u7684printf\u8bed\u53e5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [(&#39;Alice&#39;, 25, &#39;Engineer&#39;), (&#39;Bob&#39;, 30, &#39;Doctor&#39;), (&#39;Charlie&#39;, 22, &#39;Artist&#39;)]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for name, age, profession in data:<\/p>\n<p>        file.write(&#39;%-10s %-10d %-10s\\n&#39; % (name, age, profession))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c%-10s\u8868\u793a\u5b57\u7b26\u4e32\u5de6\u5bf9\u9f50\uff0c\u5360\u752810\u4e2a\u5b57\u7b26\u5bbd\u5ea6\uff1b%-10d\u8868\u793a\u6574\u6570\u5de6\u5bf9\u9f50\uff0c\u5360\u752810\u4e2a\u5b57\u7b26\u5bbd\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>2. str.format()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u66f4\u7075\u6d3b\uff0c\u5141\u8bb8\u4f7f\u7528\u547d\u540d\u53c2\u6570\u548c\u66f4\u590d\u6742\u7684\u5e03\u5c40\u63a7\u5236\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [(&#39;Alice&#39;, 25, &#39;Engineer&#39;), (&#39;Bob&#39;, 30, &#39;Doctor&#39;), (&#39;Charlie&#39;, 22, &#39;Artist&#39;)]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for name, age, profession in data:<\/p>\n<p>        file.write(&#39;{:&lt;10} {:&lt;10} {:&lt;10}\\n&#39;.format(name, age, profession))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c{:&lt;10}\u8868\u793a\u5de6\u5bf9\u9f50\uff0c\u5360\u752810\u4e2a\u5b57\u7b26\u5bbd\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>3. f-strings\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u503c\uff09<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u662fPython 3.6\u5f15\u5165\u7684\uff0c\u4f7f\u7528\u8d77\u6765\u66f4\u52a0\u7b80\u6d01\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [(&#39;Alice&#39;, 25, &#39;Engineer&#39;), (&#39;Bob&#39;, 30, &#39;Doctor&#39;), (&#39;Charlie&#39;, 22, &#39;Artist&#39;)]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for name, age, profession in data:<\/p>\n<p>        file.write(f&#39;{name:&lt;10} {age:&lt;10} {profession:&lt;10}\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c{name:&lt;10}\u8868\u793a\u5de6\u5bf9\u9f50\uff0c\u5360\u752810\u4e2a\u5b57\u7b26\u5bbd\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u8f6c\u4e49\u5b57\u7b26\u53ef\u4ee5\u7528\u6765\u63a7\u5236\u8f93\u51fa\u6587\u4ef6\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\u6362\u884c\u7b26\uff08\\n\uff09\u3001\u5236\u8868\u7b26\uff08\\t\uff09\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [(&#39;Alice&#39;, 25, &#39;Engineer&#39;), (&#39;Bob&#39;, 30, &#39;Doctor&#39;), (&#39;Charlie&#39;, 22, &#39;Artist&#39;)]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for name, age, profession in data:<\/p>\n<p>        file.write(f&#39;{name}\\t{age}\\t{profession}\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\\t\u8868\u793a\u5236\u8868\u7b26\uff0c\u7528\u4e8e\u5bf9\u9f50\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528CSV\u6a21\u5757<\/h3>\n<\/p>\n<p><p>CSV\uff08Comma Separated Values\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\uff0cPython\u7684csv\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406CSV\u6587\u4ef6\u3002\u867d\u7136CSV\u6587\u4ef6\u901a\u5e38\u7528\u4e8e\u5b58\u50a8\u8868\u683c\u6570\u636e\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u63a7\u5236txt\u6587\u4ef6\u7684\u683c\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [(&#39;Alice&#39;, 25, &#39;Engineer&#39;), (&#39;Bob&#39;, 30, &#39;Doctor&#39;), (&#39;Charlie&#39;, 22, &#39;Artist&#39;)]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file, delimiter=&#39;\\t&#39;)<\/p>\n<p>    writer.writerow([&#39;Name&#39;, &#39;Age&#39;, &#39;Profession&#39;])<\/p>\n<p>    writer.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cdelimiter=&#39;\\t&#39;\u8868\u793a\u4f7f\u7528\u5236\u8868\u7b26\u4f5c\u4e3a\u5217\u7684\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>pandas\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u901a\u5e38\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06DataFrame\u5bf9\u8c61\u5bfc\u51fa\u4e3a\u683c\u5f0f\u5316\u7684txt\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;Age&#39;: [25, 30, 22], &#39;Profession&#39;: [&#39;Engineer&#39;, &#39;Doctor&#39;, &#39;Artist&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df.to_csv(&#39;output.txt&#39;, sep=&#39;\\t&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0csep=&#39;\\t&#39;\u8868\u793a\u4f7f\u7528\u5236\u8868\u7b26\u4f5c\u4e3a\u5217\u7684\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u63a7\u5236Python\u8f93\u51fatxt\u6587\u4ef6\u7684\u683c\u5f0f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec<strong>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u3001\u4f7f\u7528CSV\u6a21\u5757\u3001\u4f7f\u7528pandas\u5e93<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u6280\u5de7\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u7075\u6d3b\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u8f93\u51fa\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u8f93\u51fa\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u6307\u5b9a<code>encoding=&#39;utf-8&#39;<\/code>\u6765\u786e\u4fdd\u6587\u672c\u6587\u4ef6\u4ee5UTF-8\u7f16\u7801\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;, encoding=&#39;utf-8&#39;) as file:\n    file.write(&quot;\u8fd9\u662f\u4e00\u4e9b\u793a\u4f8b\u6587\u672c\u3002&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u7684\u5b57\u7b26\u7f16\u7801\u4e0e\u9884\u671f\u4e00\u81f4\uff0c\u907f\u514d\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u63a7\u5236\u6587\u672c\u6587\u4ef6\u7684\u884c\u95f4\u8ddd\u548c\u7f29\u8fdb\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u5199\u5165\u6587\u672c\u65f6\u52a0\u5165\u989d\u5916\u7684\u6362\u884c\u7b26\u6216\u7a7a\u683c\u6765\u63a7\u5236\u884c\u95f4\u8ddd\u548c\u7f29\u8fdb\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>\\n<\/code>\u53ef\u4ee5\u589e\u52a0\u884c\u95f4\u8ddd\uff0c\u800c\u4f7f\u7528\u5236\u8868\u7b26<code>\\t<\/code>\u6216\u7a7a\u683c\u53ef\u4ee5\u5b9e\u73b0\u7f29\u8fdb\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:\n    file.write(&quot;\u7b2c\u4e00\u884c\u6587\u672c\u3002\\n\\n&quot;)  # \u589e\u52a0\u884c\u95f4\u8ddd\n    file.write(&quot;\\t\u7b2c\u4e8c\u884c\u6587\u672c\u3002\\n&quot;)  # \u6dfb\u52a0\u7f29\u8fdb\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6587\u4ef6\u7684\u683c\u5f0f\uff0c\u8ba9\u5176\u66f4\u7b26\u5408\u60a8\u7684\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u683c\u5f0f\u5316\u8f93\u51fa\u7684\u6587\u672c\u5185\u5bb9\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08\u5982f-string\u6216<code>str.format()<\/code>\uff09\u6765\u63a7\u5236\u8f93\u51fa\u7684\u6587\u672c\u5185\u5bb9\u683c\u5f0f\u3002\u8fd9\u8ba9\u60a8\u53ef\u4ee5\u8f7b\u677e\u63d2\u5165\u53d8\u91cf\u5e76\u8bbe\u5b9a\u683c\u5f0f\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">name = &quot;Alice&quot;\nage = 30\nwith open(&#39;output.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:\n    file.write(f&quot;{name} \u7684\u5e74\u9f84\u662f {age} \u5c81\u3002\\n&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u8ba9\u6587\u672c\u66f4\u6613\u8bfb\uff0c\u540c\u65f6\u4e5f\u80fd\u786e\u4fdd\u6570\u636e\u7684\u6574\u9f50\u6392\u5217\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u63a7\u5236\u8f93\u51fatxt\u6587\u4ef6\u7684\u683c\u5f0f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u8f6c\u4e49\u5b57\u7b26\u3001CSV [&hellip;]","protected":false},"author":3,"featured_media":1089530,"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\/1089521"}],"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=1089521"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089521\/revisions"}],"predecessor-version":[{"id":1089531,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089521\/revisions\/1089531"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1089530"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1089521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1089521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1089521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}