{"id":204147,"date":"2025-05-29T08:36:19","date_gmt":"2025-05-29T00:36:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204147\/"},"modified":"2025-05-29T08:36:19","modified_gmt":"2025-05-29T00:36:19","slug":"python%e7%88%ac%e8%99%ab%e5%af%bc%e5%87%bacsv%e6%95%b0%e6%8d%ae%e9%94%99%e4%b9%b1%ef%bc%9a%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%e5%95%86%e5%93%81%e8%af%a6%e6%83%85%e6%ba%a2%e5%87%ba%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204147\/","title":{"rendered":"Python\u722c\u866b\u5bfc\u51faCSV\u6570\u636e\u9519\u4e71\uff1a\u5982\u4f55\u89e3\u51b3\u5546\u54c1\u8be6\u60c5\u6ea2\u51fa\u95ee\u9898\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u722c\u866b\u5bfc\u51faCSV\u6570\u636e\u9519\u4e71\uff1a\u5982\u4f55\u89e3\u51b3\u5546\u54c1\u8be6\u60c5\u6ea2\u51fa\u95ee\u9898\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aPython\u722c\u866b\u5bfc\u51faCSV\u6570\u636e\u9519\u4e71\uff1a\u5982\u4f55\u89e3\u51b3\u5546\u54c1\u8be6\u60c5\u6ea2\u51fa\u95ee\u9898\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241029\/17302126086720f300039ef.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python\u722c\u866b\u5bfc\u51fa\u5546\u54c1\u8be6\u60c5\u5230csv\u51fa\u73b0\u9519\u4e71\u7684\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728\u4f7f\u7528python\u8fdb\u884c\u722c\u866b\u5e76\u5bfc\u51fa\u6570\u636e\u5230csv\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5546\u54c1\u8be6\u60c5\u5185\u5bb9\u6ea2\u51fa\u5230\u540e\u7eed\u884c\u7684\u60c5\u51b5\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u7531\u4e8e\u6570\u636e\u4e2d\u5305\u542b\u672a\u8f6c\u4e49\u7684\u53cc\u5f15\u53f7\uff08&#8221;\uff09\u5bfc\u81f4\u7684\u3002<\/p>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u8bf7\u91c7\u53d6\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u5c06\u6570\u636e\u4e2d\u7684\u53cc\u5f15\u53f7\u8f6c\u4e49\u3002<\/strong>\u628a\u6570\u636e\u4e2d\u7684\u6240\u6709\u53cc\u5f15\u53f7\uff08&#8221;\uff09\u66ff\u6362\u4e3a\u6210\u5bf9\u7684\u53cc\u5f15\u53f7\uff08&#8221;&#8221;\uff09\u3002\u4f8b\u5982\uff1a\u5c06&#8221;\u5546\u54c1\u540d\u79f0&#8221;\u66ff\u6362\u4e3a&#8221;&#8221;\u5546\u54c1\u540d\u79f0&#8221;&#8221;\u3002<\/li>\n<li><strong>\u5982\u679c\u8bc6\u522b\u4e0d\u4e86utf8\uff0c\u8bf7\u6dfb\u52a0bom\u5934\u3002<\/strong>bom\uff08\u5b57\u8282\u987a\u5e8f\u6807\u8bb0\uff09\u662f\u4e00\u4e2a\u7279\u6b8a\u5b57\u7b26\u5e8f\u5217\uff0c\u7528\u4e8e\u6807\u8bc6csv\u6587\u4ef6\u7684\u5b57\u7b26\u7f16\u7801\u3002\u5982\u679c\u60a8\u4f7f\u7528\u7684csv\u6587\u4ef6\u662futf-8\u7f16\u7801\u7684\uff0c\u4f46\u65e0\u6cd5\u6b63\u786e\u8bc6\u522b\uff0c\u8bf7\u5728\u6587\u4ef6\u7684\u5f00\u5934\u6dfb\u52a0\u4e00\u4e2abom\u5934\u4fe1\u606f\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4fee\u6539\u8fc7\u7684\u5bfc\u51fa\u4ee3\u7801\u793a\u4f8b\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u53cc\u5f15\u53f7\u8f6c\u4e49\u548cbom\u5934\uff1a<\/p>\n<pre>import csv\n\n# \u6253\u5f00csv\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4e00\u4e2abom\u5934\nwith open('\u5546\u54c1\u8be6\u60c5.csv', 'w', newline='') as f:\n    writer = csv.writer(f)\n    writer.writerow(['\\ufeff\u5546\u54c1\u8be6\u60c5'])\n\n    # \u5faa\u73af\u904d\u5386\u5546\u54c1\u8be6\u60c5\u6570\u636e\n    for detail in \u5546\u54c1\u8be6\u60c5\u5217\u8868:\n        # \u8f6c\u4e49\u53cc\u5f15\u53f7\n        detail = detail.replace('\"', '\"\"')\n        # \u5199\u5165\u6570\u636e\n        writer.writerow([detail])<\/pre>\n<p>\u901a\u8fc7\u6267\u884c\u6b64\u4fee\u6539\uff0c\u5bfc\u51fa\u7684csv\u6587\u4ef6\u5c06\u6b63\u786e\u5904\u7406\u53cc\u5f15\u53f7\uff0c\u5e76\u4e14\u5546\u54c1\u8be6\u60c5\u4e0d\u4f1a\u518d\u6ea2\u51fa\u5230\u540e\u7eed\u884c\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPython\u722c\u866b\u5bfc\u51faCSV\u6570\u636e\u9519\u4e71\uff1a\u5982\u4f55\u89e3\u51b3\u5546\u54c1\u8be6\u60c5\u6ea2\u51fa\u95ee\u9898\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u722c\u866b\u5bfc\u51faCSV\u6570\u636e\u9519&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204147","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204147"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204147\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}