{"id":94,"date":"2026-08-06T00:29:06","date_gmt":"2026-08-05T16:29:06","guid":{"rendered":"https:\/\/ltrainyard.icu\/?p=94"},"modified":"2026-08-06T00:44:21","modified_gmt":"2026-08-05T16:44:21","slug":"day-14-%e9%98%b6%e6%ae%b5%e4%ba%8c%e9%a1%b9%e7%9b%ae%ef%bc%9a%e5%ad%a6%e7%94%9f%e4%bf%a1%e6%81%af%e7%ae%a1%e7%90%86%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/ltrainyard.icu\/index.php\/2026\/08\/06\/day-14-%e9%98%b6%e6%ae%b5%e4%ba%8c%e9%a1%b9%e7%9b%ae%ef%bc%9a%e5%ad%a6%e7%94%9f%e4%bf%a1%e6%81%af%e7%ae%a1%e7%90%86%e7%b3%bb%e7%bb%9f\/","title":{"rendered":"| Day 14 | \u9636\u6bb5\u4e8c\u9879\u76ee\uff1a\u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf |"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1 \u9879\u76ee\u603b\u89c8 \u2014\u2014 \u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf\uff08\u9636\u6bb5\u4e8c\u6536\u5b98\uff09<\/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\">\u4eca\u5929\u662f\u9636\u6bb5\u4e8c\u6536\u5b98\u65e5\uff0c<strong>\u6ca1\u6709\u65b0\u77e5\u8bc6\u70b9<\/strong>\u2014\u2014\u91cd\u70b9\u662f\u628a Day 8~13 \u5b66\u7684\u96f6\u6563\u77e5\u8bc6\u7ec4\u88c5\u6210\u4e00\u4e2a\u300c\u771f\u6b63\u80fd\u7528\u7684\u300d\u63a7\u5236\u53f0\u7ba1\u7406\u7cfb\u7edf\u3002\u524d 13 \u5929\u5b66\u7684\u77e5\u8bc6\u4eca\u5929\u5168\u90e8\u7528\u4e0a\uff1a\u51fd\u6570\u7ec4\u7ec7\u4ee3\u7801\u3001\u5217\u8868+\u5b57\u5178\u5b58\u6570\u636e\u3001\u6587\u4ef6\u8bfb\u5199\u505a\u6301\u4e45\u5316\u3001\u5f02\u5e38\u5904\u7406\u515c\u5e95\u3002\u8fd9\u4e2a\u7cfb\u7edf\u505a\u5b8c\uff0c\u4f60\u5c31\u5177\u5907\u505a\u4e00\u4e2a\u300c\u80fd\u7528\u7684\u5de5\u5177\u300d\u7684\u80fd\u529b\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9636\u6bb5\u77e5\u8bc6\u5730\u56fe<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Day 8   \u51fd\u6570\u5165\u95e8\uff08def\/\u53c2\u6570\/\u8fd4\u56de\u503c\/\u4f5c\u7528\u57df\uff09 \u2500\u2500\u2500\u2500\u2500\u2500\u2510\nDay 9   \u51fd\u6570\u8fdb\u9636\uff08*args\/**kwargs\/lambda\/\u9012\u5f52\uff09 \u2500\u2500\u2510\u2502\nDay 10  \u6a21\u5757\u4e0e\u5305\uff08import\/\u6807\u51c6\u5e93\/\u81ea\u5b9a\u4e49\u6a21\u5757\uff09 \u2500\u2500\u2500\u2500\u2510\u2502\u2502\nDay 11  \u6587\u4ef6\u8bfb\u5199\uff08open\/with\/w\u4e0ea\/\u7f16\u7801\uff09 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u2502\u2502\u2502\nDay 12  CSV\u4e0eJSON\uff08csv\u6a21\u5757\/json\u56db\u51fd\u6570\/\u6301\u4e45\u5316\uff09 \u2500\u2510\u2502\u2502\u2502\u2502\nDay 13  \u5f02\u5e38\u5904\u7406\uff08try\/except\/raise\/\u81ea\u5b9a\u4e49\u5f02\u5e38\uff09 \u2500\u2510\u2502\u2502\u2502\u2502\u2502\n                                             \u2514\u2500\u2534\u2534\u2534\u2534\u2534\u2500\u2500\u2192 \u9636\u6bb5\u9879\u76ee\uff1a\u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u53e5\u8bdd\u4e32\u6cd5\uff1a<strong>\u51fd\u6570\u8ba9\u4ee3\u7801\u6a21\u5757\u5316 \u2192 \u53c2\u6570\u8ba9\u51fd\u6570\u901a\u7528 \u2192 \u4f5c\u7528\u57df\u7ba1\u597d\u53d8\u91cf \u2192 \u6a21\u5757\u8ba9\u4ee3\u7801\u62c6\u5206 \u2192 \u6587\u4ef6\u8bfb\u5199\u8ba9\u6570\u636e\u843d\u76d8 \u2192 CSV\/JSON \u8ba9\u6570\u636e\u6709\u7ed3\u6784 \u2192 \u5f02\u5e38\u5904\u7406\u8ba9\u7a0b\u5e8f\u625b\u5f97\u4f4f<\/strong>\u3002\u5b66\u751f\u7ba1\u7406\u7cfb\u7edf\u5c31\u662f\u628a\u8fd9\u4e9b\u96f6\u4ef6\u6309\u987a\u5e8f\u7ec4\u88c5\u8d77\u6765\uff1a\u51fd\u6570\u5f53\u9aa8\u67b6\u3001\u5217\u8868\u5b57\u5178\u5f53\u4ed3\u5e93\u3001JSON \u5f53\u4fdd\u9669\u67dc\u3001\u5f02\u5e38\u5904\u7406\u5f53\u5b89\u5168\u6c14\u56ca\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>\u9879\u76ee\u62c6\u89e3\u8868\uff08\u516d\u5927\u529f\u80fd \u2192 \u77e5\u8bc6\u70b9\uff09<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u529f\u80fd<\/th><th>\u7528\u5230\u7684\u77e5\u8bc6\u70b9<\/th><th>\u5177\u4f53\u5199\u6cd5<\/th><th>\u6765\u81ea\u54ea\u4e00\u5929<\/th><\/tr><\/thead><tbody><tr><td>\u6570\u636e\u6a21\u578b<\/td><td>\u5217\u8868 + \u5b57\u5178<\/td><td><code>students = []<\/code>\u3001<code>{\"name\": ..., \"age\": ..., \"score\": ...}<\/code><\/td><td>Day 6\/7<\/td><\/tr><tr><td>\u6dfb\u52a0\u5b66\u751f<\/td><td>\u51fd\u6570\u3001\u7c7b\u578b\u8f6c\u6362\u3001\u5f02\u5e38\u5904\u7406<\/td><td><code>int(input(\"\u5e74\u9f84\uff1a\"))<\/code>\u3001<code>except ValueError<\/code><\/td><td>Day 8\/2\/13<\/td><\/tr><tr><td>\u67e5\u770b\u5168\u90e8<\/td><td>\u5faa\u73af\u3001f-string \u683c\u5f0f\u5316<\/td><td><code>for s in students:<\/code>\u3001<code>f\"{s['name']:&lt;8}...\"<\/code><\/td><td>Day 5\/9<\/td><\/tr><tr><td>\u6309\u59d3\u540d\u67e5\u627e<\/td><td>\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/td><td><code>[s for s in students if s[\"name\"] == name]<\/code><\/td><td>Day 6\/7<\/td><\/tr><tr><td>\u5220\u9664\u5b66\u751f<\/td><td>\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5207\u7247\u8d4b\u503c<\/td><td><code>students[:] = [s for s in students if s[\"name\"] != name]<\/code><\/td><td>Day 6<\/td><\/tr><tr><td>\u4fdd\u5b58\u6570\u636e<\/td><td>\u51fd\u6570\u3001JSON \u6301\u4e45\u5316<\/td><td><code>json.dump(students, f, ensure_ascii=False, indent=2)<\/code><\/td><td>Day 8\/12<\/td><\/tr><tr><td>\u52a0\u8f7d\u6570\u636e<\/td><td>\u6a21\u5757\u3001\u6587\u4ef6\u5224\u65ad\u3001JSON\u3001\u5f02\u5e38\u5904\u7406<\/td><td><code>os.path.exists<\/code>\u3001<code>json.load<\/code>\u3001<code>except (json.JSONDecodeError, OSError)<\/code><\/td><td>Day 10\/12\/13<\/td><\/tr><tr><td>\u4e3b\u83dc\u5355<\/td><td>while \u5faa\u73af\u3001\u6761\u4ef6\u5224\u65ad<\/td><td><code>while True:<\/code> + <code>if\/elif<\/code> + <code>break<\/code><\/td><td>Day 4\/5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u5f02\u5e38\u9632\u62a4\u8868\uff08\u98ce\u9669\u573a\u666f \u2192 \u77e5\u8bc6\u70b9\uff09<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u98ce\u9669\u573a\u666f<\/th><th>\u9632\u62a4\u5199\u6cd5<\/th><th>\u77e5\u8bc6\u70b9<\/th><\/tr><\/thead><tbody><tr><td>\u5e74\u9f84 \/ \u6210\u7ee9\u8f93\u5165\u975e\u6570\u5b57<\/td><td><code>try: age = int(...)<\/code> + <code>except ValueError<\/code><\/td><td>Day 13 \u5f02\u5e38\u5904\u7406<\/td><\/tr><tr><td>\u6570\u636e\u6587\u4ef6\u4e0d\u5b58\u5728\uff08\u7b2c\u4e00\u6b21\u8fd0\u884c\uff09<\/td><td><code>if os.path.exists(DATA_FILE):<\/code><\/td><td>Day 10\/11 \u6a21\u5757\u4e0e\u6587\u4ef6<\/td><\/tr><tr><td>\u6570\u636e\u6587\u4ef6\u88ab\u624b\u52a8\u6539\u574f<\/td><td><code>except (json.JSONDecodeError, OSError)<\/code><\/td><td>Day 12\/13 JSON + \u5f02\u5e38<\/td><\/tr><tr><td>\u4fdd\u5b58\u540e\u4e2d\u6587\u53d8 <code>\\uXXXX<\/code><\/td><td><code>json.dump(..., ensure_ascii=False)<\/code><\/td><td>Day 12 JSON<\/td><\/tr><tr><td>\u51fd\u6570\u5185\u91cd\u65b0\u8d4b\u503c\u5168\u5c40\u5217\u8868<\/td><td><code>global students<\/code> \u58f0\u660e<\/td><td>Day 8 \u4f5c\u7528\u57df<\/td><\/tr><\/tbody><\/table><\/figure>\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\u4e00\u53e3\u6c14\u5199\u5b8c 100 \u884c\u4ee3\u7801\u518d\u8fd0\u884c\uff08\u300c\u618b\u5927\u62db\u300d\uff09 \u2192 \u95ee\u9898\uff1a\u5199\u4e86\u51e0\u5341\u884c\u6ca1\u8fd0\u884c\u8fc7\u4e00\u6b21\uff0c\u4e00\u8fd0\u884c\u5168\u662f\u9519\uff0c\u4e0d\u77e5\u9053\u4ece\u54ea\u6539\u8d77 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u4e09\u6b65\u8d70\uff0c\u6bcf\u6b65\u90fd\u80fd\u8fd0\u884c\u3001\u90fd\u80fd\u9a8c\u8bc1\uff08\u89c1\u7b2c 5 \u8282\uff09\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u9879\u76ee\u505a\u4e0d\u51fa\u6765\u5c31\u6000\u7591\u662f\u4eca\u5929\u7684\u95ee\u9898 \u2192 \u95ee\u9898\uff1a\u5f80\u5f80\u662f\u524d\u9762\u67d0\u5929\u57fa\u7840\u6ca1\u6253\u7262 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5bf9\u7167\u62c6\u89e3\u8868\u81ea\u67e5\u300c\u8fd9\u4e00\u6b65\u7528\u5230\u4e86\u54ea\u4e2a\u77e5\u8bc6\u70b9\u300d\uff0c\u56de\u53bb\u7ffb\u90a3\u4e00\u5929\u7684\u603b\u7ed3\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u51fd\u6570\u5f53\u9aa8\u67b6\uff0c\u5b57\u5178\u5f53\u4ed3\u5e93\uff1bJSON \u4fdd\u9669\u67dc\uff0c\u5f02\u5e38\u5b89\u5168\u6c14\u56ca\uff1b\u62c6\u89e3\u8868\u5bf9\u53f7\u5165\u5ea7\uff0c\u5c0f\u6b65\u8fed\u4ee3\u4e0d\u618b\u5927\u62db\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2 \u7cfb\u7edf\u8bbe\u8ba1 \u2014\u2014 \u5148\u60f3\u6e05\u695a\u518d\u52a8\u624b<\/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\">\u52a8\u624b\u5199\u4ee3\u7801\u524d\uff0c\u5148\u753b\u51fa\u529f\u80fd\u5730\u56fe\u2014\u2014\u8fd9\u5c31\u662f\u7a0b\u5e8f\u5458\u5e38\u8bf4\u7684\u300c\u5148\u8bbe\u8ba1\u518d\u7f16\u7801\u300d\u3002\u529f\u80fd\u8d8a\u591a\uff0c\u63d0\u524d\u753b\u56fe\u8d8a\u91cd\u8981\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>\u8bbe\u8ba1\u56db\u8981\u7d20<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u6a21\u578b<\/strong>\uff1a\u5b66\u751f\u5217\u8868 <code>students<\/code>\uff0c\u6bcf\u4e2a\u5b66\u751f\u662f\u4e00\u4e2a\u5b57\u5178\uff1a<code>{\"name\": \u59d3\u540d, \"age\": \u5e74\u9f84, \"score\": \u6210\u7ee9}<\/code><\/li>\n\n\n\n<li><strong>\u6570\u636e\u6301\u4e45\u5316<\/strong>\uff1a\u4fdd\u5b58\u5230 <code>students.json<\/code>\uff0c\u542f\u52a8\u65f6\u81ea\u52a8\u52a0\u8f7d<\/li>\n\n\n\n<li><strong>\u529f\u80fd\u5217\u8868<\/strong>\uff1a\u6dfb\u52a0\u3001\u67e5\u770b\u5168\u90e8\u3001\u6309\u59d3\u540d\u67e5\u627e\u3001\u5220\u9664\u3001\u4fdd\u5b58\u3001\u52a0\u8f7d<\/li>\n\n\n\n<li><strong>\u7ed3\u6784\u7ec4\u7ec7<\/strong>\uff1a\u6bcf\u4e2a\u529f\u80fd\u4e00\u4e2a\u51fd\u6570\uff0c<code>main()<\/code> \u91cc\u7528\u5faa\u73af + \u83dc\u5355\u8c03\u5ea6<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u6570\u636e\u683c\u5f0f\u7ea6\u5b9a<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\nimport os\n\nDATA_FILE = \"students.json\"\n\n# \u4e00\u540d\u5b66\u751f = \u4e00\u4e2a\u5b57\u5178\n# {\"name\": \"\u5f20\u4e09\", \"age\": 18, \"score\": 90}\n# \u6240\u6709\u5b66\u751f = \u5217\u8868\n# &#91;{\"name\": \"\u5f20\u4e09\", ...}, {\"name\": \"\u674e\u56db\", ...}]\n<\/code><\/pre>\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\u4e0d\u8bbe\u8ba1\u76f4\u63a5\u5f00\u5199 \u2192 \u95ee\u9898\uff1a\u5199\u5230\u4e00\u534a\u53d1\u73b0\u7ed3\u6784\u4e0d\u5bf9\uff0c\u8fd4\u5de5\u91cd\u6765 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5148\u753b\u300c\u6570\u636e\u6a21\u578b + \u529f\u80fd\u5217\u8868 + \u7ed3\u6784\u7ec4\u7ec7\u300d\uff0c\u518d\u5199\u4ee3\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u5148\u753b\u56fe\u518d\u52a8\u5de5\uff0c\u6570\u636e\u6a21\u578b\u5b9a\u57fa\u8c03\uff1b\u529f\u80fd\u5217\u8868\u4e00\u6761\u6761\uff0c\u51fd\u6570\u8c03\u5ea6\u4e0d\u4e71\u5957\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3 \u6570\u636e\u6a21\u578b\u4e0e\u4f5c\u7528\u57df \u2014\u2014 global \u7684\u5751<\/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\"><code>students<\/code> \u662f\u5168\u5c40\u5217\u8868\uff0c\u516d\u4e2a\u51fd\u6570\u5171\u4eab\u5b83\u3002\u4f46\u300c\u4fee\u6539\u5217\u8868\u5185\u5bb9\u300d\u548c\u300c\u91cd\u65b0\u8d4b\u503c\u300d\u5bf9 <code>global<\/code> \u7684\u8981\u6c42\u5b8c\u5168\u4e0d\u540c\u2014\u2014\u8fd9\u662f\u672c\u9879\u76ee\u6700\u5bb9\u6613\u8e29\u7684\u5751\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\"><code>students.append(...)<\/code> \u64cd\u4f5c\u7684\u662f\u5168\u5c40\u5217\u8868<strong>\u672c\u8eab<\/strong>\uff08\u5f80\u91cc\u9762\u585e\u5143\u7d20\uff09\uff0cPython \u80fd\u8ba4\u51fa\u8fd9\u662f\u5728\u4fee\u6539\u5df2\u6709\u7684\u5168\u5c40\u5bf9\u8c61\uff0c\u4e0d\u9700\u8981 <code>global<\/code>\u3002\u53cd\u8fc7\u6765\uff0c<code>load_data<\/code> \u91cc <code>students = json.load(f)<\/code> \u662f\u8ba9\u53d8\u91cf\u540d\u6307\u5411\u4e00\u4e2a<strong>\u5168\u65b0\u7684\u5217\u8868\u5bf9\u8c61<\/strong>\u2014\u2014\u4e0d\u58f0\u660e <code>global<\/code> \u7684\u8bdd\uff0cPython \u4f1a\u8ba4\u4e3a\u8fd9\u662f\u51fd\u6570\u91cc\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u8d4b\u503c\u5b8c\u51fd\u6570\u7ed3\u675f\u5c31\u4e22\u4e86\uff0c\u5168\u5c40 <code>students<\/code> \u8fd8\u662f\u7a7a\u5217\u8868\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>\u4fee\u6539\u5217\u8868\u5185\u5bb9 vs \u91cd\u65b0\u8d4b\u503c<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>students = &#91;]\n\ndef add_student():\n    students.append({\"name\": \"\u5f20\u4e09\"})   # \u4fee\u6539\u5217\u8868\u5185\u5bb9\uff1a\u4e0d\u9700\u8981 global\n\nadd_student()\nprint(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>&#91;{'name': '\u5f20\u4e09'}]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u91cd\u65b0\u8d4b\u503c\u5fc5\u987b global<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def load_data():\n    global students               # \u5fc5\u987b\u58f0\u660e\uff01\n    students = json.load(f)       # \u91cd\u65b0\u8d4b\u503c\uff1a\u8ba9\u53d8\u91cf\u6307\u5411\u5168\u65b0\u5bf9\u8c61\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3 **\u5220\u9664\u7528 <code>students[:] = ...<\/code> \u800c\u4e0d\u662f <code>students = ...<\/code>**\uff1a\u5207\u7247\u6574\u4f53\u66ff\u6362\u4fdd\u7559\u540c\u4e00\u4e2a\u5217\u8868\u5bf9\u8c61\uff0c\u6240\u6709\u5f15\u7528\u8fd9\u4e2a\u5217\u8868\u7684\u5730\u65b9\u770b\u5230\u7684\u90fd\u662f\u65b0\u5185\u5bb9\uff1b<code>students = ...<\/code> \u6362\u65b0\u5bf9\u8c61\uff0c\u65e7\u5bf9\u8c61\u4e0a\u7684\u4fee\u6539\u5c31\u4e22\u4e86\u3002\u5728\u591a\u4e2a\u51fd\u6570\u5171\u4eab\u540c\u4e00\u4e2a <code>students<\/code> \u7684\u573a\u666f\u4e0b\uff0c<code>[:]<\/code> \u5199\u6cd5\u66f4\u7a33\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<code>load_data<\/code> \u91cc <code>students = json.load(f)<\/code> \u5fd8\u4e86 <code>global<\/code> \u2192 \u95ee\u9898\uff1a\u62a5 <code>UnboundLocalError<\/code>\uff0c\u6216\u5168\u5c40 students \u8fd8\u662f\u7a7a\u5217\u8868 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u51fd\u6570\u5f00\u5934\u5199 <code>global students<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5220\u9664\u5b66\u751f\u7528 <code>students = [...]<\/code> \u2192 \u95ee\u9898\uff1a\u6362\u4e86\u65b0\u5bf9\u8c61\uff0c\u5176\u5b83\u51fd\u6570\u5bf9\u65e7\u5bf9\u8c61\u7684\u5f15\u7528\u5168\u90e8\u5931\u6548 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>students[:] = [...]<\/code> \u539f\u5730\u66ff\u6362\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u52a8\u65e7\u5bf9\u8c61\u4e0d\u7528 global\uff0c\u6362\u65b0\u5bf9\u8c61\u5fc5\u987b global\uff1b\u5220\u9664\u7528\u5207\u7247 [:]\uff0c\u539f\u5730\u66ff\u6362\u6700\u7a33\u5f53\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4 \u6301\u4e45\u5316\u4e0e\u5f02\u5e38\u9632\u62a4 \u2014\u2014 JSON \u4fdd\u9669\u67dc + \u5b89\u5168\u6c14\u56ca<\/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\">\u7cfb\u7edf\u7684\u300c\u5fc3\u810f\u300d\u662f\u4fdd\u5b58\u548c\u52a0\u8f7d\uff1a<code>save_data<\/code> \u7528 <code>json.dump<\/code> \u628a\u6574\u4e2a\u5b66\u751f\u5217\u8868\u5b58\u6210 <code>students.json<\/code>\uff0c<code>load_data<\/code> \u7528 <code>json.load<\/code> \u8bfb\u56de\u6765\u3002\u7a0b\u5e8f\u91cd\u542f\u540e\u6570\u636e\u8fd8\u5728\uff0c\u9760\u7684\u5c31\u662f\u8fd9\u4e00\u5bf9\u51fd\u6570\u3002\u540c\u65f6\uff0c\u5f02\u5e38\u5904\u7406\u8ba9\u7a0b\u5e8f\u5728\u975e\u6cd5\u8f93\u5165\u3001\u6587\u4ef6\u635f\u574f\u65f6\u4e5f\u4e0d\u95ea\u9000\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>\u4fdd\u5b58 \/ \u52a0\u8f7d\u7684\u6838\u5fc3\u4ee3\u7801<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def save_data():\n    with open(DATA_FILE, \"w\", encoding=\"utf-8\") as f:\n        json.dump(students, f, ensure_ascii=False, indent=2)\n    print(f\"\u5df2\u4fdd\u5b58 {len(students)} \u540d\u5b66\u751f\u5230 {DATA_FILE}\")\n\ndef load_data():\n    global students\n    if os.path.exists(DATA_FILE):\n        try:\n            with open(DATA_FILE, \"r\", encoding=\"utf-8\") as f:\n                students = json.load(f)\n            print(f\"\u5df2\u4ece {DATA_FILE} \u52a0\u8f7d {len(students)} \u540d\u5b66\u751f\")\n        except (json.JSONDecodeError, OSError) as e:\n            print(f\"\u52a0\u8f7d\u6587\u4ef6\u5931\u8d25\uff08{e}\uff09\uff0c\u4f7f\u7528\u7a7a\u6570\u636e\")\n    else:\n        print(\"\u6ca1\u6709\u6570\u636e\u6587\u4ef6\uff0c\u4ece\u7a7a\u5217\u8868\u5f00\u59cb\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong><code>add_student<\/code> \u7684\u6570\u5b57\u6821\u9a8c<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    age = int(input(\"\u5e74\u9f84\uff1a\"))\n    score = float(input(\"\u6210\u7ee9\uff1a\"))\nexcept ValueError:\n    print(\"\u5e74\u9f84\u548c\u6210\u7ee9\u5fc5\u987b\u662f\u6570\u5b57\uff0c\u6dfb\u52a0\u5931\u8d25\")\n    return\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>int(\"abc\")<\/code> \u4f1a\u629b <code>ValueError<\/code>\uff0c\u88ab <code>except ValueError<\/code> \u63a5\u4f4f\uff0c\u6253\u5370\u53cb\u597d\u63d0\u793a\u2014\u2014\u7a0b\u5e8f\u4e0d\u5d29\u6e83\uff0c\u7528\u6237\u91cd\u65b0\u8f93\u5165\u5373\u53ef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 **\u4e3a\u4ec0\u4e48 <code>except (json.JSONDecodeError, OSError)<\/code> \u800c\u4e0d\u662f\u88f8 <code>except:<\/code>**\uff1a\u7cbe\u786e\u6355\u83b7\u300c\u6587\u4ef6\u635f\u574f\u300d\u548c\u300c\u8bfb\u53d6\u5931\u8d25\u300d\u4e24\u7c7b\u5df2\u77e5\u98ce\u9669\uff1b\u88f8 <code>except:<\/code> \u4f1a\u628a\u6240\u6709\u5f02\u5e38\uff08\u5305\u62ec Ctrl+C \u7684 <code>KeyboardInterrupt<\/code>\uff09\u90fd\u541e\u6389\uff0c\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u7ec8\u6b62\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\u4fdd\u5b58\u65f6\u6f0f\u4e86 <code>ensure_ascii=False<\/code> \u2192 \u95ee\u9898\uff1a\u4e2d\u6587\u53d8 <code>\\u5f20<\/code> \u8f6c\u4e49\u7801\uff0c\u6587\u4ef6\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<code>load_data<\/code> \u7528\u88f8 <code>except:<\/code> \u2192 \u95ee\u9898\uff1aCtrl+C \u4e5f\u88ab\u541e\u6389\uff0c\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u7ec8\u6b62 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>except (json.JSONDecodeError, OSError)<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>add_student<\/code> \u4e0d\u52a0 try\/except \u2192 \u95ee\u9898\uff1a\u7528\u6237\u5e74\u9f84\u8f93\u5165 &#8220;abc&#8221;\uff0c\u7a0b\u5e8f\u76f4\u63a5\u5d29\u6e83\u9000\u51fa\uff0c\u4e4b\u524d\u5f55\u5165\u7684\u6570\u636e\u5168\u767d\u8d39 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>try\/except ValueError<\/code> \u6355\u83b7\u5e76\u63d0\u793a\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1ajson.dump \u5b58\u3001json.load \u53d6\uff0c\u91cd\u542f\u4e0d\u4e22\u9760\u95ed\u73af\uff1b\u975e\u6cd5\u8f93\u5165\u52a0\u9632\u62a4\uff0c\u6587\u4ef6\u635f\u574f\u4e0d\u95ea\u9000\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5 \u52a8\u624b\u5b9e\u8df5 \u2014\u2014 \u5b8c\u6574\u7684\u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f\u6574\u4e2a\u7cfb\u7edf\uff0c\u4e00\u4e2a\u6587\u4ef6\u76f4\u63a5\u8fd0\u884c\u3002\u5efa\u8bae\u5148\u901a\u8bfb\u4e00\u904d\uff0c\u518d\u7167\u7740\u6572\u4e00\u904d\u52a0\u6df1\u5370\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># student_manager.py \u2014\u2014 \u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf\nimport json\nimport os\n\nDATA_FILE = \"students.json\"\nstudents = &#91;]   # \u5168\u5c40\u6570\u636e\uff1a\u6240\u6709\u5b66\u751f\uff08\u6bcf\u4e2a\u5b66\u751f\u662f\u4e00\u4e2a\u5b57\u5178\uff09\n\n\ndef load_data():\n    \"\"\"\u4ece\u6587\u4ef6\u52a0\u8f7d\u5b66\u751f\u6570\u636e\uff0c\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u4fdd\u6301\u7a7a\u5217\u8868\"\"\"\n    global students\n    if os.path.exists(DATA_FILE):\n        try:\n            with open(DATA_FILE, \"r\", encoding=\"utf-8\") as f:\n                students = json.load(f)\n            print(f\"\u5df2\u4ece {DATA_FILE} \u52a0\u8f7d {len(students)} \u540d\u5b66\u751f\")\n        except (json.JSONDecodeError, OSError) as e:\n            print(f\"\u52a0\u8f7d\u6587\u4ef6\u5931\u8d25\uff08{e}\uff09\uff0c\u4f7f\u7528\u7a7a\u6570\u636e\")\n    else:\n        print(\"\u6ca1\u6709\u6570\u636e\u6587\u4ef6\uff0c\u4ece\u7a7a\u5217\u8868\u5f00\u59cb\")\n\n\ndef save_data():\n    \"\"\"\u628a\u5b66\u751f\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\"\"\"\n    with open(DATA_FILE, \"w\", encoding=\"utf-8\") as f:\n        json.dump(students, f, ensure_ascii=False, indent=2)\n    print(f\"\u5df2\u4fdd\u5b58 {len(students)} \u540d\u5b66\u751f\u5230 {DATA_FILE}\")\n\n\ndef add_student():\n    \"\"\"\u6dfb\u52a0\u4e00\u540d\u5b66\u751f\"\"\"\n    name = input(\"\u59d3\u540d\uff1a\").strip()\n    if not name:\n        print(\"\u59d3\u540d\u4e0d\u80fd\u4e3a\u7a7a\")\n        return\n    try:\n        age = int(input(\"\u5e74\u9f84\uff1a\"))\n        score = float(input(\"\u6210\u7ee9\uff1a\"))\n    except ValueError:\n        print(\"\u5e74\u9f84\u548c\u6210\u7ee9\u5fc5\u987b\u662f\u6570\u5b57\uff0c\u6dfb\u52a0\u5931\u8d25\")\n        return\n    students.append({\"name\": name, \"age\": age, \"score\": score})\n    print(f\"\u5df2\u6dfb\u52a0\uff1a{name}\")\n\n\ndef show_all():\n    \"\"\"\u67e5\u770b\u5168\u90e8\u5b66\u751f\"\"\"\n    if not students:\n        print(\"\u6682\u65e0\u5b66\u751f\u6570\u636e\")\n        return\n    print(f\"{'\u59d3\u540d':&lt;8}{'\u5e74\u9f84':&lt;6}{'\u6210\u7ee9':&lt;8}\")\n    for s in students:\n        print(f\"{s&#91;'name']:&lt;8}{s&#91;'age']:&lt;6}{s&#91;'score']:&lt;8}\")\n    print(f\"\u5171 {len(students)} \u4eba\")\n\n\ndef find_student():\n    \"\"\"\u6309\u59d3\u540d\u67e5\u627e\u5b66\u751f\"\"\"\n    name = input(\"\u8bf7\u8f93\u5165\u8981\u67e5\u627e\u7684\u59d3\u540d\uff1a\").strip()\n    found = &#91;s for s in students if s&#91;\"name\"] == name]\n    if not found:\n        print(f\"\u6ca1\u6709\u627e\u5230\u540d\u4e3a {name} \u7684\u5b66\u751f\")\n        return\n    for s in found:\n        print(f\"\u59d3\u540d\uff1a{s&#91;'name']}\uff0c\u5e74\u9f84\uff1a{s&#91;'age']}\uff0c\u6210\u7ee9\uff1a{s&#91;'score']}\")\n\n\ndef delete_student():\n    \"\"\"\u6309\u59d3\u540d\u5220\u9664\u5b66\u751f\uff08\u540c\u540d\u5168\u90e8\u5220\u9664\uff09\"\"\"\n    name = input(\"\u8bf7\u8f93\u5165\u8981\u5220\u9664\u7684\u59d3\u540d\uff1a\").strip()\n    before = len(students)\n    students&#91;:] = &#91;s for s in students if s&#91;\"name\"] != name]\n    removed = before - len(students)\n    if removed:\n        print(f\"\u5df2\u5220\u9664 {removed} \u540d {name}\")\n    else:\n        print(f\"\u6ca1\u6709\u627e\u5230\u540d\u4e3a {name} \u7684\u5b66\u751f\")\n\n\ndef main():\n    \"\"\"\u4e3b\u83dc\u5355\"\"\"\n    load_data()\n    menu = (\n        \"\\n===== \u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf =====\\n\"\n        \"1. \u6dfb\u52a0\u5b66\u751f\\n\"\n        \"2. \u67e5\u770b\u5168\u90e8\\n\"\n        \"3. \u6309\u59d3\u540d\u67e5\u627e\\n\"\n        \"4. \u5220\u9664\u5b66\u751f\\n\"\n        \"5. \u4fdd\u5b58\u5230\u6587\u4ef6\\n\"\n        \"6. \u4ece\u6587\u4ef6\u52a0\u8f7d\\n\"\n        \"0. \u9000\u51fa\\n\"\n        \"\u8bf7\u9009\u62e9\uff1a\"\n    )\n    while True:\n        choice = input(menu)\n        if choice == \"1\":\n            add_student()\n        elif choice == \"2\":\n            show_all()\n        elif choice == \"3\":\n            find_student()\n        elif choice == \"4\":\n            delete_student()\n        elif choice == \"5\":\n            save_data()\n        elif choice == \"6\":\n            load_data()\n        elif choice == \"0\":\n            save_data()   # \u9000\u51fa\u524d\u81ea\u52a8\u4fdd\u5b58\uff0c\u907f\u514d\u6570\u636e\u4e22\u5931\n            print(\"\u518d\u89c1\")\n            break\n        else:\n            print(\"\u65e0\u6548\u9009\u62e9\uff0c\u8bf7\u8f93\u5165 0-6\")\n\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>\u6ca1\u6709\u6570\u636e\u6587\u4ef6\uff0c\u4ece\u7a7a\u5217\u8868\u5f00\u59cb\n\n===== \u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf =====\n1. \u6dfb\u52a0\u5b66\u751f\n2. \u67e5\u770b\u5168\u90e8\n3. \u6309\u59d3\u540d\u67e5\u627e\n4. \u5220\u9664\u5b66\u751f\n5. \u4fdd\u5b58\u5230\u6587\u4ef6\n6. \u4ece\u6587\u4ef6\u52a0\u8f7d\n0. \u9000\u51fa\n\u8bf7\u9009\u62e9\uff1a1\n\u59d3\u540d\uff1a\u5f20\u4e09\n\u5e74\u9f84\uff1a18\n\u6210\u7ee9\uff1a90\n\u5df2\u6dfb\u52a0\uff1a\u5f20\u4e09\n\u8bf7\u9009\u62e9\uff1a2\n\u59d3\u540d      \u5e74\u9f84    \u6210\u7ee9\n\u5f20\u4e09      18    90.0\n\u5171 2 \u4eba\n\u8bf7\u9009\u62e9\uff1a0\n\u5df2\u4fdd\u5b58 2 \u540d\u5b66\u751f\u5230 students.json\n\u518d\u89c1\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e8c\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u4f1a\u81ea\u52a8\u52a0\u8f7d <code>students.json<\/code>\uff0c\u521a\u624d\u7684\u5b66\u751f\u8fd8\u5728\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u503c\u5f97\u6ce8\u610f\u7684\u7ec6\u8282<\/strong>\uff1a<code>students[:] = ...<\/code> \u7528\u5207\u7247\u6574\u4f53\u66ff\u6362\uff1b\u5220\u9664\u529f\u80fd\u4f1a\u5220\u6389\u6240\u6709\u540c\u540d\u5b66\u751f\uff1b\u9000\u51fa\u65f6\u81ea\u52a8\u4fdd\u5b58 + \u63d0\u4f9b\u624b\u52a8\u4fdd\u5b58\u53cc\u4fdd\u9669\uff1b<code>load_data<\/code> \u6355\u83b7 JSON \u635f\u574f\u4e0d\u5d29\u6e83\uff1b\u7528 <code>if __name__ == \"__main__\"<\/code> \u4fdd\u62a4\u5165\u53e3\uff0c\u4e4b\u540e\u53ef\u4ee5\u88ab\u5176\u5b83\u6a21\u5757\u5bfc\u5165\u590d\u7528\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6 \u5f00\u53d1\u6d41\u7a0b \u2014\u2014 \u4e09\u6b65\u642d\u8d77\u4f60\u7684\u7cfb\u7edf<\/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\">\u4e0d\u8981\u4e00\u53e3\u6c14\u5199\u5b8c 100 \u884c\u4ee3\u7801\u2014\u2014\u65b0\u624b\u6700\u5e38\u89c1\u7684\u5931\u8d25\u65b9\u5f0f\u5c31\u662f\u300c\u618b\u5927\u62db\u300d\uff1a\u5199\u4e86\u51e0\u5341\u884c\u6ca1\u8fd0\u884c\u8fc7\u4e00\u6b21\uff0c\u4e00\u8fd0\u884c\u5168\u662f\u9519\uff0c\u6839\u672c\u4e0d\u77e5\u9053\u4ece\u54ea\u6539\u8d77\u3002\u6b63\u786e\u505a\u6cd5\u662f<strong>\u4e09\u6b65\u8d70\uff0c\u6bcf\u6b65\u90fd\u80fd\u8fd0\u884c\u3001\u90fd\u80fd\u9a8c\u8bc1<\/strong>\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>\u7b2c 1 \u6b65\uff1a\u642d\u9aa8\u67b6\u2014\u2014\u5148\u8ba9\u83dc\u5355\u8dd1\u8d77\u6765<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add_student():\n    pass\n\ndef show_all():\n    pass\n\ndef main():\n    while True:\n        choice = input(\"\u8bf7\u9009\u62e9\uff080 \u9000\u51fa\uff09\uff1a\")\n        if choice == \"0\":\n            break\n        elif choice == \"1\":\n            add_student()\n        elif choice == \"2\":\n            show_all()\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\"><code>pass<\/code> \u662f\u300c\u5360\u4f4d\u8bed\u53e5\u300d\u2014\u2014\u51fd\u6570\u4f53\u6682\u65f6\u7a7a\u7740\u4e5f\u80fd\u901a\u8fc7\u8bed\u6cd5\u68c0\u67e5\u3002<strong>\u8fd9\u4e00\u6b65\u7684\u610f\u4e49\uff1a\u5148\u628a\u300c\u7a0b\u5e8f\u7684\u5f62\u72b6\u300d\u7acb\u8d77\u6765\uff0c\u9519\u8bef\u8303\u56f4\u6700\u5c0f\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u7b2c 2 \u6b65\uff1a\u586b\u7a7a\u8179\u2014\u2014\u5148\u505a\u300c\u6dfb\u52a0\u300d\u548c\u300c\u67e5\u770b\u300d<\/strong>\uff1a\u628a <code>add_student<\/code> \u548c <code>show_all<\/code> \u7684 <code>pass<\/code> \u6362\u6210\u771f\u5b9e\u903b\u8f91\uff0c\u8dd1\u4e00\u904d\uff1a\u80fd\u6dfb\u52a0\u5b66\u751f\u3001\u80fd\u5217\u8868\u5c55\u793a\u3002\u505a\u5b8c\u8fd9\u4e24\u6b65\uff0c\u7cfb\u7edf\u5df2\u7ecf\u6709\u300c\u6838\u5fc3\u4ef7\u503c\u300d\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>\u7b2c 3 \u6b65\uff1a\u52a0\u6301\u4e45\u5316\u548c\u9632\u62a4\u2014\u2014\u4fdd\u5b58\/\u52a0\u8f7d + \u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u6700\u540e\u8865\u4e0a <code>save_data<\/code> \/ <code>load_data<\/code> \/ <code>find_student<\/code> \/ <code>delete_student<\/code>\uff0c\u518d\u7ed9\u8f93\u5165\u52a0\u4e0a <code>try \/ except ValueError<\/code>\u3002\u8fd9\u6b65\u662f\u300c\u9526\u4e0a\u6dfb\u82b1 + \u52a0\u56fa\u300d\uff0c\u5373\u4f7f\u51fa\u9519\u4e5f\u4e0d\u5f71\u54cd\u524d\u4e24\u6b65\u5df2\u7ecf\u9a8c\u8bc1\u8fc7\u7684\u529f\u80fd\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\u4e00\u6b21\u5199\u5b8c\u6240\u6709\u529f\u80fd\u518d\u8fd0\u884c \u2192 \u95ee\u9898\uff1a\u51fa\u9519\u8303\u56f4\u5927\uff0c\u96be\u4ee5\u5b9a\u4f4d \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u6bcf\u5b8c\u6210\u4e00\u6b65\u5c31\u8dd1\u4e00\u6b21\u3001\u9a8c\u8bc1\u4e00\u6b21\u2014\u2014\u51fa\u95ee\u9898\u8303\u56f4\u5c0f\u3001\u597d\u5b9a\u4f4d\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u8df3\u8fc7\u9aa8\u67b6\u76f4\u63a5\u5199\u529f\u80fd \u2192 \u95ee\u9898\uff1a\u83dc\u5355\u903b\u8f91\u548c\u529f\u80fd\u903b\u8f91\u6df7\u5728\u4e00\u8d77 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5148\u8ba9\u83dc\u5355\u5faa\u73af\u8dd1\u901a\uff08\u7528 pass \u5360\u4f4d\uff09\uff0c\u518d\u9010\u4e2a\u586b\u7a7a\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u5148\u9aa8\u67b6\u540e\u586b\u7a7a\uff0c\u518d\u52a0\u56fa\uff1b\u6bcf\u6b65\u90fd\u8fd0\u884c\uff0c\u5c0f\u6b65\u5feb\u8fed\u4ee3\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\">\u9636\u6bb5\u77e5\u8bc6\u5730\u56fe\uff1a\u51fd\u6570 \u2192 \u53c2\u6570\/\u4f5c\u7528\u57df \u2192 \u6a21\u5757 \u2192 \u6587\u4ef6\u8bfb\u5199 \u2192 CSV\/JSON \u2192 \u5f02\u5e38\u5904\u7406\uff0c\u4e00\u73af\u6263\u4e00\u73af\n\u7cfb\u7edf\u8bbe\u8ba1\u5148\u884c\uff1a\u5148\u753b\u300c\u6570\u636e\u6a21\u578b + \u529f\u80fd\u5217\u8868 + \u7ed3\u6784\u7ec4\u7ec7\u300d\uff0c\u518d\u52a8\u624b\u5199\u4ee3\u7801\n\u6570\u636e\u6a21\u578b\uff1a\u4e00\u4e2a\u5217\u8868\u5b58\u6240\u6709\u5b66\u751f\uff0c\u4e00\u4e2a\u5b57\u5178\uff08<code>name<\/code> \/ <code>age<\/code> \/ <code>score<\/code>\uff09\u5b58\u4e00\u540d\u5b66\u751f\n\u51fd\u6570\u5316\uff1a\u516d\u4e2a\u529f\u80fd\u6bcf\u4e2a\u4e00\u4e2a\u51fd\u6570\uff0c<code>main()<\/code> \u53ea\u505a\u83dc\u5355\u8c03\u5ea6\uff0c\u4ee3\u7801\u50cf\u8bf4\u660e\u4e66\n\u4f5c\u7528\u57df\u7ec6\u8282\uff1a<code>students.append(...)<\/code> \u4e0d\u7528 global\uff0c<code>students = ...<\/code> \u5fc5\u987b <code>global<\/code>\n\u6301\u4e45\u5316\u95ed\u73af\uff1a<code>json.dump<\/code> \u4fdd\u5b58 + <code>json.load<\/code> \u52a0\u8f7d\uff0c\u91cd\u542f\u7a0b\u5e8f\u6570\u636e\u8fd8\u5728\n\u5f02\u5e38\u9632\u62a4\uff1a\u8f93\u5165\u975e\u6570\u5b57\uff08<code>except ValueError<\/code>\uff09\u3001\u6587\u4ef6\u635f\u574f\uff08<code>except (JSONDecodeError, OSError)<\/code>\uff09\u90fd\u4e0d\u95ea\u9000\n\u9879\u76ee\u62c6\u89e3\u80fd\u529b\uff1a\u80fd\u5bf9\u7167\u300c\u529f\u80fd \u2192 \u77e5\u8bc6\u70b9\u300d\u8868\u683c\uff0c\u8bf4\u6e05\u6bcf\u4e2a\u529f\u80fd\u7528\u5230\u4e86\u54ea\u51e0\u5929\u7684\u77e5\u8bc6\n\u5f00\u53d1\u6d41\u7a0b\uff1a\u4e09\u6b65\u8d70\uff08\u9aa8\u67b6 \u2192 \u586b\u7a7a\u8179 \u2192 \u52a0\u56fa\uff09\uff0c\u6bcf\u6b65\u90fd\u80fd\u8fd0\u884c\u9a8c\u8bc1\n\u72ec\u7acb\u5b8c\u6210\u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf\uff0c\u5e76\u6d4b\u8bd5\u975e\u6cd5\u8f93\u5165\u3001\u6587\u4ef6\u635f\u574f\u7b49\u8fb9\u754c\u573a\u666f<\/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.JSONDecodeError<\/code><\/td><td><code>students.json<\/code> \u88ab\u624b\u52a8\u6539\u574f\u6216\u683c\u5f0f\u4e0d\u5bf9<\/td><td>\u5220\u9664\u635f\u574f\u6587\u4ef6\u91cd\u65b0\u5f00\u59cb\uff0c\u6216\u5728 <code>load_data<\/code> \u4e2d\u6355\u83b7<\/td><\/tr><tr><td><code>UnboundLocalError<\/code><\/td><td>\u51fd\u6570\u5185\u7ed9\u5168\u5c40\u5217\u8868\u8d4b\u503c\u5374\u5fd8\u4e86 <code>global<\/code> \u58f0\u660e<\/td><td>\u7528 <code>students[:] = ...<\/code> \u4fee\u6539\uff0c\u6216\u5728\u51fd\u6570\u5f00\u5934\u58f0\u660e <code>global<\/code><\/td><\/tr><tr><td><code>ValueError: invalid literal for int()<\/code><\/td><td>\u5e74\u9f84\u8f93\u5165\u4e86\u975e\u6570\u5b57<\/td><td>\u53c2\u8003 <code>add_student<\/code> \u7528 <code>try \/ except<\/code> \u6355\u83b7<\/td><\/tr><tr><td><code>KeyError: 'score'<\/code><\/td><td>\u5b57\u5178\u91cc\u6ca1\u6709\u8fd9\u4e2a\u952e\uff08\u6bd4\u5982\u65e7\u6570\u636e\u683c\u5f0f\u4e0d\u540c\uff09<\/td><td>\u6253\u5370 <code>students<\/code> \u68c0\u67e5\u6bcf\u9879\u7684\u952e\u662f\u5426\u4e00\u81f4<\/td><\/tr><tr><td>\u4fdd\u5b58\u540e\u4e2d\u6587\u53d8 <code>\\uXXXX<\/code><\/td><td>\u5199 JSON \u65f6\u6f0f\u4e86 <code>ensure_ascii=False<\/code><\/td><td>\u4fdd\u5b58\u65f6\u52a0\u4e0a <code>ensure_ascii=False<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1 \u9879\u76ee\u603b\u89c8 \u2014\u2014 \u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf\uff08\u9636\u6bb5\u4e8c\u6536\u5b98\uff09 \u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f \u4eca\u5929\u662f\u9636\u6bb5\u4e8c\u6536\u5b98\u65e5\uff0c\u6ca1\u6709\u65b0\u77e5\u8bc6\u70b9\u2014\u2014\u91cd [&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-94","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\/94","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=94"}],"version-history":[{"count":2,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/94\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}