{"id":1114913,"date":"2025-01-08T18:01:53","date_gmt":"2025-01-08T10:01:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1114913.html"},"modified":"2025-01-08T18:01:55","modified_gmt":"2025-01-08T10:01:55","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e7%b1%bb%e5%8f%98%e9%87%8f%e5%ba%8f%e5%88%97%e5%8c%96","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1114913.html","title":{"rendered":"python\u5982\u4f55\u5c06\u7c7b\u53d8\u91cf\u5e8f\u5217\u5316"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075852\/610d4b09-3f91-4344-b1e2-1123abd28400.webp\" alt=\"python\u5982\u4f55\u5c06\u7c7b\u53d8\u91cf\u5e8f\u5217\u5316\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u5f0f\u5c06\u7c7b\u53d8\u91cf\u8fdb\u884c\u5e8f\u5217\u5316\uff1a\u4f7f\u7528\u5185\u7f6e\u6a21\u5757<code>pickle<\/code>\u3001\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u3001\u6216\u8005\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>marshmallow<\/code>\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>pickle<\/code>\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5e8f\u5217\u5316\u51e0\u4e4e\u6240\u6709\u7684Python\u5bf9\u8c61\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528<code>pickle<\/code>\u6a21\u5757\u8fdb\u884c\u5e8f\u5217\u5316\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528pickle\u6a21\u5757\u8fdb\u884c\u5e8f\u5217\u5316<\/h3>\n<\/p>\n<p><p><code>pickle<\/code>\u6a21\u5757\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\uff08\u5305\u62ec\u7c7b\u7684\u5b9e\u4f8b\uff09\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\uff0c\u5e76\u4e14\u53ef\u4ee5\u5c06\u5b57\u8282\u6d41\u91cd\u65b0\u8f6c\u6362\u56dePython\u5bf9\u8c61\u3002\u8fd9\u4f7f\u5f97<code>pickle<\/code>\u6a21\u5757\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5c06\u7c7b\u53d8\u91cf\u8fdb\u884c\u5e8f\u5217\u5316\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\uff0c\u5e76\u521b\u5efa\u5176\u5b9e\u4f8b\u3002\u7136\u540e\u4f7f\u7528<code>pickle<\/code>\u6a21\u5757\u5c06\u5176\u5e8f\u5217\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>my_instance = MyClass(&quot;Alice&quot;, 30)<\/p>\n<h2><strong>\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/strong><\/h2>\n<p>with open(&#39;my_instance.pkl&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    pickle.dump(my_instance, file)<\/p>\n<p>print(&quot;\u7c7b\u5b9e\u4f8b\u5df2\u6210\u529f\u5e8f\u5217\u5316\u5230\u6587\u4ef6 my_instance.pkl&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06<code>my_instance<\/code>\u5bf9\u8c61\u5e8f\u5217\u5316\u5e76\u4fdd\u5b58\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002<code>pickle.dump()<\/code>\u51fd\u6570\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u5e76\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u53cd\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/h4>\n<\/p>\n<p><p>\u8981\u4ece\u6587\u4ef6\u4e2d\u6062\u590d\u5e8f\u5217\u5316\u7684\u5bf9\u8c61\uff0c\u6211\u4eec\u4f7f\u7528<code>pickle.load()<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53cd\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/p>\n<p>with open(&#39;my_instance.pkl&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    loaded_instance = pickle.load(file)<\/p>\n<p>print(&quot;\u7c7b\u5b9e\u4f8b\u5df2\u6210\u529f\u4ece\u6587\u4ef6 my_instance.pkl \u53cd\u5e8f\u5217\u5316&quot;)<\/p>\n<p>print(f&quot;Name: {loaded_instance.name}, Age: {loaded_instance.age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u53cd\u5e8f\u5217\u5316\uff0c\u6211\u4eec\u53ef\u4ee5\u6062\u590d\u539f\u59cb\u5bf9\u8c61\uff0c\u5e76\u4e14\u5b83\u5c06\u4fdd\u7559\u539f\u59cb\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528json\u6a21\u5757\u8fdb\u884c\u5e8f\u5217\u5316<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>pickle<\/code>\u6a21\u5757\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u4e00\u79cd\u66f4\u901a\u7528\u7684\u683c\u5f0f\u8fdb\u884c\u5e8f\u5217\u5316\uff0c\u4f8b\u5982JSON\u3002<code>json<\/code>\u6a21\u5757\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u53ef\u4ee5\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u56dePython\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5c06\u7c7b\u5b9e\u4f8b\u8f6c\u6362\u4e3a\u53ef\u4ee5\u88abJSON\u5e8f\u5217\u5316\u7684\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def to_dict(self):<\/p>\n<p>        return {&quot;name&quot;: self.name, &quot;age&quot;: self.age}<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>my_instance = MyClass(&quot;Alice&quot;, 30)<\/p>\n<h2><strong>\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/strong><\/h2>\n<p>with open(&#39;my_instance.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(my_instance.to_dict(), file)<\/p>\n<p>print(&quot;\u7c7b\u5b9e\u4f8b\u5df2\u6210\u529f\u5e8f\u5217\u5316\u5230\u6587\u4ef6 my_instance.json&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4e00\u4e2a<code>to_dict()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5c06\u7c7b\u7684\u5c5e\u6027\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u7136\u540e\u6211\u4eec\u4f7f\u7528<code>json.dump()<\/code>\u5c06\u5b57\u5178\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u53cd\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/h4>\n<\/p>\n<p><p>\u8981\u4ece\u6587\u4ef6\u4e2d\u6062\u590d\u5e8f\u5217\u5316\u7684\u5bf9\u8c61\uff0c\u6211\u4eec\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u4ece\u5b57\u5178\u521b\u5efa\u7c7b\u5b9e\u4f8b\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u5b57\u5178\u521b\u5efa\u7c7b\u5b9e\u4f8b\u7684\u65b9\u6cd5<\/p>\n<p>def from_dict(data):<\/p>\n<p>    return MyClass(data[&quot;name&quot;], data[&quot;age&quot;])<\/p>\n<h2><strong>\u53cd\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/strong><\/h2>\n<p>with open(&#39;my_instance.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>    loaded_instance = from_dict(data)<\/p>\n<p>print(&quot;\u7c7b\u5b9e\u4f8b\u5df2\u6210\u529f\u4ece\u6587\u4ef6 my_instance.json \u53cd\u5e8f\u5217\u5316&quot;)<\/p>\n<p>print(f&quot;Name: {loaded_instance.name}, Age: {loaded_instance.age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u53cd\u5e8f\u5217\u5316\uff0c\u6211\u4eec\u53ef\u4ee5\u6062\u590d\u539f\u59cb\u5bf9\u8c61\uff0c\u5e76\u4e14\u5b83\u5c06\u4fdd\u7559\u539f\u59cb\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528marshmallow\u5e93\u8fdb\u884c\u5e8f\u5217\u5316<\/h3>\n<\/p>\n<p><p><code>marshmallow<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u5c06\u590d\u6742\u6570\u636e\u7c7b\u578b\uff08\u5982\u5bf9\u8c61\uff09\u4e0ePython\u6570\u636e\u7c7b\u578b\uff08\u5982\u5b57\u5178\u3001\u5217\u8868\uff09\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u7684\u5e93\u3002\u5b83\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5marshmallow\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5<code>marshmallow<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install marshmallow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b9a\u4e49Schema\u7c7b<\/h4>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a<code>Schema<\/code>\u7c7b\u6765\u63cf\u8ff0\u6211\u4eec\u5e0c\u671b\u5982\u4f55\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from marshmallow import Schema, fields<\/p>\n<p>class MyClassSchema(Schema):<\/p>\n<p>    name = fields.Str()<\/p>\n<p>    age = fields.Int()<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>my_instance = MyClass(&quot;Alice&quot;, 30)<\/p>\n<h2><strong>\u521b\u5efaSchema\u5b9e\u4f8b<\/strong><\/h2>\n<p>schema = MyClassSchema()<\/p>\n<h2><strong>\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/strong><\/h2>\n<p>serialized_data = schema.dump(my_instance)<\/p>\n<p>print(&quot;\u5e8f\u5217\u5316\u6570\u636e:&quot;, serialized_data)<\/p>\n<h2><strong>\u53cd\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/strong><\/h2>\n<p>loaded_instance = schema.load(serialized_data)<\/p>\n<p>print(&quot;\u53cd\u5e8f\u5217\u5316\u6570\u636e:&quot;, loaded_instance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>MyClassSchema<\/code>\u7c7b\uff0c\u8be5\u7c7b\u7ee7\u627f\u81ea<code>Schema<\/code>\u5e76\u63cf\u8ff0\u4e86\u7c7b\u7684\u5c5e\u6027\u3002\u7136\u540e\u6211\u4eec\u4f7f\u7528<code>schema.dump()<\/code>\u65b9\u6cd5\u5c06\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a\u5b57\u5178\uff0c\u5e76\u4f7f\u7528<code>schema.load()<\/code>\u65b9\u6cd5\u5c06\u5b57\u5178\u53cd\u5e8f\u5217\u5316\u4e3a\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6bd4\u8f83\u4e0d\u540c\u7684\u5e8f\u5217\u5316\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u5e8f\u5217\u5316\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>pickle<\/strong>\uff1a\u80fd\u591f\u5e8f\u5217\u5316\u51e0\u4e4e\u6240\u6709\u7684Python\u5bf9\u8c61\uff0c\u4f46\u751f\u6210\u7684\u5b57\u8282\u6d41\u4e0d\u6613\u8bfb\uff0c\u4e0d\u9002\u5408\u8de8\u8bed\u8a00\u548c\u8de8\u5e73\u53f0\u4f7f\u7528\u3002<\/li>\n<li><strong>json<\/strong>\uff1a\u751f\u6210\u7684JSON\u5b57\u7b26\u4e32\u6613\u8bfb\u4e14\u9002\u5408\u8de8\u8bed\u8a00\u548c\u8de8\u5e73\u53f0\u4f7f\u7528\uff0c\u4f46\u53ea\u652f\u6301\u57fa\u672c\u7684\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li><strong>marshmallow<\/strong>\uff1a\u80fd\u591f\u7075\u6d3b\u5730\u5c06\u590d\u6742\u6570\u636e\u7c7b\u578b\u4e0ePython\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u5e76\u4e14\u751f\u6210\u7684\u5b57\u5178\u6613\u8bfb\u4e14\u9002\u5408\u8de8\u8bed\u8a00\u548c\u8de8\u5e73\u53f0\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><p>\u5728\u9009\u62e9\u5e8f\u5217\u5316\u65b9\u6cd5\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u5e8f\u5217\u5316\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u5e8f\u5217\u5316\u65b9\u6cd5\uff0c\u5982<code>yaml<\/code>\u3001<code>msgpack<\/code>\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u7c7b\u53d8\u91cf\u8fdb\u884c\u5e8f\u5217\u5316\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f\u6765\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528yaml\u8fdb\u884c\u5e8f\u5217\u5316<\/h4>\n<\/p>\n<p><p><code>yaml<\/code>\u662f\u4e00\u79cd\u4eba\u7c7b\u53ef\u8bfb\u7684\u6570\u636e\u5e8f\u5217\u5316\u683c\u5f0f\uff0c\u9002\u5408\u7528\u4e8e\u914d\u7f6e\u6587\u4ef6\u7b49\u573a\u666f\u3002\u53ef\u4ee5\u4f7f\u7528<code>PyYAML<\/code>\u5e93\u8fdb\u884c\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def to_dict(self):<\/p>\n<p>        return {&quot;name&quot;: self.name, &quot;age&quot;: self.age}<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>my_instance = MyClass(&quot;Alice&quot;, 30)<\/p>\n<h2><strong>\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/strong><\/h2>\n<p>with open(&#39;my_instance.yaml&#39;, &#39;w&#39;) as file:<\/p>\n<p>    yaml.dump(my_instance.to_dict(), file)<\/p>\n<p>print(&quot;\u7c7b\u5b9e\u4f8b\u5df2\u6210\u529f\u5e8f\u5217\u5316\u5230\u6587\u4ef6 my_instance.yaml&quot;)<\/p>\n<h2><strong>\u53cd\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/strong><\/h2>\n<p>with open(&#39;my_instance.yaml&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = yaml.load(file, Loader=yaml.FullLoader)<\/p>\n<p>    loaded_instance = MyClass(data[&quot;name&quot;], data[&quot;age&quot;])<\/p>\n<p>print(&quot;\u7c7b\u5b9e\u4f8b\u5df2\u6210\u529f\u4ece\u6587\u4ef6 my_instance.yaml \u53cd\u5e8f\u5217\u5316&quot;)<\/p>\n<p>print(f&quot;Name: {loaded_instance.name}, Age: {loaded_instance.age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528msgpack\u8fdb\u884c\u5e8f\u5217\u5316<\/h4>\n<\/p>\n<p><p><code>msgpack<\/code>\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u5316\u683c\u5f0f\uff0c\u9002\u5408\u7528\u4e8e\u7f51\u7edc\u4f20\u8f93\u7b49\u573a\u666f\u3002\u53ef\u4ee5\u4f7f\u7528<code>msgpack<\/code>\u5e93\u8fdb\u884c\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import msgpack<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def to_dict(self):<\/p>\n<p>        return {&quot;name&quot;: self.name, &quot;age&quot;: self.age}<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>my_instance = MyClass(&quot;Alice&quot;, 30)<\/p>\n<h2><strong>\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/strong><\/h2>\n<p>with open(&#39;my_instance.msgpack&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    file.write(msgpack.packb(my_instance.to_dict()))<\/p>\n<p>print(&quot;\u7c7b\u5b9e\u4f8b\u5df2\u6210\u529f\u5e8f\u5217\u5316\u5230\u6587\u4ef6 my_instance.msgpack&quot;)<\/p>\n<h2><strong>\u53cd\u5e8f\u5217\u5316\u7c7b\u5b9e\u4f8b<\/strong><\/h2>\n<p>with open(&#39;my_instance.msgpack&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    data = msgpack.unpackb(file.read())<\/p>\n<p>    loaded_instance = MyClass(data[&quot;name&quot;], data[&quot;age&quot;])<\/p>\n<p>print(&quot;\u7c7b\u5b9e\u4f8b\u5df2\u6210\u529f\u4ece\u6587\u4ef6 my_instance.msgpack \u53cd\u5e8f\u5217\u5316&quot;)<\/p>\n<p>print(f&quot;Name: {loaded_instance.name}, Age: {loaded_instance.age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u7c7b\u53d8\u91cf\u8fdb\u884c\u5e8f\u5217\u5316<\/strong>\uff0c\u5305\u62ec<code>pickle<\/code>\u3001<code>json<\/code>\u3001<code>marshmallow<\/code>\u3001<code>yaml<\/code>\u3001<code>msgpack<\/code>\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002<code>pickle<\/code>\u6a21\u5757\u9002\u5408\u5e8f\u5217\u5316\u51e0\u4e4e\u6240\u6709\u7684Python\u5bf9\u8c61\uff0c\u4f46\u751f\u6210\u7684\u5b57\u8282\u6d41\u4e0d\u6613\u8bfb\uff1b<code>json<\/code>\u6a21\u5757\u751f\u6210\u7684JSON\u5b57\u7b26\u4e32\u6613\u8bfb\u4e14\u9002\u5408\u8de8\u8bed\u8a00\u548c\u8de8\u5e73\u53f0\u4f7f\u7528\uff0c\u4f46\u53ea\u652f\u6301\u57fa\u672c\u7684\u6570\u636e\u7c7b\u578b\uff1b<code>marshmallow<\/code>\u5e93\u80fd\u591f\u7075\u6d3b\u5730\u5c06\u590d\u6742\u6570\u636e\u7c7b\u578b\u4e0ePython\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u5e76\u4e14\u751f\u6210\u7684\u5b57\u5178\u6613\u8bfb\u4e14\u9002\u5408\u8de8\u8bed\u8a00\u548c\u8de8\u5e73\u53f0\u4f7f\u7528\uff1b<code>yaml<\/code>\u548c<code>msgpack<\/code>\u5219\u9002\u5408\u7528\u4e8e\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\uff0c\u5982\u914d\u7f6e\u6587\u4ef6\u548c\u7f51\u7edc\u4f20\u8f93\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u5e8f\u5217\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u7c7b\u53d8\u91cf\u7684\u6301\u4e45\u5316\u548c\u4f20\u8f93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5e8f\u5217\u5316\u7c7b\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pickle<\/code>\u6a21\u5757\u8f7b\u677e\u5e8f\u5217\u5316\u7c7b\u53d8\u91cf\u3002\u901a\u8fc7<code>pickle.dumps()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\uff0c\u800c<code>pickle.loads()<\/code>\u5219\u53ef\u4ee5\u5c06\u5b57\u8282\u6d41\u8fd8\u539f\u4e3a\u5bf9\u8c61\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7c7b\u9700\u8981\u5b9a\u4e49<code>__getstate__<\/code>\u548c<code>__setstate__<\/code>\u65b9\u6cd5\uff0c\u4ee5\u786e\u4fdd\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u8fc7\u7a0b\u4e2d\u6b63\u786e\u5904\u7406\u7c7b\u53d8\u91cf\u3002<\/p>\n<p><strong>\u662f\u5426\u6240\u6709\u7c7b\u53d8\u91cf\u90fd\u53ef\u4ee5\u88ab\u5e8f\u5217\u5316\uff1f<\/strong><br \/>\u5e76\u4e0d\u662f\u6240\u6709\u7c7b\u53d8\u91cf\u90fd\u80fd\u88ab\u5e8f\u5217\u5316\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u5982\u6574\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u7b49\uff09\u53ef\u4ee5\u88ab\u5e8f\u5217\u5316\uff0c\u800c\u4e00\u4e9b\u590d\u6742\u5bf9\u8c61\uff08\u5982\u6253\u5f00\u7684\u6587\u4ef6\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\uff09\u5219\u65e0\u6cd5\u5e8f\u5217\u5316\u3002\u786e\u4fdd\u7c7b\u53d8\u91cf\u662f\u53ef\u5e8f\u5217\u5316\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u7684\u65b9\u6cd5\u6765\u5904\u7406\u90a3\u4e9b\u4e0d\u652f\u6301\u5e8f\u5217\u5316\u7684\u5c5e\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u5e8f\u5217\u5316\u540e\u7684\u6570\u636e\u5b89\u5168\uff1f<\/strong><br \/>\u5e8f\u5217\u5316\u6570\u636e\u53ef\u80fd\u4f1a\u5e26\u6765\u5b89\u5168\u9690\u60a3\uff0c\u5c24\u5176\u662f\u5f53\u4ece\u4e0d\u53ef\u4fe1\u6765\u6e90\u52a0\u8f7d\u6570\u636e\u65f6\u3002\u4e3a\u4e86\u786e\u4fdd\u5b89\u5168\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pickle<\/code>\u6a21\u5757\u7684<code>pickle.loads()<\/code>\u65f6\u52a0\u4e0a\u5b89\u5168\u68c0\u67e5\uff0c\u6216\u9009\u62e9\u5176\u4ed6\u5e8f\u5217\u5316\u683c\u5f0f\uff0c\u5982<code>json<\/code>\uff0c\u5176\u4e0d\u652f\u6301\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff0c\u4ece\u800c\u964d\u4f4e\u5b89\u5168\u98ce\u9669\u3002\u540c\u65f6\uff0c\u786e\u4fdd\u4e0d\u53cd\u5e8f\u5217\u5316\u6765\u81ea\u4e0d\u4fe1\u4efb\u6e90\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u5f0f\u5c06\u7c7b\u53d8\u91cf\u8fdb\u884c\u5e8f\u5217\u5316\uff1a\u4f7f\u7528\u5185\u7f6e\u6a21\u5757pickle\u3001\u4f7f\u7528json\u6a21\u5757\u3001\u6216\u8005\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1114920,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114913"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1114913"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114913\/revisions"}],"predecessor-version":[{"id":1114923,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114913\/revisions\/1114923"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1114920"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1114913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1114913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1114913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}