{"id":932632,"date":"2024-12-26T18:01:44","date_gmt":"2024-12-26T10:01:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/932632.html"},"modified":"2024-12-26T18:01:47","modified_gmt":"2024-12-26T10:01:47","slug":"python%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%bacsv","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/932632.html","title":{"rendered":"python\u5982\u4f55\u521b\u5efacsv"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070351\/6974f080-d9a6-4fc8-9267-b14d265502d3.webp\" alt=\"python\u5982\u4f55\u521b\u5efacsv\" \/><\/p>\n<p><p> <strong>\u521b\u5efaCSV\u6587\u4ef6\u5728Python\u4e2d\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u3001<code>pandas<\/code>\u5e93\u548c<code>numpy<\/code>\u5e93\u7b49\u3002\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93\u3002<\/strong>\u8fd9\u91cc\u5c06\u91cd\u70b9\u4ecb\u7ecd\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u548c<code>pandas<\/code>\u5e93\u7684\u521b\u5efa\u65b9\u6cd5\u3002\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236CSV\u6587\u4ef6\u7684\u8bfb\u5199\u8fc7\u7a0b\uff0c\u800c\u4f7f\u7528<code>pandas<\/code>\u5e93\u5219\u80fd\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u5927\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528csv\u6a21\u5757<\/strong><br \/>Python\u7684\u5185\u7f6e<code>csv<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9CSV\u6587\u4ef6\u7684\u8bfb\u5199\u652f\u6301\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>csv.writer<\/code>\u5bf9\u8c61\u521b\u5efa\u5e76\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>1. \u4f7f\u7528csv\u6a21\u5757<\/strong><\/h2>\n<p>filename = &quot;example.csv&quot;<\/p>\n<h2><strong>\u6570\u636e\u53ef\u4ee5\u662f\u4e00\u4e2a\u5217\u8868\u7684\u5217\u8868<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;],<\/p>\n<p>    [&quot;Alice&quot;, 30, &quot;New York&quot;],<\/p>\n<p>    [&quot;Bob&quot;, 25, &quot;Los Angeles&quot;],<\/p>\n<p>    [&quot;Charlie&quot;, 35, &quot;Chicago&quot;],<\/p>\n<p>]<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u5199\u5165CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(filename, mode=&#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>newline=&#39;&#39;<\/code>\u53c2\u6570\u7528\u4e8e\u907f\u514d\u5728Windows\u4e0a\u6bcf\u884c\u4e4b\u95f4\u63d2\u5165\u989d\u5916\u7684\u7a7a\u884c\u3002\u4f7f\u7528<code>csv.writerows(data)<\/code>\u53ef\u4ee5\u4e00\u6b21\u6027\u5199\u5165\u591a\u884c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u5e93<\/strong><br \/><code>pandas<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u5904\u7406\u5e93\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u66f4\u52a0\u7b80\u4fbf\u5730\u521b\u5efa\u548c\u5904\u7406CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>2. \u4f7f\u7528pandas\u5e93<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &quot;Name&quot;: [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;],<\/p>\n<p>    &quot;Age&quot;: [30, 25, 35],<\/p>\n<p>    &quot;City&quot;: [&quot;New York&quot;, &quot;Los Angeles&quot;, &quot;Chicago&quot;]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5199\u5165CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&quot;example.csv&quot;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>pandas.DataFrame<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6846\u67b6\uff0c\u7136\u540e\u4f7f\u7528<code>to_csv<\/code>\u65b9\u6cd5\u5c06\u5176\u5199\u5165CSV\u6587\u4ef6\u4e2d\u3002\u53c2\u6570<code>index=False<\/code>\u7528\u4e8e\u907f\u514d\u5c06\u884c\u7d22\u5f15\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001CSV\u6587\u4ef6\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>CSV\uff08Comma-Separated Values\uff09\u6587\u4ef6\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6587\u672c\u683c\u5f0f\uff0c\u7528\u4e8e\u5b58\u50a8\u8868\u683c\u6570\u636e\uff0c\u5982\u7535\u5b50\u8868\u683c\u6216\u6570\u636e\u5e93\u3002\u6bcf\u884c\u6570\u636e\u79f0\u4e3a\u8bb0\u5f55\uff0c\u6bcf\u4e2a\u8bb0\u5f55\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u6bb5\u7ec4\u6210\uff0c\u5b57\u6bb5\u4e4b\u95f4\u7528\u9017\u53f7\u5206\u9694\u3002CSV\u6587\u4ef6\u683c\u5f0f\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u4ea4\u6362\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u6613\u4e8e\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><h4>CSV\u6587\u4ef6\u7684\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>CSV\u6587\u4ef6\u6ca1\u6709\u7279\u5b9a\u7684\u683c\u5f0f\u8981\u6c42\uff0c\u4f46\u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6807\u9898\u884c<\/strong>\uff1a\u7b2c\u4e00\u884c\u901a\u5e38\u5305\u542b\u5b57\u6bb5\u540d\u79f0\u3002<\/li>\n<li><strong>\u6570\u636e\u884c<\/strong>\uff1a\u540e\u7eed\u884c\u5305\u542b\u5b9e\u9645\u6570\u636e\u3002<\/li>\n<li><strong>\u5b57\u6bb5\u5206\u9694\u7b26<\/strong>\uff1a\u901a\u5e38\u4f7f\u7528\u9017\u53f7\uff08<code>,<\/code>\uff09\u4f5c\u4e3a\u5b57\u6bb5\u5206\u9694\u7b26\uff0c\u4f46\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7b26\u53f7\uff0c\u5982\u5206\u53f7\uff08<code>;<\/code>\uff09\u6216\u5236\u8868\u7b26\uff08<code>\\t<\/code>\uff09\u3002<\/li>\n<\/ul>\n<p><h4>CSV\u6587\u4ef6\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u8bfb\uff1aCSV\u6587\u4ef6\u662f\u7eaf\u6587\u672c\u6587\u4ef6\uff0c\u4efb\u4f55\u6587\u672c\u7f16\u8f91\u5668\u90fd\u53ef\u4ee5\u6253\u5f00\u3002<\/li>\n<li>\u5e7f\u6cdb\u652f\u6301\uff1a\u51e0\u4e4e\u6240\u6709\u7684\u6570\u636e\u5904\u7406\u5de5\u5177\u548c\u7f16\u7a0b\u8bed\u8a00\u90fd\u652f\u6301CSV\u683c\u5f0f\u3002<\/li>\n<li>\u6613\u4e8e\u89e3\u6790\uff1aCSV\u6587\u4ef6\u7ed3\u6784\u7b80\u5355\uff0c\u89e3\u6790\u901f\u5ea6\u5feb\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u65e0\u6cd5\u5b58\u50a8\u590d\u6742\u6570\u636e\uff1aCSV\u6587\u4ef6\u53ea\u80fd\u5b58\u50a8\u4e8c\u7ef4\u8868\u683c\u6570\u636e\uff0c\u4e0d\u652f\u6301\u5d4c\u5957\u7ed3\u6784\u3002<\/li>\n<li>\u7f3a\u4e4f\u6570\u636e\u7c7b\u578b\u4fe1\u606f\uff1aCSV\u6587\u4ef6\u4e0d\u5305\u542b\u6570\u636e\u7c7b\u578b\u4fe1\u606f\uff0c\u6240\u6709\u6570\u636e\u90fd\u88ab\u89c6\u4e3a\u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u6613\u51fa\u73b0\u89e3\u6790\u9519\u8bef\uff1a\u5982\u679c\u6570\u636e\u4e2d\u5305\u542b\u5206\u9694\u7b26\u5b57\u7b26\uff0c\u53ef\u80fd\u5bfc\u81f4\u89e3\u6790\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528csv\u6a21\u5757\u521b\u5efaCSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e<code>csv<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\u6765\u5904\u7406CSV\u6587\u4ef6\u3002\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u53ef\u4ee5\u7cbe\u786e\u63a7\u5236CSV\u6587\u4ef6\u7684\u8bfb\u5199\u8fc7\u7a0b\uff0c\u5305\u62ec\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u3001\u5f15\u7528\u5b57\u7b26\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efaCSV\u6587\u4ef6\u7684\u57fa\u672c\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165csv\u6a21\u5757<\/strong>\uff1a\u5728\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u5b83\u3002<\/li>\n<li><strong>\u6253\u5f00\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u4ee5\u5199\u6a21\u5f0f\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u3002<\/li>\n<li><strong>\u521b\u5efacsv.writer\u5bf9\u8c61<\/strong>\uff1a\u4f7f\u7528<code>csv.writer()<\/code>\u521b\u5efa\u4e00\u4e2awriter\u5bf9\u8c61\u3002<\/li>\n<li><strong>\u5199\u5165\u6570\u636e<\/strong>\uff1a\u4f7f\u7528<code>writer.writerow()<\/code>\u6216<code>writer.writerows()<\/code>\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002<\/li>\n<li><strong>\u5173\u95ed\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528<code>file.close()<\/code>\u6216\u901a\u8fc7<code>with<\/code>\u8bed\u53e5\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><h4>\u4ee3\u7801\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u521b\u5efaCSV\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.csv&#39;, mode=&#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    # \u521b\u5efacsv.writer\u5bf9\u8c61<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    # \u5199\u5165\u6807\u9898\u884c<\/p>\n<p>    writer.writerow([&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;])<\/p>\n<p>    # \u5199\u5165\u6570\u636e\u884c<\/p>\n<p>    writer.writerow([&quot;Alice&quot;, 30, &quot;New York&quot;])<\/p>\n<p>    writer.writerow([&quot;Bob&quot;, 25, &quot;Los Angeles&quot;])<\/p>\n<p>    writer.writerow([&quot;Charlie&quot;, 35, &quot;Chicago&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u81ea\u5b9a\u4e49CSV\u683c\u5f0f<\/h4>\n<\/p>\n<p><p><code>csv.writer()<\/code>\u51fd\u6570\u5141\u8bb8\u81ea\u5b9a\u4e49CSV\u6587\u4ef6\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\u66f4\u6539\u5b57\u6bb5\u5206\u9694\u7b26\u6216\u5f15\u7528\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;custom_data.csv&#39;, mode=&#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    # \u521b\u5efacsv.writer\u5bf9\u8c61\uff0c\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u548c\u5f15\u7528\u5b57\u7b26<\/p>\n<p>    writer = csv.writer(file, delimiter=&#39;;&#39;, quotechar=&#39;&quot;&#39;, quoting=csv.QUOTE_MINIMAL)<\/p>\n<p>    # \u5199\u5165\u6570\u636e<\/p>\n<p>    writer.writerow([&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;])<\/p>\n<p>    writer.writerow([&quot;Alice&quot;, 30, &quot;New York&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pandas\u5e93\u521b\u5efaCSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u63a5\u53e3\u6765\u5904\u7406CSV\u6587\u4ef6\u3002\u4f7f\u7528<code>pandas<\/code>\u5e93\u53ef\u4ee5\u66f4\u52a0\u65b9\u4fbf\u5730\u64cd\u4f5c\u5927\u6570\u636e\u96c6\uff0c\u8fdb\u884c\u6570\u636e\u8f6c\u6362\u548c\u6e05\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efaCSV\u6587\u4ef6\u7684\u57fa\u672c\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165pandas\u5e93<\/strong>\uff1a\u5728\u4f7f\u7528<code>pandas<\/code>\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u5b83\u3002<\/li>\n<li><strong>\u521b\u5efaDataFrame<\/strong>\uff1a\u4f7f\u7528<code>pandas.DataFrame()<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6846\u67b6\u3002<\/li>\n<li><strong>\u5199\u5165CSV\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528<code>DataFrame.to_csv()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><h4>\u4ee3\u7801\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>pandas<\/code>\u5e93\u521b\u5efaCSV\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &quot;Name&quot;: [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;],<\/p>\n<p>    &quot;Age&quot;: [30, 25, 35],<\/p>\n<p>    &quot;City&quot;: [&quot;New York&quot;, &quot;Los Angeles&quot;, &quot;Chicago&quot;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5199\u5165CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&quot;data.csv&quot;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u81ea\u5b9a\u4e49CSV\u683c\u5f0f<\/h4>\n<\/p>\n<p><p><code>to_csv()<\/code>\u65b9\u6cd5\u5141\u8bb8\u81ea\u5b9a\u4e49CSV\u6587\u4ef6\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\u66f4\u6539\u5206\u9694\u7b26\u3001\u6307\u5b9a\u7f16\u7801\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &quot;Name&quot;: [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;],<\/p>\n<p>    &quot;Age&quot;: [30, 25, 35],<\/p>\n<p>    &quot;City&quot;: [&quot;New York&quot;, &quot;Los Angeles&quot;, &quot;Chicago&quot;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5199\u5165CSV\u6587\u4ef6\uff0c\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u548c\u7f16\u7801<\/strong><\/h2>\n<p>df.to_csv(&quot;custom_data.csv&quot;, sep=&#39;;&#39;, encoding=&#39;utf-8&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001CSV\u6587\u4ef6\u7684\u5e38\u89c1\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9664\u4e86\u521b\u5efaCSV\u6587\u4ef6\u5916\uff0c\u8fd8\u9700\u8981\u5bf9CSV\u6587\u4ef6\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u8bfb\u53d6\u3001\u66f4\u65b0\u3001\u5220\u9664\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684CSV\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u6216<code>pandas<\/code>\u5e93\u90fd\u53ef\u4ee5\u8bfb\u53d6CSV\u6587\u4ef6\u3002\u4f7f\u7528<code>pandas<\/code>\u5e93\u8bfb\u53d6CSV\u6587\u4ef6\u66f4\u4e3a\u65b9\u4fbf\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&quot;data.csv&quot;)<\/p>\n<h2><strong>\u8f93\u51fa\u8bfb\u53d6\u7684\u6570\u636e<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u66f4\u65b0CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u66f4\u65b0CSV\u6587\u4ef6\u901a\u5e38\u9700\u8981\u5148\u8bfb\u53d6\u6587\u4ef6\uff0c\u4fee\u6539\u6570\u636e\u540e\u518d\u5199\u56de\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&quot;data.csv&quot;)<\/p>\n<h2><strong>\u4fee\u6539\u6570\u636e<\/strong><\/h2>\n<p>df.loc[1, &#39;Age&#39;] = 26<\/p>\n<h2><strong>\u5199\u56deCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&quot;data.csv&quot;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5220\u9664CSV\u6587\u4ef6\u4e2d\u7684\u884c\u6216\u5217<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>drop()<\/code>\u65b9\u6cd5\u5220\u9664CSV\u6587\u4ef6\u4e2d\u7684\u884c\u6216\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&quot;data.csv&quot;)<\/p>\n<h2><strong>\u5220\u9664\u4e00\u884c<\/strong><\/h2>\n<p>df = df.drop(index=1)<\/p>\n<h2><strong>\u5220\u9664\u4e00\u5217<\/strong><\/h2>\n<p>df = df.drop(columns=&#39;City&#39;)<\/p>\n<h2><strong>\u5199\u56deCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&quot;data.csv&quot;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5927\u578bCSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578bCSV\u6587\u4ef6\u65f6\uff0c\u5185\u5b58\u53ef\u80fd\u6210\u4e3a\u9650\u5236\u56e0\u7d20\u3002<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u4f18\u5316\u5185\u5b58\u4f7f\u7528\uff0c\u4f8b\u5982\u6309\u5757\u8bfb\u53d6\u6570\u636e\u548c\u6307\u5b9a\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>\u6309\u5757\u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pandas.read_csv()<\/code>\u51fd\u6570\u7684<code>chunksize<\/code>\u53c2\u6570\u53ef\u4ee5\u6309\u5757\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u6309\u5757\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>chunksize = 1000<\/p>\n<p>for chunk in pd.read_csv(&quot;large_data.csv&quot;, chunksize=chunksize):<\/p>\n<p>    # \u5904\u7406\u6bcf\u4e2a\u5757\u7684\u6570\u636e<\/p>\n<p>    print(chunk)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6307\u5b9a\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6CSV\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>dtype<\/code>\u53c2\u6570\u6307\u5b9a\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u6307\u5b9a\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p>dtype = {&#39;Name&#39;: &#39;str&#39;, &#39;Age&#39;: &#39;int32&#39;, &#39;City&#39;: &#39;str&#39;}<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&quot;data.csv&quot;, dtype=dtype)<\/p>\n<h2><strong>\u8f93\u51fa\u8bfb\u53d6\u7684\u6570\u636e<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u548c\u5904\u7406CSV\u6587\u4ef6\u662f\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u4e2d\u7684\u5e38\u89c1\u4efb\u52a1\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u548c\u5e93\u6765\u7b80\u5316\u8fd9\u4e00\u8fc7\u7a0b\uff0c\u5305\u62ec\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u548c\u529f\u80fd\u5f3a\u5927\u7684<code>pandas<\/code>\u5e93\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u96c6\u7684\u5927\u5c0f\u3002\u5728\u5904\u7406\u5927\u578bCSV\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684\u6309\u5757\u8bfb\u53d6\u548c\u6570\u636e\u7c7b\u578b\u4f18\u5316\u529f\u80fd\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\uff0c\u63d0\u9ad8\u5904\u7406\u6548\u7387\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u4e86\u89e3CSV\u6587\u4ef6\u7684\u57fa\u672c\u7ed3\u6784\u548c\u5e38\u89c1\u64cd\u4f5c\u90fd\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efaCSV\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u8f7b\u677e\u521b\u5efaCSV\u6587\u4ef6\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165<code>csv<\/code>\u6a21\u5757\uff0c\u7136\u540e\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u4ee5\u5199\u5165\u6570\u636e\u3002\u4f7f\u7528<code>csv.writer<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import csv\n\ndata = [\n    [&#39;\u59d3\u540d&#39;, &#39;\u5e74\u9f84&#39;, &#39;\u57ce\u5e02&#39;],\n    [&#39;\u5f20\u4e09&#39;, 28, &#39;\u5317\u4eac&#39;],\n    [&#39;\u674e\u56db&#39;, 34, &#39;\u4e0a\u6d77&#39;],\n]\n\nwith open(&#39;output.csv&#39;, mode=&#39;w&#39;, newline=&#39;&#39;) as file:\n    writer = csv.writer(file)\n    writer.writerows(data)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u540e\uff0c\u60a8\u5c06\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u770b\u5230\u4e00\u4e2a\u540d\u4e3a<code>output.csv<\/code>\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728\u521b\u5efaCSV\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u5728\u521b\u5efaCSV\u6587\u4ef6\u65f6\uff0c\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u5408\u9002\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u4f7f\u7528<code>utf-8<\/code>\u7f16\u7801\u53ef\u4ee5\u907f\u514d\u4e2d\u6587\u5b57\u7b26\u51fa\u73b0\u4e71\u7801\u3002\u5728\u6253\u5f00\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a<code>encoding=&#39;utf-8&#39;<\/code>\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;output.csv&#39;, mode=&#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as file:\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u60a8\u5728CSV\u6587\u4ef6\u4e2d\u6b63\u786e\u4fdd\u5b58\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5411\u73b0\u6709CSV\u6587\u4ef6\u6dfb\u52a0\u6570\u636e\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u9700\u8981\u5411\u73b0\u6709\u7684CSV\u6587\u4ef6\u6dfb\u52a0\u65b0\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>mode=&#39;a&#39;<\/code>\u6765\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8ffd\u52a0\u3002\u4e0e\u5199\u5165\u65b0\u6587\u4ef6\u7684\u8fc7\u7a0b\u7c7b\u4f3c\uff0c\u60a8\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528<code>csv.writer<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">new_data = [[&#39;\u738b\u4e94&#39;, 30, &#39;\u5e7f\u5dde&#39;]]\n\nwith open(&#39;output.csv&#39;, mode=&#39;a&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as file:\n    writer = csv.writer(file)\n    writer.writerows(new_data)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728<code>output.csv<\/code>\u6587\u4ef6\u4e2d\u6dfb\u52a0\u65b0\u7684\u4e00\u884c\u6570\u636e\uff0c\u800c\u4e0d\u4f1a\u8986\u76d6\u539f\u6709\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u521b\u5efaCSV\u6587\u4ef6\u5728Python\u4e2d\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684csv\u6a21\u5757\u3001pandas\u5e93\u548cnum [&hellip;]","protected":false},"author":3,"featured_media":932637,"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\/932632"}],"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=932632"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932632\/revisions"}],"predecessor-version":[{"id":932638,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932632\/revisions\/932638"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/932637"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=932632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=932632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=932632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}