{"id":1108350,"date":"2025-01-08T16:59:21","date_gmt":"2025-01-08T08:59:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108350.html"},"modified":"2025-01-08T16:59:28","modified_gmt":"2025-01-08T08:59:28","slug":"python-%e5%a6%82%e4%bd%95%e5%b0%86%e7%9f%a9%e9%98%b5%e8%bd%ac%e5%8c%96%e4%b8%bacsv%e8%be%93%e5%87%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108350.html","title":{"rendered":"python \u5982\u4f55\u5c06\u77e9\u9635\u8f6c\u5316\u4e3aCSV\u8f93\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072035\/3742ed24-e695-42bc-88bd-bf9f0d4c5045.webp\" alt=\"python \u5982\u4f55\u5c06\u77e9\u9635\u8f6c\u5316\u4e3aCSV\u8f93\u51fa\" \/><\/p>\n<p><p> <strong>Python\u5c06\u77e9\u9635\u8f6c\u5316\u4e3aCSV\u8f93\u51fa\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001csv\u6a21\u5757\u3001numpy\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u6700\u4fbf\u6377\u7684\u65b9\u6cd5\u662f\u4f7f\u7528pandas\u5e93\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528pandas\u5e93\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>pandas\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f88\u591a\u65b9\u4fbf\u7684\u51fd\u6570\u6765\u5904\u7406\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pandas\u7684<code>DataFrame<\/code>\u5bf9\u8c61\u548c<code>to_csv<\/code>\u65b9\u6cd5\u6765\u8f7b\u677e\u5730\u5c06\u77e9\u9635\u8f6c\u6362\u4e3aCSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5pandas\u5e93\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5\u5b83\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5c06\u77e9\u9635\u8f6c\u6362\u4e3aDataFrame<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u77e9\u9635\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3apandas\u7684DataFrame\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u77e9\u9635<\/strong><\/h2>\n<p>matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u5c06\u77e9\u9635\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5c06DataFrame\u5199\u5165CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>to_csv<\/code>\u65b9\u6cd5\u5c06DataFrame\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06DataFrame\u5199\u5165CSV\u6587\u4ef6<\/p>\n<p>df.to_csv(&#39;output.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>index=False<\/code>\u53c2\u6570\u8868\u793a\u4e0d\u5199\u5165\u884c\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528csv\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u77e9\u9635\u5230CSV\u6587\u4ef6\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165csv\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5c06\u77e9\u9635\u5199\u5165CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>csv.writer<\/code>\u5bf9\u8c61\u5c06\u77e9\u9635\u9010\u884c\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u77e9\u9635<\/p>\n<p>matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u5199\u5165CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerows(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>newline=&#39;&#39;<\/code>\u53c2\u6570\u7528\u4e8e\u907f\u514d\u5728Windows\u5e73\u53f0\u4e0a\u5199\u5165\u989d\u5916\u7684\u7a7a\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u77e9\u9635\u662f\u901a\u8fc7numpy\u521b\u5efa\u7684\uff0cnumpy\u5e93\u4e5f\u63d0\u4f9b\u4e86\u76f4\u63a5\u5c06\u6570\u7ec4\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5numpy\u5e93<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5numpy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5c06\u77e9\u9635\u8f6c\u6362\u4e3anumpy\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u77e9\u9635\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3anumpy\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5c06numpy\u6570\u7ec4\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.savetxt<\/code>\u65b9\u6cd5\u5c06\u6570\u7ec4\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06numpy\u6570\u7ec4\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6<\/p>\n<p>np.savetxt(&#39;output.csv&#39;, matrix, delimiter=&#39;,&#39;, fmt=&#39;%d&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>delimiter=&#39;,&#39;<\/code>\u53c2\u6570\u8868\u793a\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c<code>fmt=&#39;%d&#39;<\/code>\u53c2\u6570\u8868\u793a\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c<strong>\u4f7f\u7528pandas\u5e93\u662f\u6700\u7b80\u4fbf\u7684\u65b9\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f88\u591a\u9ad8\u7ea7\u7684\u529f\u80fd\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002\u5982\u679c\u4f60\u4e0d\u60f3\u5b89\u88c5\u989d\u5916\u7684\u5e93\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u3002\u5bf9\u4e8e\u5904\u7406\u5927\u91cf\u6570\u503c\u6570\u636e\uff0c<strong>numpy\u5e93\u4e5f\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9<\/strong>\u3002\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5c06\u4f7f\u4f60\u7684\u5de5\u4f5c\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684<code>pandas<\/code>\u5e93\u6216<code>csv<\/code>\u6a21\u5757\u6765\u5c06\u77e9\u9635\u8f6c\u6362\u4e3aCSV\u6587\u4ef6\u3002\u5982\u679c\u4f7f\u7528<code>pandas<\/code>\uff0c\u53ef\u4ee5\u5c06\u77e9\u9635\u8f6c\u6362\u4e3aDataFrame\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>to_csv()<\/code>\u65b9\u6cd5\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u3002\u5bf9\u4e8e<code>csv<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528<code>writer<\/code>\u5bf9\u8c61\u9010\u884c\u5199\u5165\u77e9\u9635\u6570\u636e\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u4fdd\u5b58\u5e76\u8fdb\u884c\u540e\u7eed\u5206\u6790\u3002<\/p>\n<p><strong>\u5728\u5c06\u77e9\u9635\u8f6c\u6362\u4e3aCSV\u65f6\uff0c\u6709\u54ea\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u5c06\u77e9\u9635\u8f6c\u6362\u4e3aCSV\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6570\u636e\u7684\u683c\u5f0f\u548c\u5206\u9694\u7b26\u3002\u786e\u4fdd\u77e9\u9635\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u4e00\u81f4\uff0c\u907f\u514d\u6df7\u5408\u4f7f\u7528\u5b57\u7b26\u4e32\u548c\u6570\u5b57\u3002\u6b64\u5916\uff0c\u9009\u62e9\u5408\u9002\u7684\u5206\u9694\u7b26\uff08\u5982\u9017\u53f7\u3001\u5236\u8868\u7b26\u7b49\uff09\u4e5f\u5f88\u91cd\u8981\uff0c\u4ee5\u4fdd\u8bc1\u540e\u7eed\u8bfb\u53d6\u6570\u636e\u65f6\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<p><strong>\u80fd\u5426\u5c06\u5e26\u6709\u6807\u9898\u7684\u77e9\u9635\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u5728\u4f7f\u7528<code>pandas<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u5728\u521b\u5efaDataFrame\u65f6\u5c06\u6807\u9898\u4f5c\u4e3a\u5217\u540d\u4f20\u5165\u3002\u4f7f\u7528<code>to_csv()<\/code>\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6e<code>header=True<\/code>\u4ee5\u5305\u542b\u5217\u540d\u3002\u5728\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u65f6\uff0c\u9996\u5148\u5199\u5165\u6807\u9898\u884c\uff0c\u7136\u540e\u518d\u5199\u5165\u77e9\u9635\u6570\u636e\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fddCSV\u6587\u4ef6\u7684\u53ef\u8bfb\u6027\u548c\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u77e9\u9635\u8f6c\u5316\u4e3aCSV\u8f93\u51fa\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001csv\u6a21\u5757\u3001numpy\u5e93\u3002 \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u6700\u4fbf [&hellip;]","protected":false},"author":3,"featured_media":1108366,"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\/1108350"}],"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=1108350"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108350\/revisions"}],"predecessor-version":[{"id":1108369,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108350\/revisions\/1108369"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108366"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}