{"id":1028406,"date":"2024-12-31T10:59:21","date_gmt":"2024-12-31T02:59:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1028406.html"},"modified":"2024-12-31T10:59:23","modified_gmt":"2024-12-31T02:59:23","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%8f%8d%e5%ba%8f%e5%88%97%e5%8c%96","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1028406.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5b9e\u73b0\u53cd\u5e8f\u5217\u5316"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/bbad82f6-edf8-4433-8ef4-b0f935e5df83.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u5b9e\u73b0\u53cd\u5e8f\u5217\u5316\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9e\u73b0\u53cd\u5e8f\u5217\u5316\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528pickle\u3001json\u3001yaml\u7b49\u5e93\uff0c\u5206\u522b\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e8f\u5217\u5316\u683c\u5f0f\u3002pickle\u3001json\u662f\u6700\u5e38\u89c1\u7684\u53cd\u5e8f\u5217\u5316\u65b9\u5f0f\u3002pickle\u6a21\u5757\u9002\u7528\u4e8ePython\u7279\u6709\u7684\u6570\u636e\u7c7b\u578b\uff0cjson\u6a21\u5757\u9002\u7528\u4e8e\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684\u6570\u636e\u4ea4\u6362\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>pickle\u53cd\u5e8f\u5217\u5316<\/strong>\uff1apickle\u6a21\u5757\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\uff0c\u5e76\u652f\u6301\u5c06\u5b57\u8282\u6d41\u6062\u590d\u4e3aPython\u5bf9\u8c61\uff0c\u5373\u53cd\u5e8f\u5217\u5316\u3002pickle\u6a21\u5757\u652f\u6301\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\uff0c\u5982\u7c7b\u5b9e\u4f8b\u3001\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u7b49\u3002\u4f7f\u7528<code>pickle.load()<\/code>\u65b9\u6cd5\u4ece\u6587\u4ef6\u6216\u5b57\u8282\u6d41\u4e2d\u52a0\u8f7d\u5e8f\u5217\u5316\u6570\u636e\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cpickle\u7684\u53cd\u5e8f\u5217\u5316\u64cd\u4f5c\u53ef\u80fd\u5b58\u5728\u5b89\u5168\u98ce\u9669\uff0c\u4e0d\u8981\u53cd\u5e8f\u5217\u5316\u4e0d\u4fe1\u4efb\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>json\u53cd\u5e8f\u5217\u5316<\/strong>\uff1ajson\u6a21\u5757\u9002\u7528\u4e8e\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002JSON\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u6613\u4e8e\u4eba\u7c7b\u9605\u8bfb\u548c\u7f16\u5199\uff0c\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\u3002\u4f7f\u7528<code>json.loads()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\uff0c\u4f7f\u7528<code>json.load()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7dJSON\u6570\u636e\u5e76\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002JSON\u4e3b\u8981\u7528\u4e8e\u7b80\u5355\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u3001\u5b57\u5178\u3001\u5b57\u7b26\u4e32\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001PICKLE\u6a21\u5757\u7684\u53cd\u5e8f\u5217\u5316<\/h3>\n<\/p>\n<p><h4>1.1\u3001pickle\u6a21\u5757\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>pickle\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06Python\u5bf9\u8c61\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u65b9\u6cd5\u3002\u5b83\u652f\u6301\u51e0\u4e4e\u6240\u6709\u7684Python\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u81ea\u5b9a\u4e49\u7c7b\u7684\u5b9e\u4f8b\u5bf9\u8c61\u3002\u4e0e\u5176\u4ed6\u5e8f\u5217\u5316\u683c\u5f0f\u4e0d\u540c\uff0cpickle\u5e8f\u5217\u5316\u540e\u7684\u6570\u636e\u662f\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u56e0\u6b64\u5728\u8de8\u8bed\u8a00\u6570\u636e\u4ea4\u6362\u65f6\u5e76\u4e0d\u9002\u7528\u3002<\/p>\n<\/p>\n<p><h4>1.2\u3001\u53cd\u5e8f\u5217\u5316\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528pickle\u6a21\u5757\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u5e8f\u5217\u5316\u6587\u4ef6data.pkl<\/strong><\/h2>\n<p>with open(&#39;data.pkl&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    data = pickle.load(file)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pickle.load()<\/code>\u65b9\u6cd5\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7d\u5e8f\u5217\u5316\u6570\u636e\uff0c\u5e76\u5c06\u5176\u53cd\u5e8f\u5217\u5316\u4e3aPython\u5bf9\u8c61\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>rb<\/code>\u6a21\u5f0f\u7528\u4e8e\u4ee5\u4e8c\u8fdb\u5236\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1.3\u3001\u53cd\u5e8f\u5217\u5316\u81ea\u5b9a\u4e49\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>pickle\u6a21\u5757\u8fd8\u652f\u6301\u53cd\u5e8f\u5217\u5316\u81ea\u5b9a\u4e49\u7c7b\u7684\u5b9e\u4f8b\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\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>\u5047\u8bbe\u6709\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u5e8f\u5217\u5316\u6587\u4ef6myclass.pkl<\/strong><\/h2>\n<p>with open(&#39;myclass.pkl&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    obj = pickle.load(file)<\/p>\n<p>print(f&quot;Name: {obj.name}, Age: {obj.age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cMyClass\u7c7b\u7684\u5b9e\u4f8b\u5bf9\u8c61\u88ab\u6210\u529f\u53cd\u5e8f\u5217\u5316\uff0c\u5e76\u4e14\u4fdd\u7559\u4e86\u539f\u59cb\u7684\u5c5e\u6027\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001JSON\u6a21\u5757\u7684\u53cd\u5e8f\u5217\u5316<\/h3>\n<\/p>\n<p><h4>2.1\u3001json\u6a21\u5757\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>json\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06JSON\u5b57\u7b26\u4e32\u548cPython\u5bf9\u8c61\u76f8\u4e92\u8f6c\u6362\u7684\u65b9\u6cd5\u3002JSON\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e7f\u6cdb\u7528\u4e8eWeb\u5f00\u53d1\u548cAPI\u6570\u636e\u4ea4\u6362\u3002json\u6a21\u5757\u4e3b\u8981\u652f\u6301\u7b80\u5355\u6570\u636e\u7c7b\u578b\uff0c\u5982\u5217\u8868\u3001\u5b57\u5178\u3001\u5b57\u7b26\u4e32\u3001\u6570\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2.2\u3001\u53cd\u5e8f\u5217\u5316\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528json\u6a21\u5757\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u4e2aJSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_str = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;}&#39;<\/p>\n<p>data = json.loads(json_str)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>json.loads()<\/code>\u65b9\u6cd5\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5b57\u5178\u3002<code>json.loads()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u548c\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>2.3\u3001\u4ece\u6587\u4ef6\u4e2d\u53cd\u5e8f\u5217\u5316<\/h4>\n<\/p>\n<p><p>json\u6a21\u5757\u8fd8\u652f\u6301\u4ece\u6587\u4ef6\u4e2d\u53cd\u5e8f\u5217\u5316JSON\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u4e2a\u5df2\u5b58\u5728\u7684JSON\u6587\u4ef6data.json<\/strong><\/h2>\n<p>with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>json.load()<\/code>\u65b9\u6cd5\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7dJSON\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001YAML\u6a21\u5757\u7684\u53cd\u5e8f\u5217\u5316<\/h3>\n<\/p>\n<p><h4>3.1\u3001yaml\u6a21\u5757\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>yaml\u6a21\u5757\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u9700\u8981\u901a\u8fc7pip\u5b89\u88c5\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06YAML\u6570\u636e\u53cd\u5e8f\u5217\u5316\u4e3aPython\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002YAML\u662f\u4e00\u79cd\u4eba\u7c7b\u53ef\u8bfb\u7684\u6570\u636e\u5e8f\u5217\u5316\u683c\u5f0f\uff0c\u5e38\u7528\u4e8e\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>3.2\u3001\u5b89\u88c5yaml\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528yaml\u6a21\u5757\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u8be5\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pyyaml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3\u3001\u53cd\u5e8f\u5217\u5316\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528yaml\u6a21\u5757\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u4e2aYAML\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>yaml_str = &quot;&quot;&quot;<\/p>\n<p>name: Alice<\/p>\n<p>age: 25<\/p>\n<p>city: New York<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>data = yaml.<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_load(yaml_str)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>yaml.safe_load()<\/code>\u65b9\u6cd5\u5c06YAML\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5b57\u5178\u3002<code>yaml.safe_load()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u548c\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>3.4\u3001\u4ece\u6587\u4ef6\u4e2d\u53cd\u5e8f\u5217\u5316<\/h4>\n<\/p>\n<p><p>yaml\u6a21\u5757\u8fd8\u652f\u6301\u4ece\u6587\u4ef6\u4e2d\u53cd\u5e8f\u5217\u5316YAML\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u4e2a\u5df2\u5b58\u5728\u7684YAML\u6587\u4ef6data.yaml<\/strong><\/h2>\n<p>with open(&#39;data.yaml&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = yaml.safe_load(file)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>yaml.safe_load()<\/code>\u65b9\u6cd5\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7dYAML\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001Protobuf\u6a21\u5757\u7684\u53cd\u5e8f\u5217\u5316<\/h3>\n<\/p>\n<p><h4>4.1\u3001protobuf\u6a21\u5757\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>protobuf\uff08Protocol Buffers\uff09\u662f\u7531Google\u5f00\u53d1\u7684\u4e00\u79cd\u9ad8\u6548\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u5316\u683c\u5f0f\u3002\u5b83\u9002\u7528\u4e8e\u8de8\u8bed\u8a00\u6570\u636e\u4ea4\u6362\uff0c\u5177\u6709\u9ad8\u6548\u3001\u7d27\u51d1\u3001\u53ef\u6269\u5c55\u7684\u7279\u70b9\u3002protobuf\u9700\u8981\u4f7f\u7528.proto\u6587\u4ef6\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\uff0c\u5e76\u751f\u6210\u76f8\u5e94\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>4.2\u3001\u5b89\u88c5protobuf\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528protobuf\u6a21\u5757\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u8be5\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install protobuf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.3\u3001\u5b9a\u4e49.proto\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a.proto\u6587\u4ef6\uff0c\u4f8b\u5982<code>example.proto<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-protobuf\">syntax = &quot;proto3&quot;;<\/p>\n<p>message Person {<\/p>\n<p>    string name = 1;<\/p>\n<p>    int32 age = 2;<\/p>\n<p>    string city = 3;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.4\u3001\u751f\u6210Python\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528protoc\u7f16\u8bd1\u5668\u751f\u6210Python\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">protoc --python_out=. example.proto<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.5\u3001\u53cd\u5e8f\u5217\u5316\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528protobuf\u6a21\u5757\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import example_pb2<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u5e8f\u5217\u5316\u6587\u4ef6person.bin<\/strong><\/h2>\n<p>with open(&#39;person.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    person = example_pb2.Person()<\/p>\n<p>    person.ParseFromString(file.read())<\/p>\n<p>print(f&quot;Name: {person.name}, Age: {person.age}, City: {person.city}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>ParseFromString()<\/code>\u65b9\u6cd5\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u53cd\u5e8f\u5217\u5316\u4e3aPerson\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5b9e\u73b0\u53cd\u5e8f\u5217\u5316\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ecpickle\u3001json\u3001yaml\u548cprotobuf\u7b49\u6a21\u5757\u3002\u9009\u62e9\u5408\u9002\u7684\u53cd\u5e8f\u5217\u5316\u65b9\u6cd5\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u683c\u5f0f\u6765\u51b3\u5b9a\u3002pickle\u6a21\u5757\u9002\u7528\u4e8ePython\u7279\u6709\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u4e0d\u9002\u7528\u4e8e\u8de8\u8bed\u8a00\u6570\u636e\u4ea4\u6362\u3002json\u6a21\u5757\u9002\u7528\u4e8e\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7684\u6570\u636e\u4ea4\u6362\uff0c\u4e3b\u8981\u652f\u6301\u7b80\u5355\u6570\u636e\u7ed3\u6784\u3002yaml\u6a21\u5757\u9002\u7528\u4e8e\u914d\u7f6e\u6587\u4ef6\uff0c\u5177\u6709\u8f83\u597d\u7684\u53ef\u8bfb\u6027\u3002protobuf\u6a21\u5757\u9002\u7528\u4e8e\u9ad8\u6548\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u5316\uff0c\u9002\u5408\u8de8\u8bed\u8a00\u6570\u636e\u4ea4\u6362\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u53cd\u5e8f\u5217\u5316\u5728Python\u4e2d\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u53cd\u5e8f\u5217\u5316\u662f\u5c06\u5b57\u8282\u6d41\u6216\u5b57\u7b26\u4e32\u8f6c\u6362\u56de\u539f\u59cb\u5bf9\u8c61\u7684\u8fc7\u7a0b\u3002\u5728Python\u4e2d\uff0c\u53cd\u5e8f\u5217\u5316\u901a\u5e38\u4f7f\u7528<code>pickle<\/code>\u6a21\u5757\u6216<code>json<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u8fd9\u4e9b\u6a21\u5757\uff0c\u53ef\u4ee5\u5c06\u5b58\u50a8\u5728\u6587\u4ef6\u6216\u5176\u4ed6\u4ecb\u8d28\u4e2d\u7684\u6570\u636e\u6062\u590d\u4e3aPython\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u8fdb\u884c\u540e\u7eed\u5904\u7406\u548c\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528<code>pickle<\/code>\u6a21\u5757\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528<code>pickle<\/code>\u6a21\u5757\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>pickle.load()<\/code>\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5df2\u5e8f\u5217\u5316\u7684\u6570\u636e\uff0c\u6216\u8005\u4f7f\u7528<code>pickle.loads()<\/code>\u4ece\u5b57\u8282\u6d41\u4e2d\u8bfb\u53d6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pickle\n\n# \u4ece\u6587\u4ef6\u4e2d\u53cd\u5e8f\u5217\u5316\nwith open(&#39;data.pkl&#39;, &#39;rb&#39;) as file:\n    data = pickle.load(file)\n\n# \u4ece\u5b57\u8282\u6d41\u4e2d\u53cd\u5e8f\u5217\u5316\nbyte_data = b&#39;\\x80\\x03X\\x05\\x00\\x00\\x00Helloq\\x00.&#39;\ndata = pickle.loads(byte_data)\n<\/code><\/pre>\n<p><strong>\u53cd\u5e8f\u5217\u5316\u4f1a\u9047\u5230\u54ea\u4e9b\u6f5c\u5728\u98ce\u9669\uff1f<\/strong><\/p>\n<p>\u53cd\u5e8f\u5217\u5316\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5e26\u6765\u5b89\u5168\u98ce\u9669\uff0c\u5c24\u5176\u662f\u5f53\u5904\u7406\u6765\u81ea\u4e0d\u53ef\u4fe1\u6765\u6e90\u7684\u6570\u636e\u65f6\u3002\u5728\u53cd\u5e8f\u5217\u5316\u8fc7\u7a0b\u4e2d\uff0c\u6076\u610f\u6784\u9020\u7684\u6570\u636e\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u6267\u884c\u6216\u6570\u636e\u635f\u574f\u3002\u4e3a\u4e86\u964d\u4f4e\u98ce\u9669\uff0c\u5efa\u8bae\u53ea\u53cd\u5e8f\u5217\u5316\u6765\u81ea\u53ef\u4fe1\u6765\u6e90\u7684\u6570\u636e\uff0c\u6216\u8005\u4f7f\u7528\u66f4\u5b89\u5168\u7684\u683c\u5f0f\uff0c\u5982JSON\uff0c\u6765\u66ff\u4ee3<code>pickle<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\u4e5f\u5f88\u7b80\u5355\uff0c\u901a\u5e38\u4f7f\u7528<code>json.load()<\/code>\u4ece\u6587\u4ef6\u8bfb\u53d6JSON\u683c\u5f0f\u7684\u6570\u636e\uff0c\u6216\u8005\u4f7f\u7528<code>json.loads()<\/code>\u4ece\u5b57\u7b26\u4e32\u4e2d\u8bfb\u53d6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\n# \u4ece\u6587\u4ef6\u4e2d\u53cd\u5e8f\u5217\u5316\nwith open(&#39;data.json&#39;, &#39;r&#39;) as file:\n    data = json.load(file)\n\n# \u4ece\u5b57\u7b26\u4e32\u4e2d\u53cd\u5e8f\u5217\u5316\njson_data = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}&#39;\ndata = json.loads(json_data)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u76f8\u5bf9\u5b89\u5168\uff0c\u9002\u5408\u5904\u7406\u6765\u81ea\u5916\u90e8\u6765\u6e90\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u53cd\u5e8f\u5217\u5316\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528pickle\u3001json\u3001yaml\u7b49\u5e93\uff0c\u5206\u522b\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e8f\u5217\u5316\u683c\u5f0f\u3002pi [&hellip;]","protected":false},"author":3,"featured_media":1028412,"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\/1028406"}],"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=1028406"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028406\/revisions"}],"predecessor-version":[{"id":1028416,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028406\/revisions\/1028416"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1028412"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1028406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1028406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1028406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}