{"id":812,"date":"2021-05-04T22:27:51","date_gmt":"2021-05-04T14:27:51","guid":{"rendered":"https:\/\/104.es\/?p=812"},"modified":"2021-05-04T22:27:51","modified_gmt":"2021-05-04T14:27:51","slug":"python-file-operation-dict-list","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2021\/05\/04\/python-file-operation-dict-list\/","title":{"rendered":"\u5229\u7528Python\u8cc7\u6599\u6a94\u4fdd\u5b58\u8cc7\u6599\u7d50\u69cb\u8b8a\u6578\u5167\u5bb9"},"content":{"rendered":"\n<p>\u5728\u524d\u4e00\u7bc7\u6587\u7ae0\u4e2d\u6211\u5011\u5b78\u6703\u4e86\u5982\u4f55\u5229\u7528\u6587\u5b57\u6a94\u7684\u65b9\u5f0f\uff0c\u4ee5\u6bcf\u4e00\u500b\u8cc7\u6599\u4e00\u5217\u7684\u65b9\u5f0f\u4fdd\u5b58\u4e32\u5217\u4e2d\u7684\u6bcf\u4e00\u500b\u6578\u503c\u5143\u7d20\uff0c\u6b64\u7a2e\u65b9\u5f0f\u5c0d\u65bc\u5b57\u4e32\u8cc7\u6599\u4e5f\u4e00\u4f75\u9069\u7528\u3002\u7136\u5f8c\uff0c\u6709\u6642\u5019\u6211\u5011\u7684\u8cc7\u6599\u8b8a\u6578\u7684\u578b\u614b\u8f38\u70ba\u8907\u96dc\u6642\uff0c\u5176\u5be6\u662f\u4e0d\u9700\u8981\u628a\u5b83\u5011\u9032\u884c\u5256\u6790\uff0c\u9010\u500b\u4fdd\u5b58\u7684\uff0c\u6211\u5011\u53ef\u4ee5\u4e00\u6b21\u5c31\u628a\u6240\u6709\u7684\u8cc7\u6599\u5168\u90e8\u4ee5\u8cc7\u6599\u6a94\u7684\u578b\u5f0f\u5b58\u6210\u6a94\u6848\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u4ee5\u4e0a\u4e00\u7bc7\u6587\u7ae0\u7684\u4f8b\u5b50\uff0c\u7576\u6211\u5011\u5efa\u7acb\u4e86\u4e00\u500b\u6578\u503c\u4e32\u5217\u4e4b\u5f8c\uff0c\u4e0d\u9700\u8981\u8ff4\u5708\uff0c\u53ea\u8981\u628a\u5b83\u76f4\u63a5\u8f49\u63db\u6210\u5b57\u4e32\uff0c\u518d\u5229\u7528write()\u51fd\u5f0f\u5132\u5b58\u5373\u53ef\u3002\u8acb\u53c3\u8003\u4ee5\u4e0b\u7684\u7a0b\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>scores = list()\nscore = int(input(\"Score=\"))\nwhile score &gt;=0:\n    scores.append(score)\n    score = int(input(\"Score=\"))\nfilename = input(\"\u8acb\u8f38\u5165\u8981\u4fdd\u5b58\u7684\u6a94\u6848\u540d\u7a31\uff1a\")\nfp = open(filename, \"w\")\nfp.write(str(scores))\nfp.close()<\/code><\/pre>\n\n\n\n<p>\u6b64\u7a0b\u5f0f\u57f7\u884c\u7684\u904e\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"360\" height=\"189\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2021\/05\/image-3.png?w=360\" alt=\"\" class=\"wp-image-818\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2021\/05\/image-3.png 360w, https:\/\/104.es\/wp-content\/uploads\/2021\/05\/image-3-300x158.png 300w\" sizes=\"auto, (max-width: 360px) 100vw, 360px\" \/><\/figure>\n\n\n\n<p>\u5982\u4e0a\u5716\u6240\u793a\uff0c\u6211\u5011\u628a\u90a3\u4e9b\u6578\u5b57\uff08\u653e\u5728scores\u6578\u503c\u4e32\u5217\u4e2d\uff09\u5132\u5b58\u5728data03.txt\u4e2d\uff0c\u5247\u6253\u958bdata03.txt\u4e4b\u5f8c\uff0c\u53ef\u4ee5\u770b\u5230\u4ee5\u4e0b\u7684\u5167\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;10, 40, 89, 45, 45, 98]<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u7684\u8868\u73fe\u683c\u5f0f\uff0c\u5c31\u662f\u4e32\u5217\u578b\u614b\u539f\u4f86\u7684\u6a23\u5b50\uff0c\u4f46\u662f\u5b83\u88ab\u4ee5\u5b57\u4e32\u7684\u578b\u614b\u4fdd\u5b58\u8d77\u4f86\u4e86\u3002\u540c\u6a23\u7684\u65b9\u6cd5\uff0c\u5982\u679c\u6211\u5011\u6709\u5b57\u5178\u578b\u614b\u7684\u8cc7\u6599\u9700\u8981\u4fdd\u5b58\u7684\u8a71\uff0c\u4e5f\u662f\u76f4\u63a5\u5b58\u6a94\uff0c\u8acb\u53c3\u8003\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data = dict()\ndata&#091;'Apple'] = '\u860b\u679c'\ndata&#091;'Cherry'] = '\u6afb\u6843'\ndata&#091;'Banana'] = '\u9999\u8549'\ndata&#091;'Lemon'] = '\u6ab8\u6aac'\nfilename = input(\"\u8acb\u8f38\u5165\u8981\u4fdd\u5b58\u7684\u6a94\u6848\u540d\u7a31\uff1a\")\nfp = open(filename, \"w\")\nfp.write(str(data))\nfp.close()<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u7684\u7a0b\u5f0f\u57f7\u884c\u4e4b\u5f8c\uff0c\u5047\u8a2d\u6211\u5011\u628a\u8cc7\u6599\u4fdd\u5b58\u5728data04.txt\u4e2d\uff0c\u5247\u6253\u958bdata04.txt\u53ef\u4ee5\u770b\u5230\u5982\u4e0b\u6240\u793a\u7684\u5167\u5bb9\uff0c\u5f88\u660e\u986f\u5c31\u662f\u5b57\u5178\u578b\u614b\u8a72\u6709\u7684\u6a23\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{'Apple': '\u860b\u679c', 'Cherry': '\u6afb\u6843', 'Banana': '\u9999\u8549', 'Lemon': '\u6ab8\u6aac'}<\/code><\/pre>\n\n\n\n<p>\u5176\u5b83\u7684\u683c\u5f0f\u4f9d\u6b64\u985e\u63a8\uff0c\u5c31\u4e0d\u518d\u91cd\u8907\u5c55\u793a\u3002\u4f46\u662f\uff0c\u4fdd\u5b58\u8d77\u4f86\u4e4b\u5f8c\uff0c\u6700\u91cd\u8981\u7684\u662f\u4e4b\u5f8c\u80fd\u5920\u52a0\u4ee5\u8b80\u53d6\u61c9\u7528\u3002\u7531\u65bc\u5b83\u662f\u4ee5\u6a19\u6e96\u7684\u8cc7\u6599\u578b\u614b\u8868\u793a\u65b9\u5f0f\u4f86\u5132\u5b58\uff0c\u6240\u4ee5\u4e26\u4e0d\u80fd\u4f7f\u7528readlines() \u8b80\u53d6\uff0c\u800c\u662f\u8981\u5229\u7528read()\u4e00\u53e3\u6c23\u628a\u5b83\u4ee5\u4e00\u500b\u5b57\u4e32\u7684\u65b9\u5f0f\u8b80\u5165\u8b8a\u6578\u4e2d\uff0c\u4e4b\u5f8c\u518d\u9032\u884c\u8f49\u63db\u3002\u8f49\u63db\u7684\u6a21\u7d44\u662fast\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import ast\nfilename = input(\"\u8acb\u8f38\u5165\u8981\u8b80\u53d6\u7684\u6a94\u6848\u540d\u7a31\uff1a\")\nfp = open(filename, \"r\", encoding=\"utf-8\")\ndata = fp.read()\nfp.close()\n\ndata = ast.literal_eval(data)\nprint(data&#091;'Banana'])\nprint(data&#091;'Cherry'])<\/code><\/pre>\n\n\n\n<p>\u5982\u4f60\u6240\u770b\u5230\u7684\uff0c\u8f49\u63db\u5f8c\u7684\u5b57\u5178\u8b8a\u6578\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002\u4e4b\u5f8c\u5982\u679c\u9700\u8981\u4fdd\u5b58\u6bd4\u8f03\u8907\u96dc\u7684\u8cc7\u6599\u578b\u614b\u8b8a\u6578\uff0c\u6b61\u8fce\u591a\u52a0\u4f7f\u7528\u6b64\u7a2e\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u524d\u4e00\u7bc7\u6587\u7ae0\u4e2d\u6211\u5011\u5b78\u6703\u4e86\u5982\u4f55\u5229\u7528\u6587\u5b57\u6a94\u7684\u65b9\u5f0f\uff0c\u4ee5\u6bcf\u4e00\u500b\u8cc7\u6599\u4e00\u5217\u7684\u65b9\u5f0f\u4fdd\u5b58\u4e32\u5217\u4e2d\u7684\u6bcf\u4e00\u500b\u6578\u503c\u5143\u7d20\uff0c\u6b64\u7a2e\u65b9\u5f0f\u5c0d\u65bc\u5b57\u4e32\u8cc7\u6599\u4e5f\u4e00\u4f75\u9069\u7528\u3002\u7136\u5f8c\uff0c\u6709\u6642\u5019\u6211\u5011\u7684\u8cc7\u6599\u8b8a\u6578\u7684\u578b\u614b\u8f38\u70ba\u8907\u96dc\u6642\uff0c\u5176\u5be6\u662f\u4e0d\u9700\u8981\u628a\u5b83\u5011\u9032\u884c\u5256\u6790\uff0c\u9010\u500b\u4fdd\u5b58\u7684\uff0c\u6211\u5011\u53ef\u4ee5\u4e00\u6b21\u5c31\u628a\u6240\u6709\u7684\u8cc7\u6599\u5168\u90e8\u4ee5\u8cc7\u6599\u6a94\u7684\u578b\u5f0f\u5b58\u6210\u6a94\u6848\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[15],"tags":[],"class_list":["post-812","post","type-post","status-publish","format-standard","hentry","category-python"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=812"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/812\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}