{"id":959357,"date":"2024-12-27T03:39:58","date_gmt":"2024-12-26T19:39:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/959357.html"},"modified":"2024-12-27T03:40:00","modified_gmt":"2024-12-26T19:40:00","slug":"python%e5%a6%82%e4%bd%95%e6%94%b9csv%e7%bc%96%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/959357.html","title":{"rendered":"python\u5982\u4f55\u6539csv\u7f16\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102204\/387b5782-9362-4e03-b6c3-9f808649f272.webp\" alt=\"python\u5982\u4f55\u6539csv\u7f16\u7801\" \/><\/p>\n<p><p> \u4e00\u3001PYTHON\u4e2d\u4fee\u6539CSV\u7f16\u7801\u7684\u65b9\u6cd5  <\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3001csv\u6a21\u5757\u3001\u6216\u8005\u624b\u52a8\u5904\u7406\u6587\u4ef6<\/strong>\u6765\u66f4\u6539CSV\u6587\u4ef6\u7684\u7f16\u7801\u3002\u901a\u5e38\u9009\u62e9\u4e00\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6587\u4ef6\u5927\u5c0f\u7b49\u56e0\u7d20\u3002<strong>pandas\u5e93\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\u5904\u7406\u6570\u636e\u6587\u4ef6<\/strong>\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u51e0\u79cd\u65b9\u6cd5\u6765\u6539\u53d8CSV\u6587\u4ef6\u7684\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528pandas\u5e93\u662f\u975e\u5e38\u4fbf\u5229\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3apandas\u5177\u5907\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3002\u5728\u5904\u7406CSV\u6587\u4ef6\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u6539\u53d8\u7f16\u7801\u4ee5\u786e\u4fdd\u6570\u636e\u6b63\u786e\u8bfb\u53d6\u3002\u4f8b\u5982\uff0c\u4ece\u4e0d\u517c\u5bb9\u7684Windows\u7f16\u7801\uff08\u5982&#39;ISO-8859-1&#39;\u6216&#39;latin1&#39;\uff09\u8f6c\u6362\u5230UTF-8\u7f16\u7801\u3002\u4f7f\u7528pandas\u5e93\u8bfb\u53d6\u548c\u4fdd\u5b58CSV\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>encoding<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528pandas\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u6307\u5b9a\u5f53\u524d\u7f16\u7801<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;input.csv&#39;, encoding=&#39;ISO-8859-1&#39;)<\/p>\n<h2><strong>\u5c06DataFrame\u4fdd\u5b58\u5230\u65b0\u7684CSV\u6587\u4ef6\uff0c\u5e76\u6307\u5b9a\u76ee\u6807\u7f16\u7801<\/strong><\/h2>\n<p>df.to_csv(&#39;output.csv&#39;, encoding=&#39;utf-8&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u7528pandas\u7684<code>read_csv<\/code>\u51fd\u6570\u8bfb\u53d6\u4e00\u4e2a\u7f16\u7801\u4e3a&#39;ISO-8859-1&#39;\u7684CSV\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230DataFrame\u4e2d\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>to_csv<\/code>\u65b9\u6cd5\u5c06DataFrame\u4fdd\u5b58\u5230\u65b0\u7684CSV\u6587\u4ef6\u4e2d\uff0c\u5e76\u6307\u5b9a\u76ee\u6807\u7f16\u7801\u4e3a&#39;utf-8&#39;\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u80fd\u591f\u6709\u6548\u5730\u8f6c\u6362CSV\u6587\u4ef6\u7684\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528CSV\u6a21\u5757  <\/p>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684csv\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u6765\u5904\u7406CSV\u6587\u4ef6\uff0c\u5e76\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u6765\u6539\u53d8\u7f16\u7801\u3002\u76f8\u8f83\u4e8epandas\uff0ccsv\u6a21\u5757\u66f4\u8f7b\u91cf\u7ea7\uff0c\u9002\u5408\u5904\u7406\u8f83\u5c0f\u7684\u6587\u4ef6\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528csv\u6a21\u5757\u6539\u53d8CSV\u6587\u4ef6\u7684\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u6253\u5f00\u539f\u59cbCSV\u6587\u4ef6\uff0c\u6307\u5b9a\u5176\u7f16\u7801<\/strong><\/h2>\n<p>with open(&#39;input.csv&#39;, &#39;r&#39;, encoding=&#39;ISO-8859-1&#39;) as input_file:<\/p>\n<p>    reader = csv.reader(input_file)<\/p>\n<p>    # \u6253\u5f00\u76ee\u6807CSV\u6587\u4ef6\uff0c\u6307\u5b9a\u65b0\u7684\u7f16\u7801<\/p>\n<p>    with open(&#39;output.csv&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;, newline=&#39;&#39;) as output_file:<\/p>\n<p>        writer = csv.writer(output_file)<\/p>\n<p>        # \u5c06\u8bfb\u53d6\u5230\u7684\u6570\u636e\u9010\u884c\u5199\u5165\u65b0\u7684CSV\u6587\u4ef6<\/p>\n<p>        for row in reader:<\/p>\n<p>            writer.writerow(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u7528\u6307\u5b9a\u7684\u7f16\u7801\u6253\u5f00\u539f\u59cbCSV\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528csv.reader\u8bfb\u53d6\u6570\u636e\u3002\u7136\u540e\u6211\u4eec\u6253\u5f00\u76ee\u6807CSV\u6587\u4ef6\uff0c\u5e76\u6307\u5b9a\u65b0\u7684\u7f16\u7801\u4e3a&#39;utf-8&#39;\u3002\u6700\u540e\uff0c\u6211\u4eec\u901a\u8fc7csv.writer\u5c06\u8bfb\u53d6\u5230\u7684\u6570\u636e\u9010\u884c\u5199\u5165\u5230\u65b0\u7684\u6587\u4ef6\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u8f83\u4e3a\u7e41\u7410\uff0c\u4f46\u80fd\u8ba9\u6211\u4eec\u5bf9\u6570\u636e\u7684\u5904\u7406\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u624b\u52a8\u5904\u7406\u6587\u4ef6  <\/p>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u4f9d\u8d56\u4efb\u4f55\u5916\u90e8\u5e93\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u7684\u65b9\u5f0f\u6765\u6539\u53d8CSV\u6587\u4ef6\u7684\u7f16\u7801\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u8fdb\u884c\u8fd9\u79cd\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u539f\u59cb\u6587\u4ef6\u5e76\u8bfb\u53d6\u6570\u636e<\/p>\n<p>with open(&#39;input.csv&#39;, &#39;r&#39;, encoding=&#39;ISO-8859-1&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<h2><strong>\u5c06\u6570\u636e\u5199\u5165\u5230\u65b0\u6587\u4ef6\uff0c\u6307\u5b9a\u65b0\u7684\u7f16\u7801<\/strong><\/h2>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4ee5&#39;ISO-8859-1&#39;\u7f16\u7801\u6253\u5f00\u539f\u59cbCSV\u6587\u4ef6\uff0c\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u7136\u540e\u4ee5&#39;utf-8&#39;\u7f16\u7801\u6253\u5f00\u76ee\u6807CSV\u6587\u4ef6\uff0c\u5e76\u5c06\u8bfb\u53d6\u5230\u7684\u6570\u636e\u5199\u5165\u5176\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6709\u6548\uff0c\u4f46\u4e0d\u9002\u5408\u5904\u7406\u975e\u5e38\u5927\u7684\u6587\u4ef6\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5c06\u6574\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5  <\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5982\u4f55\u6539\u53d8CSV\u6587\u4ef6\u7f16\u7801\u7684\u65b9\u6cd5\u65f6\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u6587\u4ef6\u7684\u5927\u5c0f\u3001\u7f16\u7801\u7684\u590d\u6742\u6027\u4ee5\u53ca\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u7684\u5176\u4ed6\u5904\u7406\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c<strong>pandas\u5e93\u662f\u6700\u4f73\u9009\u62e9<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u8f7b\u677e\u6539\u53d8\u7f16\u7801\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u5176\u4ed6\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u3002\u800c\u5bf9\u4e8e\u5c0f\u6587\u4ef6\u6216\u8005\u4e0d\u60f3\u5f15\u5165\u989d\u5916\u4f9d\u8d56\u7684\u60c5\u51b5\uff0c\u4f7f\u7528csv\u6a21\u5757\u6216\u624b\u52a8\u5904\u7406\u6587\u4ef6\u662f\u53ef\u884c\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879  <\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406CSV\u6587\u4ef6\u7f16\u7801\u65f6\uff0c\u6709\u51e0\u70b9\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u786e\u4fdd\u6e90\u6587\u4ef6\u7684\u7f16\u7801\u6b63\u786e\u65e0\u8bef\uff0c\u5426\u5219\u5728\u8bfb\u53d6\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u5b57\u7b26\u89e3\u7801\u9519\u8bef\u3002\u5176\u6b21\uff0c\u5f53\u4fdd\u5b58\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u76ee\u6807\u7f16\u7801\u4e0e\u540e\u7eed\u4f7f\u7528\u8be5\u6587\u4ef6\u7684\u7a0b\u5e8f\u517c\u5bb9\u3002\u4f8b\u5982\uff0cUTF-8\u662f\u4e00\u4e2a\u901a\u7528\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u80fd\u591f\u517c\u5bb9\u5927\u591a\u6570\u7a0b\u5e8f\u548c\u5e73\u53f0\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\u548c\u793a\u4f8b\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u5bf9\u5982\u4f55\u5728Python\u4e2d\u4fee\u6539CSV\u6587\u4ef6\u7684\u7f16\u7801\u6709\u4e86\u8f83\u4e3a\u5168\u9762\u7684\u4e86\u89e3\u3002\u65e0\u8bba\u662f\u4f7f\u7528pandas\u5e93\u3001csv\u6a21\u5757\u8fd8\u662f\u624b\u52a8\u5904\u7406\u6587\u4ef6\uff0c\u90fd\u5404\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u4e0d\u540c\u7f16\u7801\u7684CSV\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u8bfb\u53d6\u4e0d\u540c\u7f16\u7801\u7684CSV\u6587\u4ef6\u3002\u901a\u8fc7\u6307\u5b9a<code>encoding<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u89e3\u51b3\u7f16\u7801\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u8981\u8bfb\u53d6UTF-16\u7f16\u7801\u7684CSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = pd.read_csv(&#39;file.csv&#39;, encoding=&#39;utf-16&#39;)\n<\/code><\/pre>\n<p>\u6839\u636e\u9700\u8981\uff0c\u60a8\u53ef\u4ee5\u66f4\u6539<code>encoding<\/code>\u7684\u503c\uff0c\u4f8b\u5982<code>&#39;utf-8&#39;<\/code>\u3001<code>&#39;latin1&#39;<\/code>\u7b49\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u6587\u4ef6\u7f16\u7801\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u4fee\u6539CSV\u6587\u4ef6\u7f16\u7801\u540e\u5982\u4f55\u4fdd\u5b58\uff1f<\/strong><br \/>\u5728Pandas\u5e93\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u65b0\u7684\u7f16\u7801\u683c\u5f0f\u6765\u4fdd\u5b58CSV\u6587\u4ef6\u3002\u4f7f\u7528<code>to_csv<\/code>\u65b9\u6cd5\uff0c\u5e76\u4f20\u5165<code>encoding<\/code>\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u82e5\u5e0c\u671b\u5c06\u6587\u4ef6\u4fdd\u5b58\u4e3aUTF-8\u7f16\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">data.to_csv(&#39;new_file.csv&#39;, encoding=&#39;utf-8&#39;, index=False)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u786e\u4fdd\u6587\u4ef6\u4ee5\u6307\u5b9a\u7684\u7f16\u7801\u683c\u5f0f\u4fdd\u5b58\uff0c\u540c\u65f6<code>index=False<\/code>\u53ef\u4ee5\u907f\u514d\u5c06\u7d22\u5f15\u5217\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u6211\u7684CSV\u6587\u4ef6\u5728Excel\u4e2d\u6253\u5f00\u65f6\u663e\u793a\u4e71\u7801\uff1f<\/strong><br \/>\u5982\u679cCSV\u6587\u4ef6\u5728Excel\u4e2d\u6253\u5f00\u65f6\u51fa\u73b0\u4e71\u7801\uff0c\u901a\u5e38\u662f\u56e0\u4e3a\u6587\u4ef6\u7f16\u7801\u4e0eExcel\u9ed8\u8ba4\u7684\u7f16\u7801\u4e0d\u5339\u914d\u3002Excel\u901a\u5e38\u4f7f\u7528Windows-1252\u6216UTF-8\u7f16\u7801\u3002\u60a8\u53ef\u4ee5\u5c1d\u8bd5\u5c06CSV\u6587\u4ef6\u7684\u7f16\u7801\u8f6c\u6362\u4e3aExcel\u652f\u6301\u7684\u683c\u5f0f\uff0c\u4f8b\u5982UTF-8\uff0c\u4f7f\u7528Python\u4e2d\u7684Pandas\u5e93\u8fdb\u884c\u8f6c\u6362\uff0c\u5e76\u786e\u4fdd\u5728\u4fdd\u5b58\u65f6\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u6587\u4ef6\u5728\u4e0d\u540c\u8f6f\u4ef6\u4e2d\u7684\u517c\u5bb9\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001PYTHON\u4e2d\u4fee\u6539CSV\u7f16\u7801\u7684\u65b9\u6cd5 \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3001csv\u6a21\u5757\u3001\u6216\u8005\u624b\u52a8\u5904\u7406\u6587\u4ef6 [&hellip;]","protected":false},"author":3,"featured_media":959366,"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\/959357"}],"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=959357"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959357\/revisions"}],"predecessor-version":[{"id":959370,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959357\/revisions\/959370"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/959366"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=959357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=959357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=959357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}