{"id":204133,"date":"2025-05-29T09:02:01","date_gmt":"2025-05-29T01:02:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204133\/"},"modified":"2025-05-29T09:02:01","modified_gmt":"2025-05-29T01:02:01","slug":"python-%e5%af%bc%e5%87%ba%e5%95%86%e5%93%81%e8%af%a6%e6%83%85%e5%88%b0-csv-%e6%97%b6%e6%95%b0%e6%8d%ae%e9%94%99%e4%b9%b1%ef%bc%8c%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204133\/","title":{"rendered":"Python \u5bfc\u51fa\u5546\u54c1\u8be6\u60c5\u5230 CSV \u65f6\u6570\u636e\u9519\u4e71\uff0c\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5bfc\u51fa\u5546\u54c1\u8be6\u60c5\u5230 CSV \u65f6\u6570\u636e\u9519\u4e71\uff0c\u5982\u4f55\u89e3\u51b3\uff1f<\/h1>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241029\/1730187653672091852573b.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u5bfc\u51fa\u5546\u54c1\u8be6\u60c5\u5230 csv \u65f6\u6570\u636e\u9519\u4e71\u7684\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u4f7f\u7528 python \u4ece\u7f51\u7ad9\u722c\u53d6\u5546\u54c1\u8be6\u60c5\u5e76\u5bfc\u51fa\u5230 csv \u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5546\u54c1\u8be6\u60c5\u6570\u636e\u6ea2\u51fa\u5230\u4e0b\u4e00\u884c\u6216\u591a\u884c\u7684\u95ee\u9898\u3002\u8fd9\u662f\u4ec0\u4e48\u539f\u56e0\u9020\u6210\u7684\uff0c\u5982\u4f55\u89e3\u51b3\u5462\uff1f<\/p>\n<p><strong>\u539f\u56e0:<\/strong><\/p>\n<p>csv \u6587\u4ef6\u901a\u8fc7\u4ee5\u9017\u53f7\u5206\u9694\u5b57\u6bb5\u6765\u5b58\u50a8\u6570\u636e\u3002\u5982\u679c\u5546\u54c1\u8be6\u60c5\u5305\u542b\u5f15\u53f7 (&#8220;)\uff0c\u5c31\u4f1a\u4e0e csv \u4e2d\u4f5c\u4e3a\u5206\u9694\u7b26\u7684\u5f15\u53f7\u6df7\u6dc6\uff0c\u5bfc\u81f4\u6570\u636e\u9519\u4e71\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848:<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u9700\u8981\u5bf9\u5546\u54c1\u8be6\u60c5\u4e2d\u7684\u5f15\u53f7\u8fdb\u884c\u8f6c\u4e49\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5c06\u6bcf\u4e2a\u51fa\u73b0\u7684\u5f15\u53f7 (&#8221; &#8220;) \u66ff\u6362\u4e3a\u4e24\u4e2a\u5f15\u53f7 (&#8221; &#8220;)\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b:<\/strong><\/p>\n<pre># \u6253\u5f00 csv \u6587\u4ef6\u5e76\u5199\u5165\u5934\u4fe1\u606f\nwriter = csv.writer(open('\u5546\u54c1\u8be6\u60c5.csv', 'w'), delimiter=',')\nwriter.writerow(['\u5546\u54c1id', '\u540d\u79f0', '\u8be6\u60c5'])\n\n# \u5faa\u73af\u904d\u5386\u5546\u54c1\u5217\u8868\nfor product in products:\n    # \u5c06\u8be6\u60c5\u6309\u884c\u5206\u5272\n    details = product.detail.split('\\n')\n    # \u5c06\u6bcf\u4e2a\u8be6\u60c5\u4e2d\u7684\u5f15\u53f7\u8f6c\u4e49\n    details = [detail.replace('\"', '\"\"') for detail in details]\n    # \u5199\u5165 csv \u6587\u4ef6\n    writer.writerow([product.id, product.name, '\\n'.join(details)])<\/pre>\n<p><strong>\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879:<\/strong><\/p>\n<ul>\n<li>\u5982\u679c csv \u6587\u4ef6\u65e0\u6cd5\u6b63\u786e\u8bc6\u522b utf-8 \u7f16\u7801\uff0c\u53ef\u4ee5\u5728\u5199\u5165\u6587\u4ef6\u65f6\u6dfb\u52a0 bom \u5934\uff08\u5b57\u8282\u987a\u5e8f\u6807\u8bb0\uff09\uff1a<\/li>\n<\/ul>\n<pre>writer = csv.writer(open('\u5546\u54c1\u8be6\u60c5.csv', 'w', encoding='utf-8-sig'), delimiter=',')<\/pre>\n<ul>\n<li>\u786e\u4fdd\u5bfc\u51fa\u7684 csv \u6587\u4ef6\u4e0d\u4f1a\u5305\u542b\u989d\u5916\u7684\u7a7a\u884c\u6216\u7a7a\u767d\u5b57\u7b26\u3002\u8fd9\u4e9b\u5b57\u7b26\u4e5f\u4f1a\u5bfc\u81f4\u6570\u636e\u9519\u4e71\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPython \u5bfc\u51fa\u5546\u54c1\u8be6\u60c5\u5230 CSV \u65f6\u6570\u636e\u9519\u4e71\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5bfc\u51fa\u5546\u54c1\u8be6\u60c5\u5230 C&#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-204133","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204133","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=204133"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204133\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}