{"id":1117879,"date":"2025-01-08T18:30:05","date_gmt":"2025-01-08T10:30:05","guid":{"rendered":""},"modified":"2025-01-08T18:30:08","modified_gmt":"2025-01-08T10:30:08","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python%e8%bf%90%e8%a1%8c%e7%bb%93%e6%9e%9c%e4%bf%9d%e5%ad%98%e6%88%90txt-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1117879.html","title":{"rendered":"\u5982\u4f55\u5c06python\u8fd0\u884c\u7ed3\u679c\u4fdd\u5b58\u6210txt"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081607\/07388dd5-b1bd-4677-9139-baa23f6a9fb7.webp\" alt=\"\u5982\u4f55\u5c06python\u8fd0\u884c\u7ed3\u679c\u4fdd\u5b58\u6210txt\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5c06\u8fd0\u884c\u7ed3\u679c\u4fdd\u5b58\u6210txt\u6587\u4ef6\u662f\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\u3002<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570open()\u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2atxt\u6587\u4ef6\u3001\u4f7f\u7528write()\u65b9\u6cd5\u5199\u5165\u6570\u636e\u3001\u4f7f\u7528with\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528with\u8bed\u53e5\u662f\u8f83\u4e3a\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u80fd\u786e\u4fdd\u6587\u4ef6\u6b63\u786e\u5173\u95ed\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u5b9e\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528open()\u548cwrite()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684open()\u51fd\u6570\u7528\u4e8e\u6587\u4ef6\u64cd\u4f5c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u6b64\u51fd\u6570\u4ee5\u5199\u6a21\u5f0f\u6253\u5f00\u4e00\u4e2atxt\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528write()\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u6700\u540e\uff0c\u522b\u5fd8\u4e86\u8c03\u7528close()\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\u4ee5\u786e\u4fdd\u6570\u636e\u6b63\u786e\u5199\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>result = &quot;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u8fd0\u884c\u7ed3\u679c&quot;<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6\u7528\u4e8e\u5199\u5165<\/strong><\/h2>\n<p>file = open(&quot;result.txt&quot;, &quot;w&quot;)<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e<\/strong><\/h2>\n<p>file.write(result)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u4ef6<\/strong><\/h2>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>open(&quot;result.txt&quot;, &quot;w&quot;)<\/code>\u7528\u4e8e\u521b\u5efa\u6216\u6253\u5f00\u4e00\u4e2a\u540d\u4e3aresult.txt\u7684\u6587\u4ef6\uff0c\u6a21\u5f0f\u4e3a\u5199\u5165\u6a21\u5f0f\uff08&quot;w&quot;\uff09\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>file.write(result)<\/code>\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\uff0c\u6700\u540e\u901a\u8fc7<code>file.close()<\/code>\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528with\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90<\/h3>\n<\/p>\n<p><p><strong>with\u8bed\u53e5\u662f\u63a8\u8350\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u5f0f<\/strong>\uff0c\u56e0\u4e3a\u5b83\u80fd\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90\uff0c\u786e\u4fdd\u5728\u64cd\u4f5c\u7ed3\u675f\u540e\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u5373\u4f7f\u5728\u53d1\u751f\u5f02\u5e38\u65f6\u4e5f\u4e0d\u4f8b\u5916\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u5b89\u5168\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>result = &quot;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u8fd0\u884c\u7ed3\u679c&quot;<\/p>\n<h2><strong>\u4f7f\u7528with\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90<\/strong><\/h2>\n<p>with open(&quot;result.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>with open(&quot;result.txt&quot;, &quot;w&quot;) as file:<\/code>\u7528\u4e8e\u521b\u5efa\u6216\u6253\u5f00\u4e00\u4e2a\u540d\u4e3aresult.txt\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u5bf9\u8c61\u8d4b\u503c\u7ed9\u53d8\u91cffile\u3002with\u8bed\u53e5\u5757\u7ed3\u675f\u540e\uff0c\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\uff0c\u65e0\u9700\u663e\u5f0f\u8c03\u7528close()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5c06\u591a\u884c\u7ed3\u679c\u4fdd\u5b58\u5230txt\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u8fd0\u884c\u7ed3\u679c\u5305\u542b\u591a\u884c\u6570\u636e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528write()\u65b9\u6cd5\u9010\u884c\u5199\u5165\uff0c\u6216\u8005\u4f7f\u7528writelines()\u65b9\u6cd5\u4e00\u6b21\u6027\u5199\u5165\u591a\u884c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>results = [&quot;\u7b2c\u4e00\u884c\u7ed3\u679c\\n&quot;, &quot;\u7b2c\u4e8c\u884c\u7ed3\u679c\\n&quot;, &quot;\u7b2c\u4e09\u884c\u7ed3\u679c\\n&quot;]<\/p>\n<h2><strong>\u4f7f\u7528with\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90<\/strong><\/h2>\n<p>with open(&quot;results.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.writelines(results)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>results<\/code>\u662f\u4e00\u4e2a\u5305\u542b\u591a\u884c\u5b57\u7b26\u4e32\u7684\u5217\u8868\uff0c\u4f7f\u7528writelines()\u65b9\u6cd5\u5c06\u5176\u4e00\u6b21\u6027\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5c06\u590d\u6742\u6570\u636e\u7ed3\u6784\u4fdd\u5b58\u5230txt\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u6216\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528json\u6a21\u5757\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u7136\u540e\u5199\u5165txt\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>results = {<\/p>\n<p>    &quot;\u7b2c\u4e00\u884c&quot;: &quot;\u7b2c\u4e00\u884c\u7ed3\u679c&quot;,<\/p>\n<p>    &quot;\u7b2c\u4e8c\u884c&quot;: &quot;\u7b2c\u4e8c\u884c\u7ed3\u679c&quot;,<\/p>\n<p>    &quot;\u7b2c\u4e09\u884c&quot;: &quot;\u7b2c\u4e09\u884c\u7ed3\u679c&quot;<\/p>\n<p>}<\/p>\n<h2><strong>\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f<\/strong><\/h2>\n<p>results_str = json.dumps(results, ensure_ascii=False, indent=4)<\/p>\n<h2><strong>\u4f7f\u7528with\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90<\/strong><\/h2>\n<p>with open(&quot;results.json&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(results_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>json.dumps()<\/code>\u65b9\u6cd5\u5c06\u5b57\u5178\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u53c2\u6570<code>ensure_ascii=False<\/code>\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u4e0d\u88ab\u8f6c\u4e49\uff0c<code>indent=4<\/code>\u4f7f\u8f93\u51fa\u66f4\u52a0\u7f8e\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757\u4fdd\u5b58\u8fd0\u884c\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u8bb0\u5f55\u8be6\u7ec6\u65e5\u5fd7\u4fe1\u606f\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684logging\u6a21\u5757\u3002<strong>logging\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u529f\u80fd\uff0c\u5982\u65f6\u95f4\u6233\u3001\u65e5\u5fd7\u7ea7\u522b\u7b49<\/strong>\uff0c\u975e\u5e38\u9002\u5408\u590d\u6742\u7684\u65e5\u5fd7\u8bb0\u5f55\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/h2>\n<p>logging.basicConfig(filename=&#39;app.log&#39;, level=logging.INFO, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>result = &quot;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u8fd0\u884c\u7ed3\u679c&quot;<\/p>\n<h2><strong>\u8bb0\u5f55\u4fe1\u606f\u7ea7\u522b\u65e5\u5fd7<\/strong><\/h2>\n<p>logging.info(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>logging.basicConfig()<\/code>\u7528\u4e8e\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\uff0c<code>filename<\/code>\u53c2\u6570\u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u540d\uff0c<code>level<\/code>\u53c2\u6570\u6307\u5b9a\u65e5\u5fd7\u7ea7\u522b\uff0c<code>format<\/code>\u53c2\u6570\u6307\u5b9a\u65e5\u5fd7\u683c\u5f0f\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>logging.info()<\/code>\u65b9\u6cd5\u8bb0\u5f55\u4fe1\u606f\u7ea7\u522b\u7684\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528pandas\u5e93\u4fdd\u5b58\u6570\u636e\u5230txt\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0cpandas\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06DataFrame\u6570\u636e\u4fdd\u5b58\u5230txt\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &quot;\u52171&quot;: [&quot;\u884c1\u6570\u636e&quot;, &quot;\u884c2\u6570\u636e&quot;, &quot;\u884c3\u6570\u636e&quot;],<\/p>\n<p>    &quot;\u52172&quot;: [&quot;\u884c1\u6570\u636e&quot;, &quot;\u884c2\u6570\u636e&quot;, &quot;\u884c3\u6570\u636e&quot;]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4fdd\u5b58DataFrame\u5230txt\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&quot;data.txt&quot;, sep=&#39;\\t&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>pd.DataFrame(data)<\/code>\u7528\u4e8e\u521b\u5efa\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c<code>df.to_csv()<\/code>\u65b9\u6cd5\u5c06DataFrame\u4fdd\u5b58\u5230txt\u6587\u4ef6\uff0c\u53c2\u6570<code>sep=&#39;\\t&#39;<\/code>\u6307\u5b9a\u5206\u9694\u7b26\u4e3a\u5236\u8868\u7b26\uff0c<code>index=False<\/code>\u5ffd\u7565\u884c\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u4fdd\u5b58\u5230txt\u6587\u4ef6\uff0c\u4f8b\u5982\u56fe\u7247\u6216\u5176\u4ed6\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>wb<\/code>\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528write()\u65b9\u6cd5\u5199\u5165\u4e8c\u8fdb\u5236\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4e8c\u8fdb\u5236\u6570\u636e<\/p>\n<p>binary_data = b&#39;\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\x00\\x10&#39;<\/p>\n<h2><strong>\u4f7f\u7528with\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90<\/strong><\/h2>\n<p>with open(&quot;binary_data.txt&quot;, &quot;wb&quot;) as file:<\/p>\n<p>    file.write(binary_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>open(&quot;binary_data.txt&quot;, &quot;wb&quot;)<\/code>\u7528\u4e8e\u4ee5\u4e8c\u8fdb\u5236\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c<code>file.write(binary_data)<\/code>\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4ece\u51fd\u6570\u6216\u811a\u672c\u4fdd\u5b58\u8fd0\u884c\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5c06\u51fd\u6570\u6216\u811a\u672c\u7684\u8fd0\u884c\u7ed3\u679c\u4fdd\u5b58\u5230txt\u6587\u4ef6\uff0c\u53ef\u4ee5\u5c06\u4fdd\u5b58\u903b\u8f91\u5c01\u88c5\u5728\u51fd\u6570\u4e2d\uff0c\u5e76\u5728\u9002\u5f53\u65f6\u673a\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def save_result_to_txt(result, filename=&quot;result.txt&quot;):<\/p>\n<p>    with open(filename, &quot;w&quot;) as file:<\/p>\n<p>        file.write(result)<\/p>\n<h2><strong>\u793a\u4f8b\u51fd\u6570<\/strong><\/h2>\n<p>def calculate():<\/p>\n<p>    result = &quot;\u8ba1\u7b97\u7ed3\u679c&quot;<\/p>\n<p>    save_result_to_txt(result)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>calculate()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>save_result_to_txt()<\/code>\u51fd\u6570\u5c01\u88c5\u4e86\u4fdd\u5b58\u903b\u8f91\uff0c<code>calculate()<\/code>\u51fd\u6570\u8c03\u7528<code>save_result_to_txt()<\/code>\u5c06\u8ba1\u7b97\u7ed3\u679c\u4fdd\u5b58\u5230txt\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u7684\u4fdd\u5b58\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u751f\u6210\u5668\uff08generator\uff09\u9010\u884c\u5199\u5165\u6587\u4ef6\uff0c\u4ee5\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_large_data():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        yield f&quot;\u7b2c{i}\u884c\u6570\u636e\\n&quot;<\/p>\n<h2><strong>\u4f7f\u7528with\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90<\/strong><\/h2>\n<p>with open(&quot;large_data.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    for line in generate_large_data():<\/p>\n<p>        file.write(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>generate_large_data()<\/code>\u662f\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\uff0c\u7528\u4e8e\u751f\u6210\u5927\u89c4\u6a21\u6570\u636e\u3002\u4f7f\u7528\u751f\u6210\u5668\u9010\u884c\u5199\u5165\u6587\u4ef6\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u5168\u90e8\u6570\u636e\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u4fdd\u5b58\u8fd0\u884c\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982numpy\u3001scipy\u7b49\uff0c\u5c06\u8fd0\u884c\u7ed3\u679c\u4fdd\u5b58\u5230txt\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528numpy\u4fdd\u5b58\u6570\u7ec4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u4fdd\u5b58\u6570\u7ec4\u5230txt\u6587\u4ef6<\/strong><\/h2>\n<p>np.savetxt(&quot;array.txt&quot;, array, fmt=&quot;%d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>np.savetxt()<\/code>\u65b9\u6cd5\u5c06numpy\u6570\u7ec4\u4fdd\u5b58\u5230txt\u6587\u4ef6\uff0c<code>fmt=&quot;%d&quot;<\/code>\u6307\u5b9a\u4fdd\u5b58\u683c\u5f0f\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06Python\u8fd0\u884c\u7ed3\u679c\u4fdd\u5b58\u6210txt\u6587\u4ef6\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u9002\u5408\u4f60\u9700\u6c42\u7684\u65b9\u6cd5\u81f3\u5173\u91cd\u8981\u3002<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570open()\u548cwrite()\u3001\u4f7f\u7528with\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90\u3001\u4f7f\u7528json\u6a21\u5757\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u3001\u4f7f\u7528logging\u6a21\u5757\u8bb0\u5f55\u8be6\u7ec6\u65e5\u5fd7\u3001\u4f7f\u7528pandas\u5e93\u5904\u7406\u6570\u636e\u5206\u6790\u4efb\u52a1\u3001\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u3001\u4ece\u51fd\u6570\u6216\u811a\u672c\u4fdd\u5b58\u7ed3\u679c\u3001\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u4fdd\u5b58\u7ed3\u679c<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5747\u80fd\u6709\u6548\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u786e\u4fdd\u6587\u4ef6\u6b63\u786e\u5173\u95ed\uff0c\u6570\u636e\u6b63\u786e\u5199\u5165\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5c06Python\u7684\u8fd0\u884c\u7ed3\u679c\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u6216\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">result = &quot;\u8fd9\u662f\u9700\u8981\u4fdd\u5b58\u7684\u5185\u5bb9&quot;\nwith open(&quot;output.txt&quot;, &quot;w&quot;) as file:\n    file.write(result)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5c06\u5b57\u7b26\u4e32\u5185\u5bb9\u5199\u5165\u540d\u4e3a<code>output.txt<\/code>\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4fdd\u5b58\u54ea\u4e9b\u7c7b\u578b\u7684\u6570\u636e\u5230txt\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u51e0\u4e4e\u6240\u6709\u7c7b\u578b\u7684\u6570\u636e\u90fd\u53ef\u4ee5\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5f62\u5f0f\u5e76\u4fdd\u5b58\u5230txt\u6587\u4ef6\u4e2d\u3002\u4f8b\u5982\uff0c\u6570\u5b57\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u90fd\u53ef\u4ee5\u901a\u8fc7\u9002\u5f53\u7684\u683c\u5f0f\u5316\u5904\u7406\u540e\u4fdd\u5b58\u3002\u5bf9\u4e8e\u5217\u8868\u6216\u5b57\u5178\uff0c\u901a\u5e38\u9700\u8981\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u50cf\u8fd9\u6837\uff1a  <\/p>\n<pre><code class=\"language-python\">data = [1, 2, 3, 4, 5]\nwith open(&quot;output.txt&quot;, &quot;w&quot;) as file:\n    file.write(str(data))\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u628a\u5217\u8868\u7684\u5185\u5bb9\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u4fdd\u5b58\u8f93\u51fa\u65f6\u8ffd\u52a0\u5185\u5bb9\u800c\u4e0d\u662f\u8986\u76d6\u6587\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5728\u6587\u4ef6\u4e2d\u8ffd\u52a0\u5185\u5bb9\u800c\u4e0d\u662f\u8986\u76d6\u5df2\u6709\u5185\u5bb9\uff0c\u53ef\u4ee5\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u4f7f\u7528<code>&quot;a&quot;<\/code>\u6a21\u5f0f\u3002\u8fd9\u5c06\u5141\u8bb8\u4f60\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u65b0\u7684\u6570\u636e\uff0c\u800c\u4e0d\u5f71\u54cd\u73b0\u6709\u5185\u5bb9\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&quot;output.txt&quot;, &quot;a&quot;) as file:\n    file.write(&quot;\\n\u8fd9\u662f\u8ffd\u52a0\u7684\u5185\u5bb9&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u65b0\u5185\u5bb9\u5c06\u88ab\u6dfb\u52a0\u5230<code>output.txt<\/code>\u7684\u672b\u5c3e\uff0c\u800c\u4e0d\u4f1a\u5220\u9664\u4e4b\u524d\u7684\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u8fd0\u884c\u7ed3\u679c\u4fdd\u5b58\u6210txt\u6587\u4ef6\u662f\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4f7f\u7528\u5185\u7f6e\u51fd\u6570open()\u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2atxt\u6587\u4ef6 [&hellip;]","protected":false},"author":3,"featured_media":1117886,"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\/1117879"}],"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=1117879"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117879\/revisions"}],"predecessor-version":[{"id":1117887,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117879\/revisions\/1117887"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1117886"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1117879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1117879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1117879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}