{"id":1009107,"date":"2024-12-27T11:08:46","date_gmt":"2024-12-27T03:08:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1009107.html"},"modified":"2024-12-27T11:08:49","modified_gmt":"2024-12-27T03:08:49","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ad%98%e5%82%a8csv","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1009107.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5b58\u50a8csv"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084134\/b9ca1237-0226-41c2-8489-8bcfe122e5ed.webp\" alt=\"python\u4e2d\u5982\u4f55\u5b58\u50a8csv\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5b58\u50a8CSV\u6587\u4ef6\u6709\u591a\u79cd\u65b9\u6cd5\uff0c<strong>\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u3001\u5229\u7528<code>pandas<\/code>\u5e93\u3001\u7ed3\u5408<code>numpy<\/code>\u5e93<\/strong>\u662f\u5e38\u7528\u7684\u51e0\u79cd\u65b9\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u6765\u5b58\u50a8CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<code>csv<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u65e0\u9700\u5b89\u88c5\u4efb\u4f55\u989d\u5916\u7684\u5e93\u5373\u53ef\u4f7f\u7528\u3002\u901a\u8fc7<code>csv.writer<\/code>\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u4ee5\u5199\u5165\u6a21\u5f0f\uff08\u901a\u5e38\u662f<code>&#39;w&#39;<\/code>\uff09\u6253\u5f00\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a<code>csv.writer<\/code>\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>writerow<\/code>\u65b9\u6cd5\u5c06\u884c\u5199\u5165\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u7528\u4e8e\u5199\u5165<\/strong><\/h2>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2acsv.writer\u5bf9\u8c61<\/p>\n<p>    csvwriter = csv.writer(csvfile)<\/p>\n<p>    # \u5199\u5165\u8868\u5934<\/p>\n<p>    csvwriter.writerow([&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;])<\/p>\n<p>    # \u5199\u5165\u6570\u636e\u884c<\/p>\n<p>    csvwriter.writerow([&#39;Alice&#39;, 30, &#39;New York&#39;])<\/p>\n<p>    csvwriter.writerow([&#39;Bob&#39;, 25, &#39;Los Angeles&#39;])<\/p>\n<p>    csvwriter.writerow([&#39;Charlie&#39;, 35, &#39;Chicago&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u6253\u5f00\u4e86\u4e00\u4e2a\u540d\u4e3a<code>output.csv<\/code>\u7684\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>csv.writer<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u5199\u5165\u5bf9\u8c61\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>writerow<\/code>\u65b9\u6cd5\u5199\u5165\u4e86\u8868\u5934\u548c\u51e0\u884c\u6570\u636e\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u6253\u5f00\u6587\u4ef6\u65f6\u9700\u8981\u4f7f\u7528<code>newline=&#39;&#39;<\/code>\u6765\u907f\u514d\u591a\u4f59\u7684\u7a7a\u884c\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>CSV<\/code>\u6a21\u5757\u5b58\u50a8CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>csv<\/code>\u6a21\u5757\u662f\u5904\u7406CSV\u6587\u4ef6\u7684\u6807\u51c6\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u8bfb\u5199CSV\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>csv<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e24\u4e2a\u4e3b\u8981\u7684\u7c7b\uff1a<code>csv.reader<\/code>\u548c<code>csv.writer<\/code>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u6765\u8bfb\u53d6\u548c\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u5199\u5165CSV\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u5199\u5165CSV\u6587\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a<code>csv.writer<\/code>\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u5b83\u7684<code>writerow<\/code>\u6216<code>writerows<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u51c6\u5907\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;],<\/p>\n<p>    [&#39;Alice&#39;, 30, &#39;New York&#39;],<\/p>\n<p>    [&#39;Bob&#39;, 25, &#39;Los Angeles&#39;],<\/p>\n<p>    [&#39;Charlie&#39;, 35, &#39;Chicago&#39;]<\/p>\n<p>]<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6\u5e76\u521b\u5efacsv.writer\u5bf9\u8c61<\/strong><\/h2>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>    csvwriter = csv.writer(csvfile)<\/p>\n<p>    # \u5199\u5165\u591a\u884c\u6570\u636e<\/p>\n<p>    csvwriter.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u51c6\u5907\u4e86\u4e00\u4e2a\u5305\u542b\u6570\u636e\u7684\u5217\u8868<code>data<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6<code>output.csv<\/code>\u7528\u4e8e\u5199\u5165\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a<code>csv.writer<\/code>\u5bf9\u8c61\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>writerows<\/code>\u65b9\u6cd5\u5199\u5165\u591a\u884c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528\u5b57\u5178\u5199\u5165CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u5199\u5165\u5217\u8868\u6570\u636e\uff0c<code>csv<\/code>\u6a21\u5757\u8fd8\u652f\u6301\u4f7f\u7528\u5b57\u5178\u5199\u5165CSV\u6587\u4ef6\u3002\u8fd9\u5728\u5904\u7406\u5177\u6709\u8868\u5934\u7684\u6570\u636e\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u51c6\u5907\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    {&#39;Name&#39;: &#39;Alice&#39;, &#39;Age&#39;: 30, &#39;City&#39;: &#39;New York&#39;},<\/p>\n<p>    {&#39;Name&#39;: &#39;Bob&#39;, &#39;Age&#39;: 25, &#39;City&#39;: &#39;Los Angeles&#39;},<\/p>\n<p>    {&#39;Name&#39;: &#39;Charlie&#39;, &#39;Age&#39;: 35, &#39;City&#39;: &#39;Chicago&#39;}<\/p>\n<p>]<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6\u5e76\u521b\u5efaDictWriter\u5bf9\u8c61<\/strong><\/h2>\n<p>with open(&#39;output_dict.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>    fieldnames = [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;]<\/p>\n<p>    csvwriter = csv.DictWriter(csvfile, fieldnames=fieldnames)<\/p>\n<p>    # \u5199\u5165\u8868\u5934<\/p>\n<p>    csvwriter.writeheader()<\/p>\n<p>    # \u5199\u5165\u6570\u636e\u884c<\/p>\n<p>    csvwriter.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u51c6\u5907\u4e86\u4e00\u4e9b\u5b57\u5178\u6570\u636e\uff0c\u5e76\u4f7f\u7528<code>csv.DictWriter<\/code>\u5bf9\u8c61\u6765\u5199\u5165\u6587\u4ef6\u3002<code>DictWriter<\/code>\u9700\u8981\u4e00\u4e2a<code>fieldnames<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u8868\u5934\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>writeheader<\/code>\u65b9\u6cd5\u5199\u5165\u8868\u5934\uff0c\u5e76\u4f7f\u7528<code>writerows<\/code>\u65b9\u6cd5\u5199\u5165\u6570\u636e\u884c\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>Pandas<\/code>\u5b58\u50a8CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406\u6570\u636e\u3002\u4f7f\u7528<code>pandas<\/code>\u5b58\u50a8CSV\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>pandas<\/code>\u63d0\u4f9b\u4e86<code>to_csv<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06<code>DataFrame<\/code>\u5bf9\u8c61\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efaDataFrame\u5bf9\u8c61<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [30, 25, 35],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;output_pandas.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a<code>DataFrame<\/code>\u5bf9\u8c61<code>df<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>to_csv<\/code>\u65b9\u6cd5\u5c06\u5176\u4fdd\u5b58\u4e3a<code>output_pandas.csv<\/code>\u6587\u4ef6\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u4f7f\u7528<code>index=False<\/code>\u6765\u907f\u514d\u5c06\u7d22\u5f15\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u5904\u7406\u7f3a\u5931\u6570\u636e<\/h4>\n<\/p>\n<p><p><code>pandas<\/code>\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u7f3a\u5931\u6570\u636e\u3002\u5f53\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u4f7f\u7528\u4ec0\u4e48\u7b26\u53f7\u6765\u8868\u793a\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efaDataFrame\u5bf9\u8c61\uff0c\u5305\u542b\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [30, np.nan, 35],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\uff0c\u4f7f\u7528\u7a7a\u5b57\u7b26\u4e32\u8868\u793a\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df.to_csv(&#39;output_missing.csv&#39;, index=False, na_rep=&#39;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>np.nan<\/code>\u6765\u8868\u793a\u7f3a\u5931\u503c\u3002\u5728\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u65f6\uff0c\u6211\u4eec\u4f7f\u7528<code>na_rep=&#39;&#39;<\/code>\u6765\u6307\u5b9a\u4f7f\u7528\u7a7a\u5b57\u7b26\u4e32\u8868\u793a\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>Numpy<\/code>\u5b58\u50a8CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>numpy<\/code>\u63d0\u4f9b\u4e86<code>savetxt<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u6570\u7ec4\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\uff0c\u5305\u62ecCSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efanumpy\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([<\/p>\n<p>    [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;],<\/p>\n<p>    [&#39;Alice&#39;, 30, &#39;New York&#39;],<\/p>\n<p>    [&#39;Bob&#39;, 25, &#39;Los Angeles&#39;],<\/p>\n<p>    [&#39;Charlie&#39;, 35, &#39;Chicago&#39;]<\/p>\n<p>])<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>np.savetxt(&#39;output_numpy.csv&#39;, data, delimiter=&#39;,&#39;, fmt=&#39;%s&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a<code>numpy<\/code>\u6570\u7ec4<code>data<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>savetxt<\/code>\u51fd\u6570\u5c06\u5176\u4fdd\u5b58\u4e3a<code>output_numpy.csv<\/code>\u6587\u4ef6\u3002\u6211\u4eec\u4f7f\u7528<code>delimiter=&#39;,&#39;<\/code>\u6765\u6307\u5b9a\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u5e76\u4f7f\u7528<code>fmt=&#39;%s&#39;<\/code>\u6765\u6307\u5b9a\u683c\u5f0f\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4fdd\u5b58\u6570\u503c\u6570\u636e<\/h4>\n<\/p>\n<p><p><code>numpy<\/code>\u975e\u5e38\u9002\u5408\u5904\u7406\u6570\u503c\u6570\u636e\u3002\u5728\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u6570\u503c\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u503c\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([<\/p>\n<p>    [1.5, 2.3, 3.1],<\/p>\n<p>    [4.0, 5.6, 6.2],<\/p>\n<p>    [7.8, 8.9, 9.7]<\/p>\n<p>])<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\uff0c\u6307\u5b9a\u6570\u503c\u683c\u5f0f<\/strong><\/h2>\n<p>np.savetxt(&#39;output_values.csv&#39;, data, delimiter=&#39;,&#39;, fmt=&#39;%.2f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u6570\u503c\u6570\u7ec4<code>data<\/code>\u3002\u5728\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u65f6\uff0c\u6211\u4eec\u4f7f\u7528<code>fmt=&#39;%.2f&#39;<\/code>\u6765\u6307\u5b9a\u6570\u503c\u683c\u5f0f\u4e3a\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u56db\u3001\u5b58\u50a8\u5927\u89c4\u6a21CSV\u6587\u4ef6\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21CSV\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u95ee\u9898\uff0c\u4ee5\u63d0\u9ad8\u6548\u7387\u548c\u907f\u514d\u6f5c\u5728\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5185\u5b58\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u5185\u5b58\u7ba1\u7406\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u95ee\u9898\u3002\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528\u6d41\u5f0f\u5904\u7406\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u5927\u89c4\u6a21\u6570\u636e\u96c6<\/strong><\/h2>\n<p>large_data = [[&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;]] + [[&#39;Person{}&#39;.format(i), i, &#39;City{}&#39;.format(i)] for i in range(1000000)]<\/p>\n<h2><strong>\u4f7f\u7528\u6d41\u5f0f\u5904\u7406\u5199\u5165CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;large_output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>    csvwriter = csv.writer(csvfile)<\/p>\n<p>    for row in large_data:<\/p>\n<p>        csvwriter.writerow(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6d41\u5f0f\u5904\u7406\u9010\u884c\u5199\u5165\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u538b\u7f29\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684CSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u538b\u7f29\u6280\u672f\u6765\u51cf\u5c11\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efaDataFrame\u5bf9\u8c61<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;Age&#39;: [30, 25, 35], &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3a\u538b\u7f29\u7684CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;output_compressed.csv.gz&#39;, index=False, compression=&#39;gzip&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pandas<\/code>\u5c06\u6570\u636e\u4fdd\u5b58\u4e3a\u538b\u7f29\u7684CSV\u6587\u4ef6\u3002\u8fd9\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b58\u50a8CSV\u6587\u4ef6\u3002<strong><code>csv<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u8bfb\u5199\u529f\u80fd\uff0c\u9002\u5408\u5904\u7406\u7b80\u5355\u7684CSV\u6587\u4ef6<\/strong>\u3002<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002<code>numpy<\/code>\u5e93\u975e\u5e38\u9002\u5408\u5904\u7406\u6570\u503c\u6570\u636e\u3002\u5728\u5904\u7406\u5927\u89c4\u6a21CSV\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u5185\u5b58\u7ba1\u7406\u548c\u6587\u4ef6\u538b\u7f29\u7b49\u95ee\u9898\uff0c\u4ee5\u63d0\u9ad8\u6548\u7387\u548c\u6027\u80fd\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u6280\u672f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6CSV\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8bfb\u53d6CSV\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u6216\u8005<code>pandas<\/code>\u5e93\u3002\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>csv.reader<\/code>\u6765\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u3002\u800c\u4f7f\u7528<code>pandas<\/code>\u5e93\u5219\u53ef\u4ee5\u901a\u8fc7<code>pandas.read_csv()<\/code>\u51fd\u6570\uff0c\u5feb\u901f\u5c06CSV\u6587\u4ef6\u52a0\u8f7d\u4e3aDataFrame\u683c\u5f0f\uff0c\u65b9\u4fbf\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u6765\u5199\u5165CSV\u6587\u4ef6\uff1f<\/strong><br \/>\u5199\u5165CSV\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u3002\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>csv.writer<\/code>\u6765\u5199\u5165\u6570\u636e\u3002\u5bf9\u4e8e<code>pandas<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528DataFrame\u7684<code>to_csv()<\/code>\u65b9\u6cd5\uff0c\u5c06\u6570\u636e\u8f7b\u677e\u4fdd\u5b58\u4e3aCSV\u683c\u5f0f\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7684\u590d\u6742\u6027\u548c\u5bf9\u8f93\u51fa\u683c\u5f0f\u7684\u9700\u6c42\u6765\u51b3\u5b9a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e2d\u7684CSV\u6587\u4ef6\u7f16\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u5904\u7406CSV\u6587\u4ef6\u65f6\uff0c\u7f16\u7801\u95ee\u9898\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u8bfb\u53d6\u6216\u5199\u5165\u65f6\u51fa\u73b0\u4e71\u7801\u3002\u53ef\u4ee5\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u4f8b\u5982\u4f7f\u7528<code>encoding=&#39;utf-8&#39;<\/code>\u6216<code>encoding=&#39;gbk&#39;<\/code>\u3002\u5728\u4f7f\u7528<code>pandas<\/code>\u65f6\uff0c<code>read_csv()<\/code>\u548c<code>to_csv()<\/code>\u51fd\u6570\u540c\u6837\u63d0\u4f9b\u4e86<code>encoding<\/code>\u53c2\u6570\uff0c\u786e\u4fdd\u6570\u636e\u4ee5\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u8fdb\u884c\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b58\u50a8CSV\u6587\u4ef6\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528csv\u6a21\u5757\u3001\u5229\u7528pandas\u5e93\u3001\u7ed3\u5408numpy\u5e93\u662f\u5e38\u7528\u7684\u51e0\u79cd\u65b9\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":1009113,"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\/1009107"}],"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=1009107"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009107\/revisions"}],"predecessor-version":[{"id":1009116,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009107\/revisions\/1009116"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1009113"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1009107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1009107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1009107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}