{"id":1105178,"date":"2025-01-08T16:28:28","date_gmt":"2025-01-08T08:28:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1105178.html"},"modified":"2025-01-08T16:28:33","modified_gmt":"2025-01-08T08:28:33","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e7%9f%a9%e9%98%b5%e4%bf%9d%e5%ad%98%e4%b8%batxt","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1105178.html","title":{"rendered":"python\u5982\u4f55\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3atxt"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070211\/a3b89c10-6b8d-4226-8935-bf47bd0a330e.webp\" alt=\"python\u5982\u4f55\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3atxt\" \/><\/p>\n<p><p> <strong>Python\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3atxt\u7684\u51e0\u79cd\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528\u7eafPython\u65b9\u5f0f\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u5bf9\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528NumPy\u5e93<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u7684\u5f3a\u5927\u5e93\u3002\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4f7f\u7528NumPy\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3atxt\u6587\u4ef6<\/strong><\/h2>\n<p>np.savetxt(&#39;matrix.txt&#39;, matrix, fmt=&#39;%d&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165NumPy\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>np.savetxt<\/code>\u51fd\u6570\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3a\u540d\u4e3a<code>matrix.txt<\/code>\u7684\u6587\u4ef6\u3002<code>fmt=&#39;%d&#39;<\/code>\u53c2\u6570\u6307\u5b9a\u4e86\u4fdd\u5b58\u7684\u683c\u5f0f\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u4f7f\u7528NumPy\u5e93\u4fdd\u5b58\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4fdd\u5b58\u77e9\u9635\u4e3atxt\u6587\u4ef6\u65f6\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u5f88\u591a\u4fbf\u5229\u3002<code>np.savetxt<\/code>\u51fd\u6570\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u8c03\u6574\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>fname<\/code>: \u6587\u4ef6\u540d\u6216\u8005\u6587\u4ef6\u5bf9\u8c61\u3002<\/li>\n<li><code>X<\/code>: \u8981\u4fdd\u5b58\u7684\u6570\u636e\uff0c\u901a\u5e38\u662f\u4e00\u4e2a\u6570\u7ec4\u6216\u8005\u77e9\u9635\u3002<\/li>\n<li><code>fmt<\/code>: \u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u683c\u5f0f\u3002\u9ed8\u8ba4\u662f&#39;%.18e&#39;\uff0c\u53ef\u4ee5\u4f7f\u7528\u4f8b\u5982&#39;%d&#39;\u8868\u793a\u6574\u6570\u683c\u5f0f\uff0c&#39;%.2f&#39;\u8868\u793a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u7684\u6d6e\u70b9\u6570\u683c\u5f0f\u3002<\/li>\n<li><code>delimiter<\/code>: \u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u5206\u9694\u7b26\u3002\u9ed8\u8ba4\u662f\u7a7a\u683c\u3002<\/li>\n<li><code>newline<\/code>: \u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u884c\u5c3e\u5b57\u7b26\u3002\u9ed8\u8ba4\u662f\u6362\u884c\u7b26\u3002<\/li>\n<li><code>header<\/code>: \u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u5199\u5165\u6587\u4ef6\u5f00\u5934\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>footer<\/code>: \u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u5199\u5165\u6587\u4ef6\u672b\u5c3e\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>comments<\/code>: \u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u6ce8\u91ca\u5b57\u7b26\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[1.11, 2.22, 3.33], [4.44, 5.55, 6.66], [7.77, 8.88, 9.99]])<\/p>\n<h2><strong>\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\uff0c\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\uff0c\u5e76\u4ee5\u9017\u53f7\u5206\u9694<\/strong><\/h2>\n<p>np.savetxt(&#39;matrix_formatted.txt&#39;, matrix, fmt=&#39;%.2f&#39;, delimiter=&#39;,&#39;, header=&#39;Matrix Data&#39;, comments=&#39;# &#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u6d6e\u70b9\u6570\u7684\u77e9\u9635\uff0c\u5e76\u4f7f\u7528<code>np.savetxt<\/code>\u51fd\u6570\u5c06\u5176\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\u3002\u6211\u4eec\u6307\u5b9a\u4e86\u683c\u5f0f\u4e3a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u7684\u6d6e\u70b9\u6570\uff0c\u5e76\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6ce8\u91ca\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528pandas\u5e93<\/p>\n<\/p>\n<p><p>pandas\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\u3002\u5b83\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06DataFrame\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u5c06DataFrame\u4fdd\u5b58\u4e3atxt\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;matrix_pandas.txt&#39;, sep=&#39; &#39;, header=False, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165pandas\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2aDataFrame\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>to_csv<\/code>\u65b9\u6cd5\u5c06DataFrame\u4fdd\u5b58\u4e3a\u540d\u4e3a<code>matrix_pandas.txt<\/code>\u7684\u6587\u4ef6\u3002<code>sep=&#39; &#39;<\/code>\u53c2\u6570\u6307\u5b9a\u4e86\u5206\u9694\u7b26\u4e3a\u7a7a\u683c\uff0c<code>header=False<\/code>\u548c<code>index=False<\/code>\u53c2\u6570\u5206\u522b\u6307\u5b9a\u4e0d\u4fdd\u5b58\u5217\u540d\u548c\u884c\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7eafPython\u65b9\u5f0f<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5e93\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7eafPython\u65b9\u5f0f\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u4e0d\u60f3\u4f9d\u8d56\u5916\u90e8\u5e93\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u77e9\u9635<\/p>\n<p>matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3atxt\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;matrix_pure_python.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for row in matrix:<\/p>\n<p>        file.write(&#39; &#39;.join(map(str, row)) + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u77e9\u9635\uff0c\u5e76\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u5c06\u5176\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\u3002\u6211\u4eec\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c<code>write<\/code>\u65b9\u6cd5\u5199\u5165\u6570\u636e\uff0c<code>join<\/code>\u65b9\u6cd5\u5c06\u6bcf\u4e00\u884c\u7684\u5143\u7d20\u8fde\u63a5\u6210\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528\u6362\u884c\u7b26\u5206\u9694\u6bcf\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4e09\u79cd\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528\u7eafPython\u65b9\u5f0f\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u8bfb\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>NumPy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u5904\u7406\u529f\u80fd\uff0c\u5e76\u4e14\u53ef\u4ee5\u65b9\u4fbf\u5730\u6307\u5b9a\u4fdd\u5b58\u683c\u5f0f\u548c\u5206\u9694\u7b26<\/strong>\u3002<strong>pandas\u5e93\u66f4\u9002\u5408\u5904\u7406\u6570\u636e\u6846\uff0c\u5e76\u4e14\u53ef\u4ee5\u76f4\u63a5\u5c06DataFrame\u4fdd\u5b58\u4e3atxt\u6587\u4ef6<\/strong>\u3002<strong>\u7eafPython\u65b9\u5f0f\u5219\u9002\u7528\u4e8e\u4e0d\u60f3\u4f9d\u8d56\u5916\u90e8\u5e93\u7684\u60c5\u51b5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bfb\u8005\u5e94\u8be5\u80fd\u591f\u638c\u63e1\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5e76\u80fd\u591f\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u4fdd\u5b58\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u4fdd\u5b58\u77e9\u9635\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u4fdd\u5b58\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5982\u5217\u8868\u548c\u591a\u7ef4\u6570\u7ec4\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u8bfb\u8005\u6709\u6240\u5e2e\u52a9\uff0c\u5e76\u80fd\u591f\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u4fdd\u5b58\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u7684<code>savetxt<\/code>\u51fd\u6570\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u521b\u5efa\u77e9\u9635\u540e\uff0c\u8c03\u7528<code>savetxt<\/code>\u51fd\u6570\u5e76\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u77e9\u9635\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u77e9\u9635\nmatrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n\n# \u4fdd\u5b58\u4e3atxt\u6587\u4ef6\nnp.savetxt(&#39;matrix.txt&#39;, matrix)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3a\u540d\u4e3a<code>matrix.txt<\/code>\u7684\u6587\u672c\u6587\u4ef6\uff0c\u9ed8\u8ba4\u4ee5\u7a7a\u683c\u5206\u9694\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3a\u5176\u4ed6\u683c\u5f0f\u5417\uff1f<\/strong><br \/>\u9664\u4e86\u6587\u672c\u6587\u4ef6\uff0cNumPy\u8fd8\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u4fdd\u5b58\uff0c\u4f8b\u5982<code>.npy<\/code>\u6216<code>.csv<\/code>\u6587\u4ef6\u3002\u5982\u679c\u9700\u8981\u4ee5CSV\u683c\u5f0f\u4fdd\u5b58\uff0c\u53ef\u4ee5\u4f7f\u7528<code>np.savetxt<\/code>\u5e76\u6307\u5b9a\u5206\u9694\u7b26\u4e3a\u9017\u53f7\uff1a<\/p>\n<pre><code class=\"language-python\">np.savetxt(&#39;matrix.csv&#39;, matrix, delimiter=&#39;,&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u5730\u4e0e\u5176\u4ed6\u6570\u636e\u5904\u7406\u8f6f\u4ef6\u517c\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bfb\u53d6\u4fdd\u5b58\u7684\u77e9\u9635\u6587\u4ef6\uff1f<\/strong><br \/>\u8bfb\u53d6\u4fdd\u5b58\u7684\u77e9\u9635\u6587\u4ef6\u540c\u6837\u7b80\u5355\u3002\u5bf9\u4e8e\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>np.loadtxt<\/code>\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-python\">loaded_matrix = np.loadtxt(&#39;matrix.txt&#39;)\n<\/code><\/pre>\n<p>\u5982\u679c\u662fCSV\u683c\u5f0f\uff0c\u4f7f\u7528\u7684\u65b9\u5f0f\u4e5f\u662f\u76f8\u540c\u7684\u3002\u8bfb\u53d6\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u76f4\u63a5\u5728Python\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u77e9\u9635\u8fdb\u884c\u8ba1\u7b97\u6216\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3atxt\u7684\u51e0\u79cd\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528\u7eafPython\u65b9\u5f0f\u3002\u672c\u6587 [&hellip;]","protected":false},"author":3,"featured_media":1105189,"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\/1105178"}],"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=1105178"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105178\/revisions"}],"predecessor-version":[{"id":1105192,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105178\/revisions\/1105192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1105189"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1105178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1105178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1105178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}