{"id":1909,"date":"2023-11-13T15:52:32","date_gmt":"2023-11-13T07:52:32","guid":{"rendered":"http:\/\/zhang.mba\/?p=1909"},"modified":"2023-11-13T15:53:38","modified_gmt":"2023-11-13T07:53:38","slug":"python-zhong-de-pickle-mo-kuai","status":"publish","type":"post","link":"https:\/\/zhang.mba\/index.php\/2023\/11\/13\/15\/52\/32\/1909\/python-zhong-de-pickle-mo-kuai\/python\/zhangzhiqi\/","title":{"rendered":"Python\u4e2d\u7684 pickle \u6a21\u5757"},"content":{"rendered":"<p><code>.pkl<\/code> \u6587\u4ef6\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u7528\u4e8e\u5728Python\u4e2d\u5b58\u50a8\u5e8f\u5217\u5316\u5bf9\u8c61\u3002\u5e8f\u5217\u5316\u662f\u5c06\u5bf9\u8c61\uff08\u5982Python\u4e2d\u7684\u5b57\u5178\u3001\u5217\u8868\u3001\u751a\u81f3\u662f\u6574\u4e2a\u6a21\u578b\u5bf9\u8c61\uff09\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u7684\u8fc7\u7a0b\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u5c06\u5176\u4fdd\u5b58\u5230\u786c\u76d8\u4e0a\u3002<code>.pkl<\/code> \u662f <code>pickle<\/code> \u6a21\u5757\u751f\u6210\u7684\u6587\u4ef6\u7684\u5e38\u7528\u6269\u5c55\u540d\uff0c<code>pickle<\/code> \u662fPython\u7684\u5185\u7f6e\u6a21\u5757\uff0c\u7528\u4e8e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316Python\u5bf9\u8c61\u3002<\/p>\n<h3><a id=\"pkl%E6%96%87%E4%BB%B6%E7%9A%84%E4%B8%BB%E8%A6%81%E7%94%A8%E9%80%94%E5%8C%85%E6%8B%AC%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><code>.pkl<\/code> \u6587\u4ef6\u7684\u4e3b\u8981\u7528\u9014\u5305\u62ec\uff1a<\/h3>\n<ol>\n<li>\n<p><strong>\u5b58\u50a8\u6a21\u578b<\/strong>\uff1a\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u7ecf\u5e38\u4f7f\u7528 <code>.pkl<\/code> \u6587\u4ef6\u4fdd\u5b58\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u3002\u8fd9\u4f7f\u5f97\u60a8\u53ef\u4ee5\u5728\u4e0d\u9700\u8981\u91cd\u65b0\u8bad\u7ec3\u6a21\u578b\u7684\u60c5\u51b5\u4e0b\uff0c\u8f7b\u677e\u5730\u5728\u4e0d\u540c\u7684\u5e94\u7528\u7a0b\u5e8f\u6216\u73af\u5883\u4e2d\u4f7f\u7528\u8be5\u6a21\u578b\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u4ea4\u6362<\/strong>\uff1a<code>.pkl<\/code> \u6587\u4ef6\u4e5f\u53ef\u7528\u4e8e\u5b58\u50a8\u4efb\u4f55Python\u5bf9\u8c61\uff0c\u4f7f\u5f97\u6570\u636e\u548c\u5bf9\u8c61\u53ef\u4ee5\u5728\u4e0d\u540c\u7684Python\u7a0b\u5e8f\u4e4b\u95f4\u8f7b\u677e\u4f20\u8f93\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6301\u4e45\u6027\u5b58\u50a8<\/strong>\uff1a\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u6301\u4e45\u4fdd\u5b58\u6570\u636e\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u6216\u81ea\u5b9a\u4e49\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<\/ol>\n<h3><a id=\"%E4%BD%BF%E7%94%A8pickle%E6%A8%A1%E5%9D%97\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528 <code>pickle<\/code> \u6a21\u5757<\/h3>\n<h4><a id=\"%E4%BF%9D%E5%AD%98%E5%AF%B9%E8%B1%A1%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4fdd\u5b58\u5bf9\u8c61\uff1a<\/h4>\n<pre class=\"line-numbers\"><code class=\"language-python\">import pickle\n\nobject_to_save = {'a': 1, 'b': 2}\nwith open('filename.pkl', 'wb') as file:\n    pickle.dump(object_to_save, file)\n<\/code><\/pre>\n<h4><a id=\"%E5%8A%A0%E8%BD%BD%E5%AF%B9%E8%B1%A1%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u52a0\u8f7d\u5bf9\u8c61\uff1a<\/h4>\n<pre class=\"line-numbers\"><code class=\"language-python\">with open('filename.pkl', 'rb') as file:\n    loaded_object = pickle.load(file)\n<\/code><\/pre>\n<h3><a id=\"%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ul>\n<li>\u5728\u4f7f\u7528 <code>pickle<\/code> \u65f6\u8981\u6ce8\u610f\u5b89\u5168\u6027\uff0c\u56e0\u4e3a\u53cd\u5e8f\u5217\u5316\u4e0d\u53ef\u4fe1\u7684\u6216\u672a\u7ecf\u9a8c\u8bc1\u7684 <code>.pkl<\/code> \u6587\u4ef6\u53ef\u80fd\u5b58\u5728\u5b89\u5168\u98ce\u9669\u3002<\/li>\n<li><code>.pkl<\/code> \u6587\u4ef6\u4f9d\u8d56\u4e8ePython\u7248\u672c\u548c\u5bf9\u8c61\u7684\u7c7b\u7ed3\u6784\u3002\u5982\u679c\u60a8\u5728\u4e00\u4e2aPython\u7248\u672c\u4e2d\u4fdd\u5b58\u4e86\u4e00\u4e2a\u5bf9\u8c61\uff0c\u7136\u540e\u5728\u53e6\u4e00\u4e2a\u4e0d\u517c\u5bb9\u7684\u7248\u672c\u4e2d\u5c1d\u8bd5\u52a0\u8f7d\u5b83\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u95ee\u9898\u3002\u540c\u6837\uff0c\u5982\u679c\u5bf9\u8c61\u7684\u7c7b\u5b9a\u4e49\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u90a3\u4e48\u52a0\u8f7d\u65e7\u7684 <code>.pkl<\/code> \u6587\u4ef6\u4e5f\u53ef\u80fd\u51fa\u9519\u3002<\/li>\n<\/ul>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>.pkl \u6587\u4ef6\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u7528\u4e8e\u5728Python\u4e2d\u5b58\u50a8\u5e8f\u5217\u5316\u5bf9\u8c61\u3002\u5e8f\u5217\u5316\u662f\u5c06\u5bf9\u8c61\uff08\u5982Python\u4e2d\u7684\u5b57\u5178\u3001\u5217\u8868\u3001\u751a\u81f3\u662f\u6574\u4e2a\u6a21\u578b\u5bf9\u8c61\uff09\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u7684\u8fc7\u7a0b\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u5c06\u5176\u4fdd\u5b58\u5230\u786c\u76d8\u4e0a\u3002.pkl \u662f pickle \u6a21\u5757\u751f\u6210\u7684\u6587\u4ef6\u7684\u5e38\u7528\u6269\u5c55\u540d\uff0cpickle \u662fPython\u7684\u5185\u7f6e\u6a21\u5757 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[12,63],"tags":[54,69],"_links":{"self":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1909"}],"collection":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/comments?post=1909"}],"version-history":[{"count":0,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1909\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/media?parent=1909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/categories?post=1909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/tags?post=1909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}