{"id":8909,"date":"2023-09-11T10:34:53","date_gmt":"2023-09-11T02:34:53","guid":{"rendered":"\/?p=8909"},"modified":"2023-09-11T10:34:53","modified_gmt":"2023-09-11T02:34:53","slug":"python-pickle%e6%a8%a1%e5%9d%97%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"\/?p=8909","title":{"rendered":"python-Pickle\u6a21\u5757\u7684\u4f7f\u7528"},"content":{"rendered":"<h2><strong>1.Pickle\u7684\u4ecb\u7ecd<\/strong><\/h2>\n<p>pickle\u662fpython\u8bed\u8a00\u7684\u4e00\u4e2a\u6807\u51c6\u6a21\u5757\uff0c\u5b89\u88c5python\u540e\u5df2\u5305\u542bpickle\u5e93\uff0c\u4e0d\u9700\u8981\u5355\u72ec\u518d\u5b89\u88c5<\/p>\n<p><strong>\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u4f5c\u7528<\/strong><\/p>\n<p><strong>1.\u4fbf\u4e8e\u5b58\u50a8<\/strong>\u3002\u5e8f\u5217\u5316\u8fc7\u7a0b\u5c06\u6587\u672c\u4fe1\u606f\u8f6c\u53d8\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u6d41\u3002\u8fd9\u6837\u5c31\u4fe1\u606f\u5c31\u5bb9\u6613\u5b58\u50a8\u5728\u786c\u76d8\u4e4b\u4e2d\uff0c\u5f53\u9700\u8981\u8bfb\u53d6\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4ece\u786c\u76d8\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u7136\u540e\u518d\u5c06\u5176\u53cd\u5e8f\u5217\u5316\u4fbf\u53ef\u4ee5\u5f97\u5230\u539f\u59cb\u7684\u6570\u636e\u3002\u5728Python\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u5f97\u5230\u4e86\u4e00\u4e9b\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u6570\u636e\uff0c\u60f3\u8981\u957f\u4e45\u7684\u4fdd\u5b58\u4e0b\u6765\uff0c\u65b9\u4fbf\u4ee5\u540e\u4f7f\u7528\uff0c\u800c\u4e0d\u662f\u7b80\u5355\u7684\u653e\u5165\u5185\u5b58\u4e2d\u5173\u673a\u65ad\u7535\u5c31\u4e22\u5931\u6570\u636e\u3002python\u6a21\u5757\u5927\u5168\u4e2d\u7684Pickle\u6a21\u5757\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\uff0c\u5b83\u53ef\u4ee5\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e00\u79cd\u53ef\u4ee5\u4f20\u8f93\u6216\u5b58\u50a8\u7684\u683c\u5f0f\u3002<\/p>\n<p><strong>2.\u4fbf\u4e8e\u4f20\u8f93<\/strong>\u3002\u5f53\u4e24\u4e2a\u8fdb\u7a0b\u5728\u8fdb\u884c\u8fdc\u7a0b\u901a\u4fe1\u65f6\uff0c\u5f7c\u6b64\u53ef\u4ee5\u53d1\u9001\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u3002\u65e0\u8bba\u662f\u4f55\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff0c\u90fd\u4f1a\u4ee5\u4e8c\u8fdb\u5236\u5e8f\u5217\u7684\u5f62\u5f0f\u5728\u7f51\u7edc\u4e0a\u4f20\u9001\u3002\u53d1\u9001\u65b9\u9700\u8981\u628a\u9019\u4e2a\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u5e8f\u5217\uff0c\u5728\u80fd\u5728\u7f51\u7edc\u4e0a\u4f20\u8f93\uff1b\u63a5\u6536\u65b9\u5219\u9700\u8981\u628a\u5b57\u8282\u5e8f\u5217\u5728\u6062\u590d\u4e3a\u5bf9\u8c61\u3002<\/p>\n<p>pickle\u6a21\u5757\u662f\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\u5e8f\u5217\u5316\u540e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff08\u4fdd\u5b58\u6587\u4ef6\u7684\u540e\u7f00\u4e3a\u201d.pkl\u201d\uff09\uff0c\u4e0d\u80fd\u76f4\u63a5\u6253\u5f00\u8fdb\u884c\u9884\u89c8\u3002\u800cpython\u7684\u53e6\u4e00\u4e2a\u5e8f\u5217\u5316\u6807\u51c6\u6a21\u5757json\uff0c\u5219\u662fhuman-readable\u7684\uff0c\u53ef\u4ee5\u76f4\u63a5\u6253\u5f00\u67e5\u770b\uff08\u4f8b\u5982\u5728notepad++\u4e2d\u67e5\u770b\uff09\u3002<\/p>\n<p><strong>pickle\u6a21\u5757\u6709\u4e24\u7c7b\u4e3b\u8981\u7684\u63a5\u53e3\uff0c\u5373\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002<\/strong><\/p>\n<p><strong>\u5176\u4e2d\u5e8f\u5217\u5316\u64cd\u4f5c\u5305\u62ec\uff1a<\/strong><br \/>\npickle.dump()<br \/>\nPickler(file, protocol).dump(obj)<\/p>\n<p><strong>\u53cd\u5e8f\u5217\u5316\u64cd\u4f5c\u5305\u62ec\uff1a<\/strong><br \/>\npickle.load()<br \/>\nUnpickler(file).load()<\/p>\n<h2><strong>2 \u5e8f\u5217\u5316\u64cd\u4f5c<\/strong><\/h2>\n<h3><strong>2.1 \u5e8f\u5217\u5316\u65b9\u6cd5pickle.dump()<\/strong><\/h3>\n<p>\u5e8f\u5217\u5316\u7684\u65b9\u6cd5\u4e3a pickle.dump()\uff0c\u8be5\u65b9\u6cd5\u7684\u76f8\u5173\u53c2\u6570\u5982\u4e0b\uff1a<br \/>\n<code>pickle.dump(obj, file, protocol=None,*,fix_imports=True)<\/code><br \/>\n\u8be5\u65b9\u6cd5\u5b9e\u73b0\u7684\u662f\u5c06\u5e8f\u5217\u5316\u540e\u7684\u5bf9\u8c61obj\u4ee5\u4e8c\u8fdb\u5236\u5f62\u5f0f\u5199\u5165\u6587\u4ef6file\u4e2d\uff0c\u8fdb\u884c\u4fdd\u5b58\u3002\u5b83\u7684\u529f\u80fd\u7b49\u540c\u4e8e <code>Pickler(file, protocol).dump(obj)<\/code>\u3002<br \/>\n\u5173\u4e8e\u53c2\u6570file\uff0c\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\uff0c\u5fc5\u987b\u662f\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\u8fdb\u884c\u64cd\u4f5c\uff08\u5199\u5165\uff09\u3002<\/p>\n<p>\u53c2\u8003\u524d\u6587\u7684\u6848\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pickle\nwith open(&#039;svm_model_iris.pkl&#039;, &#039;wb&#039;) as f:\n    pickle.dump(svm_classifier, f)<\/code><\/pre>\n<p>file\u4e3a'svm_model_iris.pkl'\uff0c\u5e76\u4e14\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\uff08'wb'\uff09\u5199\u5165\u3002<br \/>\n\u5173\u4e8e\u53c2\u6570protocol\uff0c\u4e00\u5171\u67095\u4e2d\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u5373\uff080,1,2,3,4\uff09\u3002\uff080,1,2\uff09\u5bf9\u5e94\u7684\u662fpython\u65e9\u671f\u7684\u7248\u672c\uff0c\uff083,4\uff09\u5219\u662f\u5728python3\u4e4b\u540e\u7684\u7248\u672c\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u53c2\u6570\u53ef\u9009 pickle.HIGHEST_PROTOCOL\u548cpickle.DEFAULT_PROTOCOL\u3002\u5f53\u524d\uff0cpython3.5\u7248\u672c\u4e2d\uff0cpickle.HIGHEST_PROTOCOL\u7684\u503c\u4e3a4\uff0cpickle.DEFAULT_PROTOCOL\u7684\u503c\u4e3a3\u3002\u5f53protocol\u53c2\u6570\u4e3a\u8d1f\u6570\u65f6\uff0c\u8868\u793a\u9009\u62e9\u7684\u53c2\u6570\u662fpickle.HIGHEST_PROTOCOL\u3002<\/p>\n<h3><strong>2.2 \u5e8f\u5217\u5316\u65b9\u6cd5pickle.dumps()<\/strong><\/h3>\n<p>pickle.dumps()\u65b9\u6cd5\u7684\u53c2\u6570\u5982\u4e0b\uff1a<br \/>\n<code>pickle.dumps(obj, protocol=None,*,fix_imports=True)<\/code><br \/>\npickle.dumps()\u65b9\u6cd5\u8ddfpickle.dump()\u65b9\u6cd5\u7684\u533a\u522b\u5728\u4e8e\uff0cpickle.dumps()\u65b9\u6cd5\u4e0d\u9700\u8981\u5199\u5165\u6587\u4ef6\u4e2d\uff0c\u5b83\u662f\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a\u5e8f\u5217\u5316\u7684bytes\u5bf9\u8c61\u3002<\/p>\n<h3><strong>2.3 \u5e8f\u5217\u5316\u65b9\u6cd5Pickler(file, protocol).dump(obj)<\/strong><\/h3>\n<p>pickle\u6a21\u5757\u63d0\u4f9b\u4e86\u5e8f\u5217\u5316\u7684\u9762\u5411\u5bf9\u8c61\u7684\u7c7b\u65b9\u6cd5\uff0c\u5373 class pickle.Pickler(file, protocol=None,*,fix_imports=True),Pickler\u7c7b\u6709dump()\u65b9\u6cd5\u3002<\/p>\n<p>Pickler(file, protocol).dump(obj) \u5b9e\u73b0\u7684\u529f\u80fd\u8ddf pickle.dump() \u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>\u5173\u4e8ePickler\u7c7b\u7684\u5176\u4ed6method\uff0c\u8bf7\u53c2\u8003\u5b98\u65b9API\u3002<\/p>\n<h2>3 \u53cd\u5e8f\u5217\u5316\u64cd\u4f5c<\/h2>\n<h3><strong>3.1 \u53cd\u5e8f\u5217\u5316\u65b9\u6cd5pickle.load()<\/strong><\/h3>\n<p>\u5e8f\u5217\u5316\u7684\u65b9\u6cd5\u4e3a pickle.load()\uff0c\u8be5\u65b9\u6cd5\u7684\u76f8\u5173\u53c2\u6570\u5982\u4e0b\uff1a<br \/>\npickle.load(file, *,fix_imports=True, encoding=\u201dASCII\u201d. errors=\u201dstrict\u201d)<br \/>\n\u8be5\u65b9\u6cd5\u5b9e\u73b0\u7684\u662f\u5c06\u5e8f\u5217\u5316\u7684\u5bf9\u8c61\u4ece\u6587\u4ef6file\u4e2d\u8bfb\u53d6\u51fa\u6765\u3002\u5b83\u7684\u529f\u80fd\u7b49\u540c\u4e8e Unpickler(file).load()\u3002<br \/>\n\u5173\u4e8e\u53c2\u6570file\uff0c\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\uff0c\u5fc5\u987b\u662f\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\u8fdb\u884c\u64cd\u4f5c\uff08\u8bfb\u53d6\uff09\u3002<br \/>\n\u53c2\u8003\u524d\u6587\u7684\u6848\u4f8b\u5982\u4e0b\uff1a<br \/>\nimport picklewith open('svm_model_iris.pkl', 'rb') as f:<br \/>\nmodel = pickle.load(f)<br \/>\nfile\u4e3a'svm_model_iris.pkl'\uff0c\u5e76\u4e14\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\uff08'rb'\uff09\u8bfb\u53d6\u3002<br \/>\n\u8bfb\u53d6\u7684\u65f6\u5019\uff0c\u53c2\u6570protocol\u662f\u81ea\u52a8\u9009\u62e9\u7684\uff0cload()\u65b9\u6cd5\u4e2d\u6ca1\u6709\u8fd9\u4e2a\u53c2\u6570\u3002<\/p>\n<h3><strong>3.2 \u53cd\u5e8f\u5217\u5316\u65b9\u6cd5pickle.loads()<\/strong><\/h3>\n<p>pickle.loads()\u65b9\u6cd5\u7684\u53c2\u6570\u5982\u4e0b\uff1a<br \/>\npickle.loads(bytes_object, *,fix_imports=True, encoding=\u201dASCII\u201d. errors=\u201dstrict\u201d)<br \/>\npickle.loads()\u65b9\u6cd5\u8ddfpickle.load()\u65b9\u6cd5\u7684\u533a\u522b\u5728\u4e8e\uff0cpickle.loads()\u65b9\u6cd5\u662f\u76f4\u63a5\u4ecebytes\u5bf9\u8c61\u4e2d\u8bfb\u53d6\u5e8f\u5217\u5316\u7684\u4fe1\u606f\uff0c\u800c\u975e\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u3002<\/p>\n<h3><strong>3.3 \u53cd\u5e8f\u5217\u5316\u65b9\u6cd5Unpickler(file).load()<\/strong><\/h3>\n<p>pickle\u6a21\u5757\u63d0\u4f9b\u4e86\u53cd\u5e8f\u5217\u5316\u7684\u9762\u5411\u5bf9\u8c61\u7684\u7c7b\u65b9\u6cd5\uff0c\u5373 class pickle.Unpickler(file, *,fix_imports=True, encoding=&quot;ASCII&quot;. errors=&quot;strict&quot;),Pickler\u7c7b\u6709load()\u65b9\u6cd5\u3002<br \/>\nUnpickler(file).load() \u5b9e\u73b0\u7684\u529f\u80fd\u8ddf pickle.load() \u662f\u4e00\u6837\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.Pickle\u7684\u4ecb\u7ecd pickle\u662fpython\u8bed\u8a00\u7684\u4e00\u4e2a\u6807\u51c6\u6a21\u5757\uff0c\u5b89\u88c5python\u540e\u5df2\u5305\u542bpickle\u5e93\uff0c\u4e0d\u9700\u8981\u5355\u72ec\u518d\u5b89\u88c5 \u5e8f\u5217\u5316\u548c   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[200],"tags":[201,16,202],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8909"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8909"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8909\/revisions"}],"predecessor-version":[{"id":8910,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8909\/revisions\/8910"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8909"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}