{"id":90,"date":"2026-08-06T00:28:12","date_gmt":"2026-08-05T16:28:12","guid":{"rendered":"https:\/\/ltrainyard.icu\/?p=90"},"modified":"2026-08-06T00:44:06","modified_gmt":"2026-08-05T16:44:06","slug":"day-12-csv-%e4%b8%8e-json%ef%bc%9a%e5%ba%8f%e5%88%97%e5%8c%96%e4%b8%8e%e6%95%b0%e6%8d%ae%e5%ad%98%e5%8f%96","status":"publish","type":"post","link":"https:\/\/ltrainyard.icu\/index.php\/2026\/08\/06\/day-12-csv-%e4%b8%8e-json%ef%bc%9a%e5%ba%8f%e5%88%97%e5%8c%96%e4%b8%8e%e6%95%b0%e6%8d%ae%e5%ad%98%e5%8f%96\/","title":{"rendered":"| Day 12 | CSV \u4e0e JSON\uff1a\u5e8f\u5217\u5316\u4e0e\u6570\u636e\u5b58\u53d6 |"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1 \u6570\u636e\u6301\u4e45\u5316 \u2014\u2014 \u8ba9\u7a0b\u5e8f\u4e0d\u518d\u5931\u5fc6<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6301\u4e45\u5316<\/strong>\uff08persistence\uff09\u5c31\u662f\u628a\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u5b58\u5728\u5185\u5b58\u91cc\u7684\u6570\u636e\uff0c\u4fdd\u5b58\u5230\u786c\u76d8\u4e0a\uff0c\u8ba9\u7a0b\u5e8f\u5173\u95ed\u4e4b\u540e\u6570\u636e\u4f9d\u7136\u5b58\u5728\u3002\u5185\u5b58\u7684\u81f4\u547d\u7f3a\u70b9\u662f<strong>\u65ad\u7535\u5373\u5931<\/strong>\u2014\u2014\u7a0b\u5e8f\u4e00\u5173\uff0c\u6240\u6709\u53d8\u91cf\u3001\u5217\u8868\u3001\u5b57\u5178\u5168\u90e8\u6e05\u96f6\u3002\u5b66\u4f1a\u5b83\uff0c\u4f60\u7684\u7a0b\u5e8f\u624d\u771f\u6b63\u5f00\u59cb\u300c\u79ef\u7d2f\u6570\u636e\u300d\uff0c\u8fd9\u662f\u4ece\u300c\u73a9\u5177\u7a0b\u5e8f\u300d\u8d70\u5411\u300c\u5b9e\u7528\u5de5\u5177\u300d\u7684\u5206\u6c34\u5cad\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a0b\u5e8f\u8fd0\u884c\u65f6\u6570\u636e\u5b58\u5728\u5185\u5b58\uff08RAM\uff09\u91cc\uff0c\u8bbf\u95ee\u6781\u5feb\u4f46\u65ad\u7535\u6d88\u5931\uff1b\u786c\u76d8\u4e0a\u7684\u6587\u4ef6\u53ef\u4ee5\u6c38\u4e45\u4fdd\u5b58\u3002\u6301\u4e45\u5316\u7684\u672c\u8d28\u662f\uff1a<strong>\u628a\u5185\u5b58\u4e2d\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7ffb\u8bd1\u6210\u4e00\u79cd\u80fd\u5199\u6210\u6587\u4ef6\u3001\u53c8\u80fd\u4ece\u6587\u4ef6\u8bfb\u56de\u6765\u7684\u683c\u5f0f<\/strong>\u2014\u2014\u7ffb\u8bd1\u8fc7\u53bb\u53eb\u300c\u5e8f\u5217\u5316\u300d\uff0c\u7ffb\u8bd1\u56de\u6765\u53eb\u300c\u53cd\u5e8f\u5217\u5316\u300d\u3002\u4eca\u5929\u5b66\u7684 CSV \u548c JSON \u5c31\u662f\u4e24\u79cd\u300c\u7ffb\u8bd1\u683c\u5f0f\u300d\uff0c<code>csv<\/code> \u548c <code>json<\/code> \u6a21\u5757\u5c31\u662f\u300c\u7ffb\u8bd1\u5b98\u300d\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u672f\u8bed\u5c0f\u8bcd\u5178\uff1a<strong>\u5e8f\u5217\u5316<\/strong>\uff08serialization\uff09\u5c31\u662f\u628a\u5185\u5b58\u91cc\u7684\u5bf9\u8c61\u300c\u7ffb\u8bd1\u6210\u80fd\u5b58\u8fdb\u6587\u4ef6\u7684\u6587\u672c\u300d\uff1b<strong>\u53cd\u5e8f\u5217\u5316<\/strong>\uff08deserialization\uff09\u5c31\u662f\u53cd\u8fc7\u6765\u300c\u4ece\u6587\u672c\u8fd8\u539f\u6210\u5bf9\u8c61\u300d\u3002<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6301\u4e45\u5316\u50cf<strong>\u5199\u65e5\u8bb0 vs \u8bb0\u5728\u8111\u5b50\u91cc<\/strong>\uff1a\u4e0d\u5199\u65e5\u8bb0\uff0c\u7761\u4e00\u89c9\uff08\u7a0b\u5e8f\u5173\u95ed\uff09\u5c31\u5168\u5fd8\u4e86\uff1b\u5199\u8fdb\u65e5\u8bb0\u672c\uff08\u6587\u4ef6\uff09\uff0c\u660e\u5929\u7ffb\u5f00\u8fd8\u8bb0\u5f97\u3002CSV \u548c JSON \u662f\u4e24\u79cd\u300c\u65e5\u8bb0\u672c\u300d\uff1aCSV \u662f\u8868\u683c\uff0cJSON \u662f\u5e26\u5c42\u7ea7\u7684\u5361\u7247\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u6ca1\u6709\u6301\u4e45\u5316\u7684\u7a0b\u5e8f\u2014\u2014\u6570\u636e\u7528\u5b8c\u5c31\u6ca1\u4e86<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>students = &#91;\"\u5f20\u4e09\", \"\u674e\u56db\"]\nprint(\"\u7a0b\u5e8f\u8fd0\u884c\u4e2d\uff1a\", students)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7a0b\u5e8f\u8fd0\u884c\u4e2d\uff1a &#91;'\u5f20\u4e09', '\u674e\u56db']\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5217\u8868\u5b58\u5728\u5185\u5b58\u91cc\uff0c\u7a0b\u5e8f\u4e00\u7ed3\u675f\u5185\u5b58\u88ab\u56de\u6536\uff0c\u6570\u636e\u5f52\u96f6\u2014\u2014\u4e0b\u6b21\u8fd0\u884c\u53c8\u8981\u4ece\u5934\u8f93\u5165\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u6709\u6301\u4e45\u5316\u7684\u7a0b\u5e8f\u2014\u2014\u6570\u636e\u5b58\u8fdb\u6587\u4ef6\uff0c\u4e0b\u6b21\u8fd8\u80fd\u8bfb\u56de\u6765<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"memo.txt\", \"w\", encoding=\"utf-8\") as f:\n    f.write(\"\u5f20\u4e09\\n\u674e\u56db\\n\")\n\nwith open(\"memo.txt\", \"r\", encoding=\"utf-8\") as f:\n    restored = f.read()\nprint(restored)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5f20\u4e09\n\u674e\u56db\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e00\u6bb5 <code>\"w\"<\/code> \u6a21\u5f0f\u628a\u6570\u636e\u5199\u8fdb\u6587\u4ef6\uff0c\u7b2c\u4e8c\u6bb5 <code>\"r\"<\/code> \u6a21\u5f0f\u8bfb\u56de\u53d8\u91cf\u3002\u7a0b\u5e8f\u5173\u6389\u518d\u6253\u5f00\uff0c\u53ea\u8981\u6587\u4ef6\u8fd8\u5728\uff0c\u6570\u636e\u5c31\u8fd8\u5728\u2014\u2014\u8fd9\u5c31\u662f\u300c\u5b58-\u8bfb\u300d\u95ed\u73af\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u53ea\u5199\u4e0d\u8bfb\uff1a\u5199\u5b8c\u6587\u4ef6\u540e\u4ece\u4e0d\u9a8c\u8bc1 \u2192 \u95ee\u9898\uff1a\u5b58\u7684\u65f6\u5019\u62a5\u9519\u4e86\u4f60\u90fd\u4e0d\u77e5\u9053 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5199\u5b8c\u6587\u4ef6\u540e\u9a6c\u4e0a\u8bfb\u4e00\u904d\u9a8c\u8bc1\uff08\u95ed\u73af\u6d4b\u8bd5\uff09\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u7528 <code>\"w\"<\/code> \u6a21\u5f0f\u53cd\u590d\u5199\u540c\u4e00\u4e2a\u6587\u4ef6 \u2192 \u95ee\u9898\uff1a\u6bcf\u6b21\u6253\u5f00\u90fd\u4f1a<strong>\u6e05\u7a7a\u539f\u5185\u5bb9<\/strong>\uff0c\u65e7\u6570\u636e\u88ab\u8986\u76d6 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u8981\u4fdd\u7559\u65e7\u6570\u636e\u6539\u7528 <code>\"a\"<\/code> \u8ffd\u52a0\uff0c\u6216\u6362\u6587\u4ef6\u540d\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u4ee5\u4e3a\u6570\u636e\u5b58\u8fdb\u53d8\u91cf\u5c31\u300c\u4fdd\u5b58\u300d\u4e86 \u2192 \u95ee\u9898\uff1a\u53d8\u91cf\u5728\u5185\u5b58\u91cc\uff0c\u7a0b\u5e8f\u4e00\u5173\u5c31\u6ca1\u4e86 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u4fdd\u5b58 = \u5199\u5230\u6587\u4ef6\uff08<code>open<\/code> + \u5199\u64cd\u4f5c\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u5185\u5b58\u65ad\u7535\u5c31\u6e05\u96f6\uff0c\u5199\u8fdb\u6587\u4ef6\u624d\u4fdd\u547d\uff1b\u5b58\u8bfb\u95ed\u73af\u8981\u9a8c\u8bc1\uff0c\u5199\u5b8c\u9a6c\u4e0a\u8bfb\u4e00\u904d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2 CSV \u2014\u2014 \u7ed9\u8868\u683c\u6570\u636e\u51c6\u5907\u7684\u901a\u7528\u683c\u5f0f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>CSV<\/strong>\uff08Comma-Separated Values\uff0c\u9017\u53f7\u5206\u9694\u503c\uff09\uff1a\u7528\u9017\u53f7\u628a\u300c\u4e00\u884c\u7684\u5404\u4e2a\u5355\u5143\u683c\u300d\u5206\u9694\u5f00\u7684\u7eaf\u6587\u672c\u8868\u683c\u683c\u5f0f\uff0c\u7b2c\u4e00\u884c\u5e38\u662f\u8868\u5934\uff08\u5217\u540d\uff09\uff0c\u540e\u9762\u6bcf\u884c\u662f\u4e00\u6761\u8bb0\u5f55\u3002\u5b83\u7684\u4f18\u52bf\u662f<strong>\u6781\u7b80\u3001\u901a\u7528<\/strong>\uff1aExcel\u3001WPS\u3001\u4efb\u4f55\u7f16\u7a0b\u8bed\u8a00\u90fd\u80fd\u76f4\u63a5\u5904\u7406\u3002\u6210\u7ee9\u8868\u3001\u8ba2\u5355\u8868\u3001\u65e5\u5fd7\u5bfc\u51fa\u90fd\u7ed5\u4e0d\u5f00\u5b83\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">CSV \u6587\u4ef6\u672c\u8d28\u5c31\u662f\u4e00\u4e2a<strong>\u7eaf\u6587\u672c\u6587\u4ef6<\/strong>\uff1a\u6bcf\u884c\u662f\u4e00\u6761\u8bb0\u5f55\uff0c\u884c\u5185\u5355\u5143\u683c\u7528\u9017\u53f7\u9694\u5f00\u3002\u96be\u70b9\u5728\u300c\u8fb9\u754c\u60c5\u51b5\u300d\uff1a\u5982\u679c\u5355\u5143\u683c\u5185\u5bb9\u672c\u8eab\u542b\u9017\u53f7\uff08\u5982\u5907\u6ce8&#8221;\u559c\u6b22,\u7bee\u7403&#8221;\uff09\uff0c\u76f4\u63a5\u6309\u9017\u53f7\u5207\u5206\u5c31\u4f1a\u5207\u9519\u3002\u6240\u4ee5 <code>csv<\/code> \u6a21\u5757\u5199\u5165\u65f6\u81ea\u52a8\u7ed9\u7279\u6b8a\u5185\u5bb9\u52a0\u5f15\u53f7\uff0c\u8bfb\u53d6\u65f6\u81ea\u52a8\u8fd8\u539f\u2014\u2014\u4f60\u4e0d\u7528\u624b\u52a8\u5904\u7406\u8fd9\u4e9b\u7ec6\u8282\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">CSV \u50cf<strong>\u8d85\u5e02\u7684\u8d2d\u7269\u5c0f\u7968<\/strong>\uff1a\u4e00\u884c\u4e00\u884c\u6253\uff0c\u6bcf\u9879\u7528\u9017\u53f7\u9694\u5f00\uff0c\u673a\u5668\uff08Excel\uff09\u62ff\u5230\u5c0f\u7968\u5c31\u80fd\u8bc6\u522b\u6bcf\u4e00\u5217\u3002\u5c31\u7b97\u67d0\u9879\u91cc\u542b\u6709\u9017\u53f7\uff0c\u6253\u5370\u673a\u4f1a\u7528\u5f15\u53f7\u628a\u5b83\u300c\u5305\u8d77\u6765\u300d\u9632\u6b62\u8ba4\u9519\u2014\u2014<code>csv<\/code> \u6a21\u5757\u5c31\u662f\u8fd9\u53f0\u81ea\u52a8\u6253\u5370\u673a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>csv.writer \u5199\u6587\u4ef6 + csv.reader \u8bfb\u6587\u4ef6\uff08\u6700\u57fa\u7840\u7684\u4e00\u5bf9\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import csv\nwith open(\"students.csv\", \"w\", newline=\"\", encoding=\"utf-8\") as f:\n    writer = csv.writer(f)\n    writer.writerow(&#91;\"\u59d3\u540d\", \"\u5e74\u9f84\"])      # \u8868\u5934\uff1a\u4e00\u5217\u4e00\u4e2a\u540d\u5b57\n    writer.writerow(&#91;\"\u5f20\u4e09\", 18])\n    writer.writerow(&#91;\"\u674e\u56db\", 19])\nwith open(\"students.csv\", \"r\", encoding=\"utf-8\") as f:\n    for row in csv.reader(f):\n        print(row)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'\u59d3\u540d', '\u5e74\u9f84']\n&#91;'\u5f20\u4e09', '18']\n&#91;'\u674e\u56db', '19']\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>csv.writer(f)<\/code> \u521b\u5efa\u5199\u5165\u5668\uff0c<code>writerow([...])<\/code> \u6bcf\u8c03\u7528\u4e00\u6b21\u5199\u4e00\u884c\uff1b<code>csv.reader(f)<\/code> \u7528 <code>for<\/code> \u5faa\u73af\u9010\u884c\u62ff\u5230<strong>\u5217\u8868<\/strong>\u3002<code>newline=\"\"<\/code> \u662f Windows \u4e0a\u9632\u6b62\u591a\u4f59\u7a7a\u884c\u7684\u56fa\u5b9a\u642d\u914d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u5355\u5143\u683c\u91cc\u542b\u9017\u53f7\u600e\u4e48\u529e\u2014\u2014csv \u6a21\u5757\u81ea\u52a8\u7528\u5f15\u53f7\u5305\u8d77\u6765<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import csv\nwith open(\"names.csv\", \"w\", newline=\"\", encoding=\"utf-8\") as f:\n    writer = csv.writer(f)\n    writer.writerow(&#91;\"\u59d3\u540d\", \"\u5907\u6ce8\"])\n    writer.writerow(&#91;\"\u5c0f\u660e\", \"\u559c\u6b22,\u7bee\u7403\"])   # \u5907\u6ce8\u91cc\u6709\u4e00\u4e2a\u82f1\u6587\u9017\u53f7\uff01\nwith open(\"names.csv\", \"r\", encoding=\"utf-8\") as f:\n    for row in csv.reader(f):\n        print(row)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'\u59d3\u540d', '\u5907\u6ce8']\n&#91;'\u5c0f\u660e', '\u559c\u6b22,\u7bee\u7403']\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>csv.writer<\/code> \u68c0\u6d4b\u5230\u5185\u5bb9\u542b\u9017\u53f7\uff0c\u81ea\u52a8\u52a0\u5f15\u53f7\uff08\u6253\u5f00\u6587\u4ef6\u80fd\u770b\u5230 <code>\"\u559c\u6b22,\u7bee\u7403\"<\/code>\uff09\uff1b\u8bfb\u53d6\u65f6\u81ea\u52a8\u53bb\u6389\u5f15\u53f7\u3002\u81ea\u5df1\u7528 <code>split(\",\")<\/code> \u5207\u4f1a\u5207\u6210\u4e09\u4e2a\u5355\u5143\u683c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>DictWriter \/ DictReader\u2014\u2014\u7528\u300c\u5217\u540d\u300d\u8bfb\u5199\uff0c\u4e0d\u518d\u8bb0\u4e0b\u6807<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import csv\n\nwith open(\"scores.csv\", \"w\", newline=\"\", encoding=\"utf-8\") as f:\n    writer = csv.DictWriter(f, fieldnames=&#91;\"\u59d3\u540d\", \"\u8bed\u6587\", \"\u6570\u5b66\"])\n    writer.writeheader()                     # \u7528\u5217\u540d\u5217\u8868\u81ea\u52a8\u5199\u8868\u5934\n    writer.writerow({\"\u59d3\u540d\": \"\u5f20\u4e09\", \"\u8bed\u6587\": 90, \"\u6570\u5b66\": 85})\n\nwith open(\"scores.csv\", \"r\", encoding=\"utf-8\") as f:\n    for row in csv.DictReader(f):\n        print(row&#91;\"\u59d3\u540d\"], \"\u7684\u8bed\u6587\u6210\u7ee9\uff1a\", row&#91;\"\u8bed\u6587\"])\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5f20\u4e09 \u7684\u8bed\u6587\u6210\u7ee9\uff1a 90\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>DictWriter<\/code> \u5199\u5165\u65f6\u7528<strong>\u5b57\u5178<\/strong>\uff0c\u5217\u540d\u7531 <code>fieldnames<\/code> \u7edf\u4e00\u58f0\u660e\uff0c<code>writeheader()<\/code> \u81ea\u52a8\u5199\u8868\u5934\uff1b<code>DictReader<\/code> \u8bfb\u53d6\u65f6\u6bcf\u884c\u662f<strong>\u5b57\u5178<\/strong>\uff0c\u7528\u5217\u540d <code>row[\"\u59d3\u540d\"]<\/code> \u53d6\u503c\u3002\u8bfb\u51fa\u6765\u7684\u503c\u5168\u662f<strong>\u5b57\u7b26\u4e32<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5199 CSV \u65f6\u6f0f\u6389 <code>newline=\"\"<\/code> \u2192 \u95ee\u9898\uff1aWindows \u4e0a\u6bcf\u884c\u4e4b\u95f4\u591a\u4e00\u4e2a\u7a7a\u884c \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5199 CSV \u56fa\u5b9a <code>open(file, \"w\", newline=\"\", encoding=\"utf-8\")<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u76f4\u63a5\u5bf9\u6587\u4ef6\u5bf9\u8c61\u7528 <code>split(\",\")<\/code> \u5207\u884c \u2192 \u95ee\u9898\uff1a\u5355\u5143\u683c\u5185\u542b\u9017\u53f7\u65f6\u88ab\u9519\u8bef\u5207\u5206 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u4e00\u5f8b\u7528 <code>csv.reader<\/code> \/ <code>csv.writer<\/code> \u5904\u7406\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u8bfb\u53d6\u540e\u76f4\u63a5 <code>row[1] + 1<\/code> \u505a\u8fd0\u7b97 \u2192 \u95ee\u9898\uff1a\u8bfb\u51fa\u6765\u662f\u5b57\u7b26\u4e32 <code>'18'<\/code>\uff0c\u5b57\u7b26\u4e32\u52a0\u6570\u5b57\u62a5\u9519 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5148\u8f6c\u6362\uff1a<code>int(row[1])<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u5199\u5e26 newline\uff0c\u8bfb\u7528 for \u884c\uff1b\u9017\u53f7\u542b\u7279\u6b8a\uff0c\u6a21\u5757\u81ea\u52a8\u5305\uff1b\u5217\u540d\u597d\u8bb0\u7528 Dict\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3 JSON \u2014\u2014 \u5d4c\u5957\u6570\u636e\u7684\u901a\u7528\u683c\u5f0f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>JSON<\/strong>\uff08JavaScript Object Notation\uff0cJavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\uff1a\u7528\u300c\u82b1\u62ec\u53f7 + \u5192\u53f7\u300d\u63cf\u8ff0\u5d4c\u5957\u6570\u636e\u7684\u6587\u672c\u683c\u5f0f\uff0c\u8bed\u6cd5\u548c Python \u7684\u5b57\u5178 \/ \u5217\u8868\u51e0\u4e4e\u4e00\u6837\u3002\u5b83\u662f\u73b0\u4ee3\u7a0b\u5e8f\u4e4b\u95f4\u4ea4\u6362\u6570\u636e\u7684\u4e8b\u5b9e\u6807\u51c6\uff1a\u7f51\u9875\u548c\u670d\u52a1\u5668\u3001\u914d\u7f6e\u6587\u4ef6\u3001\u722c\u866b\u6570\u636e\u5168\u90fd\u7528\u5b83\u3002\u5b66 JSON\uff0c\u4f60\u7684\u7a0b\u5e8f\u5c31\u80fd\u300c\u8bf4\u5168\u4e16\u754c\u7684\u901a\u7528\u8bed\u8a00\u300d\uff0c\u8fd8\u80fd\u628a\u4efb\u610f\u5d4c\u5957\u7ed3\u6784\u5b8c\u6574\u5730\u5b58\u8fdb\u6587\u4ef6\u518d\u8bfb\u56de\u6765\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u53ea\u6709 6 \u79cd\u6570\u636e\u7c7b\u578b\uff1a\u5bf9\u8c61 <code>{}<\/code>\uff08\u5bf9\u5e94 Python \u5b57\u5178\uff09\u3001\u6570\u7ec4 <code>[]<\/code>\uff08\u5bf9\u5e94\u5217\u8868\uff09\u3001\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\uff08<code>true<\/code> \/ <code>false<\/code>\uff09\u3001\u7a7a\u503c <code>null<\/code>\u3002<code>json<\/code> \u6a21\u5757\u8d1f\u8d23\u300c\u7ffb\u8bd1\u300d\uff1a<code>dumps<\/code> \/ <code>dump<\/code> \u628a Python \u5bf9\u8c61\u7ffb\u8bd1\u6210 JSON \u6587\u672c\uff0c<code>loads<\/code> \/ <code>load<\/code> \u7ffb\u8bd1\u56de\u6765\u3002<strong>\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u76f4\u63a5\u628a\u5bf9\u8c61\u5199\u8fdb\u6587\u4ef6\uff1f<\/strong> \u6587\u4ef6\u53ea\u80fd\u5b58\u6587\u672c\uff0c\u5b57\u5178\u3001\u5217\u8868\u662f\u5185\u5b58\u91cc\u7684\u590d\u6742\u7ed3\u6784\uff0c\u5fc5\u987b\u5148\u300c\u7ffb\u8bd1\u6210\u6587\u672c\u300d\u624d\u80fd\u843d\u76d8\u3002\u6ce8\u610f\uff1aJSON \u7684\u5b57\u7b26\u4e32\u548c\u952e\u540d<strong>\u5fc5\u987b\u7528\u53cc\u5f15\u53f7<\/strong>\u2014\u2014\u8fd9\u662f\u65b0\u624b\u6700\u5bb9\u6613\u8e29\u7684\u5751\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u50cf<strong>\u5feb\u9012\u9762\u5355<\/strong>\uff1a\u5bc4\u4ef6\u4fe1\u606f\u3001\u6536\u4ef6\u4fe1\u606f\u3001\u7269\u54c1\u6e05\u5355\u90fd\u6309\u56fa\u5b9a\u683c\u5f0f\u586b\u5728\u4e00\u5f20\u5355\u5b50\u4e0a\uff08\u5d4c\u5957\uff1a\u9762\u5355\u91cc\u53c8\u5206\u300c\u5bc4\u4ef6\u4fe1\u606f\u300d\u300c\u6536\u4ef6\u4fe1\u606f\u300d\u4e24\u5757\uff09\u3002\u5feb\u9012\u516c\u53f8\uff08\u53e6\u4e00\u4e2a\u7a0b\u5e8f\uff09\u62ff\u5230\u9762\u5355\uff0c\u6309\u683c\u5f0f\u5c31\u80fd\u770b\u61c2\u5168\u90e8\u4fe1\u606f\u2014\u2014JSON \u5c31\u662f\u7a0b\u5e8f\u4e4b\u95f4\u7684\u300c\u901a\u7528\u9762\u5355\u300d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>dumps \/ loads\u2014\u2014\u5b57\u7b26\u4e32\u4e0e\u5bf9\u8c61\u7684\u4e92\u8f6c<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\nstudent = {\"name\": \"\u5f20\u4e09\", \"age\": 18}\ntext = json.dumps(student, ensure_ascii=False)\nprint(text)\nprint(type(text))\nback = json.loads(text)\nprint(back&#91;\"name\"], back&#91;\"age\"] + 2)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"name\": \"\u5f20\u4e09\", \"age\": 18}\n&lt;class 'str'&gt;\n\u5f20\u4e09 20\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>json.dumps<\/code> \u628a\u5b57\u5178\u7ffb\u8bd1\u6210 JSON \u5b57\u7b26\u4e32\uff08\u952e\u540d\u53d8\u53cc\u5f15\u53f7\uff09\uff1b<code>ensure_ascii=False<\/code> \u8ba9\u4e2d\u6587\u539f\u6837\u663e\u793a\uff1b<code>json.loads<\/code> \u628a\u5b57\u7b26\u4e32\u7ffb\u8bd1\u56de\u5b57\u5178\uff0c<code>back[\"age\"] + 2<\/code> \u8bc1\u660e\u5b83\u80fd\u50cf\u666e\u901a\u5b57\u5178\u4e00\u6837\u8fd0\u7b97\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>dump \/ load\u2014\u2014\u76f4\u63a5\u5199\u6587\u4ef6\u548c\u8bfb\u6587\u4ef6\uff08\u6301\u4e45\u5316\u6807\u51c6\u59ff\u52bf\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\nstudents = &#91;{\"name\": \"\u5f20\u4e09\", \"score\": 90}, {\"name\": \"\u674e\u56db\", \"score\": 78}]\n\nwith open(\"students.json\", \"w\", encoding=\"utf-8\") as f:\n    json.dump(students, f, ensure_ascii=False, indent=2)\n\nwith open(\"students.json\", \"r\", encoding=\"utf-8\") as f:\n    loaded = json.load(f)\n\nprint(type(loaded))\nprint(loaded&#91;1]&#91;\"name\"])\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;class 'list'&gt;\n\u674e\u56db\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>json.dump(students, f, ...)<\/code> \u628a\u6574\u4e2a\u5217\u8868\u5199\u8fdb\u6587\u4ef6\uff08\u4e0d\u7528\u52a0 <code>newline=\"\"<\/code>\uff09\uff1b<code>json.load(f)<\/code> \u4ece\u6587\u4ef6\u8bfb\u56de\uff0c\u7c7b\u578b\u4ecd\u662f <code>list<\/code>\u3002\u5b58\u8fdb\u53bb\u4ec0\u4e48\u6837\uff0c\u8bfb\u51fa\u6765\u8fd8\u662f\u4ec0\u4e48\u6837\u2014\u2014\u8fd9\u662f JSON \u6301\u4e45\u5316\u6700\u7701\u5fc3\u7684\u5730\u65b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>\u5d4c\u5957\u6570\u636e\u2014\u2014JSON \u7684\u771f\u6b63\u4f18\u52bf<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\ndata = {\n    \"\u73ed\u7ea7\": \"Python \u73ed\",\n    \"\u5b66\u751f\": &#91;\n        {\"name\": \"\u5f20\u4e09\", \"scores\": &#91;90, 85]},\n        {\"name\": \"\u674e\u56db\", \"scores\": &#91;78, 92]},\n    ],\n}\ntext = json.dumps(data, ensure_ascii=False)   # \u4e0d\u5e26 indent\uff0c\u8f93\u51fa\u7d27\u51d1\u7248\nprint(text)\nback = json.loads(text)\nprint(back&#91;\"\u5b66\u751f\"]&#91;0]&#91;\"scores\"]&#91;1])\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"\u73ed\u7ea7\": \"Python \u73ed\", \"\u5b66\u751f\": &#91;{\"name\": \"\u5f20\u4e09\", \"scores\": &#91;90, 85]}, {\"name\": \"\u674e\u56db\", \"scores\": &#91;78, 92]}]}\n85\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u300c\u5b57\u5178\u5957\u5217\u8868\u3001\u5217\u8868\u5957\u5b57\u5178\u300d\u7684\u4e09\u5c42\u5d4c\u5957\uff0cCSV \u8868\u8fbe\u4e0d\u4e86\uff0cJSON \u5374\u539f\u6837\u4fdd\u7559\u3002\u60f3\u8981\u6574\u9f50\u7684\u5c42\u7ea7\u7f29\u8fdb\uff0c\u52a0\u4e0a <code>indent=2<\/code> \u5373\u53ef\u3002<strong>\u5c42\u7ea7\u8d8a\u6df1\uff0cJSON \u7684\u4f18\u52bf\u8d8a\u5927\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5199 JSON \u65f6\u5fd8\u4e86 <code>ensure_ascii=False<\/code> \u2192 \u95ee\u9898\uff1a\u4e2d\u6587\u5168\u53d8\u6210 <code>\\u5f20<\/code> \u8f6c\u4e49\u7801\uff0c\u4eba\u6ca1\u6cd5\u8bfb \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>json.dump(..., ensure_ascii=False)<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u624b\u5199 JSON \u7528\u4e86\u5355\u5f15\u53f7\uff08<code>{'name': '\u5f20\u4e09'}<\/code>\uff09 \u2192 \u95ee\u9898\uff1a<code>json.loads<\/code> \u62a5 <code>JSONDecodeError<\/code>\uff0cJSON \u6807\u51c6\u8981\u6c42\u53cc\u5f15\u53f7 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u624b\u5199 JSON \u4e00\u5f8b\u7528\u53cc\u5f15\u53f7\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u6df7\u6dc6 <code>dump<\/code> \/ <code>dumps<\/code> \u2192 \u95ee\u9898\uff1a<code>dump<\/code> \u8981\u6587\u4ef6\u5bf9\u8c61\uff0c<code>dumps<\/code> \u8981\u5b57\u7b26\u4e32\uff0c\u4f20\u9519\u62a5\u9519 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5e26 <code>s<\/code>\uff08string\uff09\u7684\u5904\u7406\u5b57\u7b26\u4e32\uff0c\u4e0d\u5e26 <code>s<\/code> \u7684\u5904\u7406\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1adumps \u51fa\u5b57\u7b26\u4e32\uff0cloads \u5403\u5b57\u7b26\u4e32\uff1bdump \u8fdb\u6587\u4ef6\uff0cload \u51fa\u6587\u4ef6\uff1b\u4e2d\u6587\u52a0 False\uff0c\u7f29\u8fdb\u5199 indent\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4 CSV vs JSON \u2014\u2014 \u600e\u4e48\u9009\uff1f\u600e\u4e48\u4e92\u76f8\u8f6c\u6362\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b66\u4e86\u4e24\u79cd\u683c\u5f0f\uff0c\u6700\u5b9e\u9645\u7684\u95ee\u9898\u662f\uff1a<strong>\u8fd9\u4efd\u6570\u636e\u5230\u5e95\u8be5\u7528 CSV \u8fd8\u662f JSON\uff1f<\/strong> \u9009\u9519\u4e86\uff0c\u8981\u4e48\u5d4c\u5957\u7ed3\u6784\u585e\u4e0d\u8fdb CSV\uff0c\u8981\u4e48\u7b80\u5355\u8868\u683c\u767d\u767d\u589e\u52a0\u590d\u6742\u5ea6\u3002\u8fd9\u4e00\u8282\u5148\u5b66\u4f1a\u300c\u6309\u6570\u636e\u7ed3\u6784\u9009\u683c\u5f0f\u300d\uff0c\u518d\u5b66\u4f1a\u300cCSV \u8bfb\u8fdb\u6765 \u2192 JSON \u5b58\u51fa\u53bb\u300d\u7684\u8f6c\u6362\u5957\u8def\u2014\u2014\u300c\u8868\u683c\u6570\u636e\u8fdb\u7cfb\u7edf\uff0cJSON \u5b58\u5185\u90e8\u300d\u662f\u771f\u5b9e\u9879\u76ee\u91cc\u6781\u5e38\u89c1\u7684\u9700\u6c42\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e24\u8005\u7684\u672c\u8d28\u533a\u522b\u5728\u300c\u7ef4\u5ea6\u300d\uff1aCSV \u662f<strong>\u4e8c\u7ef4\u5e73\u9762<\/strong>\u2014\u2014\u53ea\u6709\u300c\u884c \u00d7 \u5217\u300d\uff0c\u4e00\u683c\u53ea\u80fd\u653e\u4e00\u4e2a\u503c\uff1bJSON \u662f<strong>\u4efb\u610f\u6df1\u5ea6<\/strong>\u2014\u2014\u901a\u8fc7\u5bf9\u8c61\u548c\u6570\u7ec4\u65e0\u9650\u5d4c\u5957\u3002\u9009\u62e9\u89c4\u5219\u5f88\u6734\u7d20\uff1a<strong>\u6570\u636e\u662f\u89c4\u6574\u8868\u683c \u2192 CSV\uff1b\u6570\u636e\u6709\u5d4c\u5957 \u2192 JSON<\/strong>\u3002\u8f6c\u6362\u4e5f\u4e0d\u96be\uff1aCSV \u8bfb\u51fa\u6765\u662f\u300c\u884c\u5217\u8868 \/ \u5b57\u5178\u5217\u8868\u300d\uff0cJSON \u5b58\u7684\u4e5f\u662f\u300c\u5217\u8868\u5957\u5b57\u5178\u300d\uff0c\u7ed3\u6784\u4e00\u81f4\uff0c\u76f4\u63a5\u300c\u8bfb A \u5199 B\u300d\u5c31\u662f\u8f6c\u6362\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">CSV \u50cf<strong>Excel \u8868\u683c<\/strong>\uff1a\u6a2a\u5e73\u7ad6\u76f4\uff0c\u9002\u5408\u6574\u9f50\u6570\u636e\uff1bJSON \u50cf<strong>\u6587\u4ef6\u5939\u5957\u6587\u4ef6\u5939<\/strong>\uff1a\u5f20\u4e09\u662f\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u91cc\u9762\u518d\u653e\u300c\u8bed\u6587\u3001\u6570\u5b66\u300d\u4e24\u4e2a\u5b50\u6587\u4ef6\u5939\uff0c\u5c42\u7ea7\u5206\u660e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u5224\u65ad\u7528\u54ea\u79cd\u683c\u5f0f\u2014\u2014\u5148\u770b\u7ed3\u6784\u518d\u9009<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rows = &#91;{\"name\": \"\u5f20\u4e09\", \"age\": 18}, {\"name\": \"\u674e\u56db\", \"age\": 19}]\nnested = {\"\u5b66\u751f\": &#91;{\"name\": \"\u5f20\u4e09\", \"scores\": {\"\u8bed\u6587\": 90}}, {\"name\": \"\u674e\u56db\", \"scores\": {\"\u8bed\u6587\": 78}}]}\nprint(\"rows \u662f\u89c4\u6574\u8868\u683c \u2192 \u7528 CSV\uff1bnested \u6709\u5d4c\u5957\u5c42\u7ea7 \u2192 \u7528 JSON\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>rows<\/code> \u662f\u5e73\u94fa\u7684\u300c\u59d3\u540d + \u5e74\u9f84\u300d\uff0c\u4e24\u5217\u5c31\u80fd\u653e\u4e0b\u2014\u2014CSV\uff1b<code>nested<\/code> \u91cc\u5b66\u751f\u5957\u7740 scores \u5b57\u5178\uff0c\u662f\u4e24\u5c42\u7ed3\u6784\u2014\u2014JSON\u3002\u5224\u65ad\u53e3\u8bc0\uff1a<strong>\u80fd\u5426\u7528\u300c\u884c \u00d7 \u5217\u300d\u6446\u5e73\uff1f\u80fd\u5c31 CSV\uff0c\u4e0d\u80fd\u5c31 JSON<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>CSV \u2192 JSON \u8f6c\u6362\uff08\u8868\u683c\u6570\u636e\u8f6c\u5d4c\u5957\u5b58\u50a8\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import csv\nimport json\n\nrows = &#91;{\"name\": \"\u5f20\u4e09\", \"age\": 18}, {\"name\": \"\u674e\u56db\", \"age\": 19}]\n\nwith open(\"data.csv\", \"w\", newline=\"\", encoding=\"utf-8\") as f:\n    writer = csv.DictWriter(f, fieldnames=&#91;\"name\", \"age\"])\n    writer.writeheader()\n    writer.writerows(rows)\n\nwith open(\"data.json\", \"w\", encoding=\"utf-8\") as f:\n    json.dump(rows, f, ensure_ascii=False, indent=2)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f6c\u6362\u7684\u672c\u8d28\u5c31\u662f\u300c<strong>\u540c\u4e00\u4efd Python \u6570\u636e\uff0c\u6362\u4e00\u79cd\u683c\u5f0f\u843d\u76d8<\/strong>\u300d\u2014\u2014\u5148\u8bfb\u8fdb\u5185\u5b58\uff0c\u518d\u6309\u76ee\u6807\u683c\u5f0f\u5199\u51fa\u53bb\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u628a\u5d4c\u5957\u6570\u636e\u786c\u585e\u8fdb CSV\uff08<code>writerow([scores\u5b57\u5178])<\/code>\uff09 \u2192 \u95ee\u9898\uff1a\u5b57\u5178\u88ab str \u6210\u4e00\u6bb5\u96be\u770b\u7684\u6587\u672c\uff0c\u8bfb\u56de\u6765\u65e0\u6cd5\u8fd8\u539f \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5d4c\u5957\u7ed3\u6784\u7528 JSON\uff0c\u53ea\u6709\u5e73\u94fa\u8868\u683c\u624d\u7528 CSV\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u8f6c\u6362\u65f6\u76f4\u63a5 <code>csv.reader<\/code> \u8bfb \u2192 \u5199 JSON \u2192 \u95ee\u9898\uff1a\u8bfb\u51fa\u6765\u7684\u5355\u5143\u683c\u5168\u662f\u5b57\u7b26\u4e32\uff0c\u6570\u5b57\u53d8\u6587\u672c \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u8f6c\u6362\u524d\u628a\u9700\u8981\u7684\u5217 <code>int()<\/code> \/ <code>float()<\/code> \u8f6c\u56de\u6570\u5b57\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5fd8\u4e86 CSV \u8868\u5934\u5bf9\u9f50\uff08\u5217\u6570\u4e0d\u4e00\u81f4\uff09 \u2192 \u95ee\u9898\uff1aExcel \u6253\u5f00\u65f6\u6570\u636e\u9519\u5217\uff0c\u65e0\u6cd5\u89e3\u6790 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>DictWriter<\/code> + <code>fieldnames<\/code> \u56fa\u5b9a\u5217\u540d\u548c\u987a\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u5e73\u94fa\u8868\u683c\u7528 CSV\uff0c\u5d4c\u5957\u7ed3\u6784\u7528 JSON\uff1b\u8f6c\u6362\u5148\u8bfb\u8fdb\u5185\u5b58\uff0c\u518d\u6362\u683c\u5f0f\u5199\u51fa\u53bb\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5 \u52a8\u624b\u5b9e\u8df5\uff1a\u6210\u7ee9\u8868\u5bfc\u5165\u5bfc\u51fa\u5de5\u5177<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u505a\u4e00\u4e2a\u300c\u6210\u7ee9\u8868\u5de5\u5177\u300d\uff1a\u80fd\u5f55\u5165\u5b66\u751f\u6210\u7ee9\u3001\u5c55\u793a\u5168\u90e8\u6210\u7ee9\u3001\u5bfc\u51fa\u6210 CSV\uff08\u7ed9 Excel \u7528\uff09\u3001\u4fdd\u5b58\u6210 JSON\uff08\u7ed9\u7a0b\u5e8f\u7528\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># score_tool.py \u2014\u2014 \u6210\u7ee9\u8868\u5bfc\u5165\u5bfc\u51fa\u5de5\u5177\nimport csv\nimport json\n\nstudents = &#91;]   # \u5185\u5b58\u6570\u636e\uff1a\u6bcf\u4e2a\u5b66\u751f\u4e00\u4e2a\u5b57\u5178\n\ndef add_student():\n    name = input(\"\u59d3\u540d\uff1a\")\n    chinese = int(input(\"\u8bed\u6587\uff1a\"))\n    math = int(input(\"\u6570\u5b66\uff1a\"))\n    students.append({\"name\": name, \"\u8bed\u6587\": chinese, \"\u6570\u5b66\": math})\n    print(f\"\u5df2\u6dfb\u52a0 {name}\")\n\ndef show_all():\n    if not students:\n        print(\"\u6682\u65e0\u6570\u636e\")\n        return\n    print(f\"{'\u59d3\u540d':&lt;6}{'\u8bed\u6587':&lt;6}{'\u6570\u5b66':&lt;6}\")\n    for s in students:\n        print(f\"{s&#91;'name']:&lt;6}{s&#91;'\u8bed\u6587']:&lt;6}{s&#91;'\u6570\u5b66']:&lt;6}\")\n\ndef export_csv():\n    with open(\"scores_export.csv\", \"w\", newline=\"\", encoding=\"utf-8\") as f:\n        writer = csv.writer(f)\n        writer.writerow(&#91;\"\u59d3\u540d\", \"\u8bed\u6587\", \"\u6570\u5b66\"])\n        for s in students:\n            writer.writerow(&#91;s&#91;\"name\"], s&#91;\"\u8bed\u6587\"], s&#91;\"\u6570\u5b66\"]])\n    print(\"\u5df2\u5bfc\u51fa scores_export.csv\")\n\ndef export_json():\n    with open(\"scores_export.json\", \"w\", encoding=\"utf-8\") as f:\n        json.dump(students, f, ensure_ascii=False, indent=2)\n    print(\"\u5df2\u5bfc\u51fa scores_export.json\")\n\ndef main():\n    while True:\n        print(\"\\n1.\u6dfb\u52a0\u5b66\u751f  2.\u67e5\u770b\u5168\u90e8  3.\u5bfc\u51faCSV  4.\u5bfc\u51faJSON  5.\u9000\u51fa\")\n        choice = input(\"\u8bf7\u9009\u62e9\uff1a\")\n        if choice == \"1\":\n            add_student()\n        elif choice == \"2\":\n            show_all()\n        elif choice == \"3\":\n            export_csv()\n        elif choice == \"4\":\n            export_json()\n        elif choice == \"5\":\n            break\n        else:\n            print(\"\u65e0\u6548\u9009\u62e9\")\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u6548\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1.\u6dfb\u52a0\u5b66\u751f  2.\u67e5\u770b\u5168\u90e8  3.\u5bfc\u51faCSV  4.\u5bfc\u51faJSON  5.\u9000\u51fa\n\u8bf7\u9009\u62e9\uff1a1\n\u59d3\u540d\uff1a\u5f20\u4e09\n\u8bed\u6587\uff1a90\n\u6570\u5b66\uff1a85\n\u5df2\u6dfb\u52a0 \u5f20\u4e09\n\u8bf7\u9009\u62e9\uff1a2\n\u59d3\u540d    \u8bed\u6587    \u6570\u5b66\n\u5f20\u4e09    90    85\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bfc\u51fa\u540e\u7528 Excel \u6253\u5f00 CSV\u3001\u7528\u8bb0\u4e8b\u672c\u6253\u5f00 JSON\uff0c\u90fd\u80fd\u770b\u5230\u521a\u624d\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5347\u7ea7\u6311\u6218<\/strong>\uff1a\u52a0\u4e00\u4e2a\u300c\u4ece CSV \u5bfc\u5165\u300d\u529f\u80fd\uff0c\u628a <code>scores.csv<\/code> \u91cc\u7684\u6570\u636e\u8bfb\u56de <code>students<\/code> \u5217\u8868\uff08\u8bb0\u5f97\u628a\u5b57\u7b26\u4e32\u8f6c\u6210\u6570\u5b57\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4eca\u65e5\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6301\u4e45\u5316\u662f\u4ec0\u4e48\uff1a\u628a\u5185\u5b58\u6570\u636e\u5199\u8fdb\u6587\u4ef6\u3001\u7a0b\u5e8f\u91cd\u542f\u540e\u8bfb\u56de\u6765\uff08\u300c\u5b58-\u8bfb\u300d\u95ed\u73af\uff09\nCSV \u9002\u5408\u89c4\u6574\u8868\u683c\uff1a<code>csv.writer<\/code> \/ <code>reader<\/code> \u8bfb\u5199\uff0c<code>DictWriter<\/code> \/ <code>DictReader<\/code> \u7528\u5217\u540d\u8bfb\u5199\uff0c\u5199 CSV \u8bb0\u5f97 <code>newline=\"\"<\/code>\nCSV \u8bfb\u51fa\u6765\u7684\u5355\u5143\u683c\u5168\u662f\u5b57\u7b26\u4e32\uff0c\u8981\u8ba1\u7b97\u5fc5\u987b\u5148\u8f6c\u6570\u5b57\nJSON \u9002\u5408\u5d4c\u5957\u7ed3\u6784\uff1a<code>dumps<\/code> \/ <code>loads<\/code> \u7ba1\u5b57\u7b26\u4e32\uff0c<code>dump<\/code> \/ <code>load<\/code> \u7ba1\u6587\u4ef6\n\u5199 JSON \u8bb0\u5f97 <code>ensure_ascii=False<\/code>\uff08\u4e2d\u6587\u539f\u6837\uff09\u548c <code>indent=2<\/code>\uff08\u7f8e\u5316\u7f29\u8fdb\uff09\nCSV \u2194 JSON \u8f6c\u6362\uff1a\u5148\u8bfb\u8fdb\u5185\u5b58\uff08\u540c\u4e00\u4efd Python \u6570\u636e\uff09\uff0c\u518d\u6309\u76ee\u6807\u683c\u5f0f\u5199\u51fa\u53bb\n\u6210\u7ee9\u8868\u5de5\u5177\u53ef\u4ee5\u5f55\u5165\u3001\u67e5\u770b\u5e76\u5bfc\u51fa CSV \u548c JSON<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u62a5\u9519<\/th><th>\u539f\u56e0<\/th><th>\u4fee\u590d<\/th><\/tr><\/thead><tbody><tr><td><code>json.decoder.JSONDecodeError<\/code><\/td><td>JSON \u6587\u4ef6\u683c\u5f0f\u574f\u4e86\uff0c\u6216\u8bfb\u4e86\u4e00\u4e2a\u6839\u672c\u4e0d\u662f JSON \u7684\u6587\u4ef6<\/td><td>\u68c0\u67e5\u6587\u4ef6\u5185\u5bb9\uff0c\u6216\u7528\u5728\u7ebf\u6821\u9a8c\u5de5\u5177\u68c0\u67e5\u683c\u5f0f<\/td><\/tr><tr><td><code>KeyError: 'name'<\/code><\/td><td>\u5b57\u5178\u91cc\u6ca1\u6709\u8fd9\u4e2a\u952e\uff08\u5982 CSV \u8868\u5934\u5bf9\u4e0d\u4e0a\uff09<\/td><td>\u6253\u5370\u6570\u636e\u5148\u770b\u7ed3\u6784\uff0c\u786e\u8ba4\u952e\u540d\u4e00\u81f4<\/td><\/tr><tr><td><code>csv.Error: iterator should return strings<\/code><\/td><td>\u8bd5\u56fe\u628a\u6570\u5b57\u76f4\u63a5\u4ea4\u7ed9 writerow \u4e4b\u5916\u7684\u975e\u5b57\u7b26\u4e32\u6570\u636e<\/td><td>\u7edf\u4e00\u7528\u5b57\u7b26\u4e32\u6216\u786e\u4fdd\u6570\u636e\u662f\u5b57\u7b26\u4e32<\/td><\/tr><tr><td>\u5199 CSV \u540e Excel \u6253\u5f00\u6bcf\u884c\u4e2d\u95f4\u6709\u7a7a\u884c<\/td><td>\u6253\u5f00\u6587\u4ef6\u65f6\u5c11\u4e86 <code>newline=\"\"<\/code><\/td><td>\u5199 CSV \u65f6\u52a0\u4e0a <code>newline=\"\"<\/code><\/td><\/tr><tr><td>\u4e2d\u6587\u8bfb\u51fa\u6765\u53d8 <code>\\uXXXX<\/code><\/td><td>\u5199 JSON \u65f6\u6ca1\u52a0 <code>ensure_ascii=False<\/code><\/td><td><code>json.dump(..., ensure_ascii=False)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1 \u6570\u636e\u6301\u4e45\u5316 \u2014\u2014 \u8ba9\u7a0b\u5e8f\u4e0d\u518d\u5931\u5fc6 \u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f \u6301\u4e45\u5316\uff08persistence\uff09\u5c31\u662f\u628a\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u5b58 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[16,9],"tags":[17],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-python","category-code-dev","tag-python-day"],"_links":{"self":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/comments?post=90"}],"version-history":[{"count":2,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/90\/revisions\/141"}],"wp:attachment":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/media?parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/categories?post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/tags?post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}