{"id":974042,"date":"2024-12-27T06:01:55","date_gmt":"2024-12-26T22:01:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/974042.html"},"modified":"2024-12-27T06:01:57","modified_gmt":"2024-12-26T22:01:57","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%87%ba%e5%a4%9a%e7%bb%b4%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/974042.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u51fa\u591a\u7ef4\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195933\/21b0bfd3-37ab-4a95-bf59-0a8db71c1e52.webp\" alt=\"python\u5982\u4f55\u5bfc\u51fa\u591a\u7ef4\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5bfc\u51fa\u591a\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528numpy\u5e93\u3001pandas\u5e93\u3001JSON\u683c\u5f0f\u3001pickle\u6a21\u5757\u7b49\u3002\u63a8\u8350\u4f7f\u7528numpy\u5e93\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u591a\u7ef4\u6570\u7ec4\u5904\u7406\u529f\u80fd\uff0c\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u5bfc\u51fa\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528numpy\u5e93\u5bfc\u51fa\u591a\u7ef4\u6570\u7ec4\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001NUMPY\u5e93\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>Numpy\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u4e4b\u4e00\uff0c\u7279\u522b\u9002\u5408\u4e8e\u591a\u7ef4\u6570\u7ec4\u7684\u64cd\u4f5c\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u5bfc\u51fa\u6570\u7ec4\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u548c\u4fdd\u5b58\u591a\u7ef4\u6570\u7ec4<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528numpy\u521b\u5efa\u4e00\u4e2a\u591a\u7ef4\u6570\u7ec4\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])<\/p>\n<h2><strong>\u4fdd\u5b58\u6570\u7ec4\u5230\u6587\u4ef6<\/strong><\/h2>\n<p>np.save(&#39;array.npy&#39;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a3D\u7684numpy\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528<code>np.save<\/code>\u51fd\u6570\u5c06\u5176\u4fdd\u5b58\u4e3a\u4e00\u4e2a.npy\u6587\u4ef6\u3002\u8fd9\u79cd\u683c\u5f0f\u4e13\u95e8\u7528\u4e8e\u5b58\u50a8numpy\u6570\u7ec4\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u52a0\u8f7d\u548c\u5bfc\u51fa\u6570\u7ec4<\/li>\n<\/ol>\n<p><p>\u5f53\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4fdd\u5b58\u7684\u6570\u7ec4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>np.load<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7d\u6570\u7ec4<\/p>\n<p>loaded_array = np.load(&#39;array.npy&#39;)<\/p>\n<p>print(loaded_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bfc\u5165\u548c\u5bfc\u51fa\u591a\u7ef4\u6570\u7ec4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001CSV\u683c\u5f0f\u7684\u5bfc\u51fa<\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1.npy\u6587\u4ef6\u683c\u5f0f\u975e\u5e38\u9ad8\u6548\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u591a\u7ef4\u6570\u7ec4\u5bfc\u51fa\u4e3a\u4eba\u7c7b\u53ef\u8bfb\u7684\u683c\u5f0f\uff0c\u6bd4\u5982CSV\u3002\u5bf9\u4e8e\u4e8c\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u7684<code>savetxt<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5c06\u4e8c\u7ef4\u6570\u7ec4\u4fdd\u5b58\u4e3aCSV<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array_2d = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>np.savetxt(&#39;array.csv&#39;, array_2d, delimiter=&#39;,&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5c06\u4e09\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3aCSV<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u4e09\u7ef4\u6570\u7ec4\uff0c\u9700\u8981\u5c06\u5176\u91cd\u5851\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u6216\u5c06\u5176\u5206\u5757\u4fdd\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u4e09\u7ef4\u6570\u7ec4\u5c55\u5f00\u4e3a\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array_3d_flattened = array.reshape(-1, array.shape[-1])<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>np.savetxt(&#39;array_3d.csv&#39;, array_3d_flattened, delimiter=&#39;,&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001PANDAS\u5e93\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>Pandas\u5e93\u4e5f\u53ef\u4ee5\u5904\u7406\u591a\u7ef4\u6570\u7ec4\uff0c\u5c24\u5176\u662f\u5f53\u6211\u4eec\u60f3\u8981\u5bfc\u51fa\u4e3aCSV\u6216Excel\u683c\u5f0f\u65f6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528DataFrame\u4fdd\u5b58\u4e8c\u7ef4\u6570\u7ec4<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(array_2d)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;array_pandas.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Excel\u683c\u5f0f\u5bfc\u51fa<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u4e3aExcel\u6587\u4ef6<\/p>\n<p>df.to_excel(&#39;array.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001JSON\u683c\u5f0f\u7684\u5bfc\u51fa<\/p>\n<\/p>\n<p><p>JSON\u683c\u5f0f\u662f\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u9002\u5408\u5b58\u50a8\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528Python\u5185\u7f6e\u7684json\u6a21\u5757<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5c06numpy\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>array_list = array.tolist()<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aJSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;array.json&#39;, &#39;w&#39;) as f:<\/p>\n<p>    json.dump(array_list, f)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8bfb\u53d6JSON\u6587\u4ef6<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u4eceJSON\u6587\u4ef6\u52a0\u8f7d\u6570\u636e<\/p>\n<p>with open(&#39;array.json&#39;, &#39;r&#39;) as f:<\/p>\n<p>    array_from_json = np.array(json.load(f))<\/p>\n<p>print(array_from_json)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001PICKLE\u6a21\u5757\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>Pickle\u6a21\u5757\u53ef\u4ee5\u5e8f\u5217\u5316\u51e0\u4e4e\u6240\u6709\u7684Python\u5bf9\u8c61\uff0c\u5305\u62ec\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528pickle\u4fdd\u5b58\u6570\u7ec4<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<h2><strong>\u4fdd\u5b58\u6570\u7ec4\u5230pickle\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;array.pkl&#39;, &#39;wb&#39;) as f:<\/p>\n<p>    pickle.dump(array, f)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u52a0\u8f7dpickle\u6587\u4ef6<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7d\u6570\u7ec4<\/p>\n<p>with open(&#39;array.pkl&#39;, &#39;rb&#39;) as f:<\/p>\n<p>    loaded_array_pickle = pickle.load(f)<\/p>\n<p>print(loaded_array_pickle)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5bfc\u51fa\u591a\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9002\u5408\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<strong>Numpy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u5b58\u50a8\u65b9\u6cd5<\/strong>\uff0c\u800cCSV\u548cJSON\u683c\u5f0f\u9002\u5408\u6570\u636e\u4ea4\u6362\u548c\u4eba\u7c7b\u9605\u8bfb\u3002Pickle\u6a21\u5757\u5219\u9002\u7528\u4e8e\u9700\u8981\u5e8f\u5217\u5316\u590d\u6742\u5bf9\u8c61\u7684\u573a\u5408\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5bfc\u51fa\u683c\u5f0f\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u53ef\u7528\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u51fa\u591a\u7ef4\u6570\u7ec4\u4e3aCSV\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u8f7b\u677e\u5730\u5c06\u591a\u7ef4\u6570\u7ec4\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5<code>pandas<\/code>\u5e93\u3002\u4f7f\u7528<code>DataFrame<\/code>\u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u8868\u683c\u5f62\u5f0f\uff0c\u7136\u540e\u5229\u7528<code>to_csv()<\/code>\u65b9\u6cd5\u5c06\u5176\u5bfc\u51fa\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\nimport numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a\u591a\u7ef4\u6570\u7ec4\narray = np.array([[1, 2, 3], [4, 5, 6]])\n\n# \u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame\ndf = pd.DataFrame(array)\n\n# \u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\ndf.to_csv(&#39;output.csv&#39;, index=False)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u751f\u6210\u4e00\u4e2a\u540d\u4e3a<code>output.csv<\/code>\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u591a\u7ef4\u6570\u7ec4\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u591a\u7ef4\u6570\u7ec4\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\uff1f<\/strong><br \/>\u9664\u4e86CSV\u6587\u4ef6\uff0cPython\u8fd8\u5141\u8bb8\u5c06\u591a\u7ef4\u6570\u7ec4\u5bfc\u51fa\u4e3aExcel\u683c\u5f0f\u3002\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>ExcelWriter<\/code>\u529f\u80fd\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\uff0c\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u8f93\u51fa\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\nimport numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a\u591a\u7ef4\u6570\u7ec4\narray = np.array([[1, 2, 3], [4, 5, 6]])\n\n# \u8f6c\u6362\u4e3aDataFrame\ndf = pd.DataFrame(array)\n\n# \u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\nwith pd.ExcelWriter(&#39;output.xlsx&#39;) as writer:\n    df.to_excel(writer, index=False, sheet_name=&#39;Sheet1&#39;)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>output.xlsx<\/code>\u6587\u4ef6\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e9b\u5e93\u53ef\u4ee5\u9ad8\u6548\u5bfc\u51fa\u591a\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u51e0\u4e2a\u5e93\u53ef\u4ee5\u7528\u4e8e\u5bfc\u51fa\u591a\u7ef4\u6570\u7ec4\u3002\u9664\u4e86<code>pandas<\/code>\uff0c<code>NumPy<\/code>\u5e93\u4e5f\u63d0\u4f9b\u4e86<code>np.savetxt()<\/code>\u65b9\u6cd5\uff0c\u9002\u5408\u5bfc\u51fa\u4e8c\u7ef4\u6570\u7ec4\u4e3a\u6587\u672c\u6587\u4ef6\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c<code>h5py<\/code>\u5e93\u53ef\u7528\u4e8e\u5c06\u591a\u7ef4\u6570\u7ec4\u5bfc\u51fa\u4e3aHDF5\u683c\u5f0f\uff0c\u9002\u5408\u5b58\u50a8\u5927\u89c4\u6a21\u6570\u636e\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u6587\u4ef6\u683c\u5f0f\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u89c4\u6a21\u548c\u540e\u7eed\u4f7f\u7528\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5bfc\u51fa\u591a\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528numpy\u5e93\u3001pandas\u5e93\u3001JSON\u683c\u5f0f\u3001pickle\u6a21\u5757\u7b49\u3002\u63a8 [&hellip;]","protected":false},"author":3,"featured_media":974051,"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\/974042"}],"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=974042"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974042\/revisions"}],"predecessor-version":[{"id":974055,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974042\/revisions\/974055"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/974051"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=974042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=974042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=974042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}