{"id":1064979,"date":"2024-12-31T16:13:47","date_gmt":"2024-12-31T08:13:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1064979.html"},"modified":"2024-12-31T16:13:49","modified_gmt":"2024-12-31T08:13:49","slug":"python%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98%e6%9f%90%e5%8f%98%e9%87%8f%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1064979.html","title":{"rendered":"python\u5982\u4f55\u4fdd\u5b58\u67d0\u53d8\u91cf\u6570\u636e\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/65c9347f-c39c-44d9-a79d-3d9296f7baae.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u4fdd\u5b58\u67d0\u53d8\u91cf\u6570\u636e\u7c7b\u578b\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<\/p>\n<p><strong>Python\u4fdd\u5b58\u53d8\u91cf\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u6709\u4f7f\u7528\u5185\u7f6e\u51fd\u6570type()\u3001\u5229\u7528pickle\u6a21\u5757\u8fdb\u884c\u5e8f\u5217\u5316\u3001\u901a\u8fc7json\u6a21\u5757\u5c06\u53d8\u91cf\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b\u548c\u65b9\u6cd5\u4fdd\u5b58\u53d8\u91cf\u7c7b\u578b<\/strong>\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570type()\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u4f46\u5b83\u53ea\u8fd4\u56de\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u540d\u79f0\u3002\u5982\u679c\u9700\u8981\u4fdd\u5b58\u53d8\u91cf\u7684\u6570\u636e\u548c\u7c7b\u578b\uff0cpickle\u6a21\u5757\u548cjson\u6a21\u5757\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002pickle\u6a21\u5757\u53ef\u4ee5\u5c06\u53d8\u91cf\u5e8f\u5217\u5316\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f\u5e76\u4fdd\u5b58\u5728\u6587\u4ef6\u4e2d\uff0c\u800cjson\u6a21\u5757\u53ef\u4ee5\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u5e76\u4fdd\u5b58\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5185\u7f6e\u51fd\u6570type()\u7684\u4f7f\u7528<\/p>\n<p>\u5185\u7f6e\u51fd\u6570type()\u662f\u83b7\u53d6Python\u53d8\u91cf\u6570\u636e\u7c7b\u578b\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u8fd4\u56de\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u4e0d\u80fd\u4fdd\u5b58\u53d8\u91cf\u7684\u6570\u636e\u3002\u8981\u4fdd\u5b58\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u5c06type()\u7684\u8f93\u51fa\u4e0e\u53d8\u91cf\u503c\u4e00\u8d77\u4fdd\u5b58\u5230\u5b57\u5178\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">variable = 123<\/p>\n<p>variable_type = type(variable)<\/p>\n<p>print(variable_type)  # &lt;class &#39;int&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u5b58\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">variable_info = {<\/p>\n<p>    &#39;value&#39;: variable,<\/p>\n<p>    &#39;type&#39;: variable_type<\/p>\n<p>}<\/p>\n<p>print(variable_info)  # {&#39;value&#39;: 123, &#39;type&#39;: &lt;class &#39;int&#39;&gt;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u662f\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4f46\u5b83\u4e0d\u80fd\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528pickle\u6a21\u5757\u8fdb\u884c\u5e8f\u5217\u5316<\/p>\n<p>pickle\u6a21\u5757\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u4fdd\u5b58\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u4fdd\u5b58\u53d8\u91cf\u7684\u503c\u3002\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>variable = 123<\/p>\n<p>with open(&#39;variable.pkl&#39;, &#39;wb&#39;) as f:<\/p>\n<p>    pickle.dump(variable, f)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8981\u8bfb\u53d6\u4fdd\u5b58\u7684\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;variable.pkl&#39;, &#39;rb&#39;) as f:<\/p>\n<p>    loaded_variable = pickle.load(f)<\/p>\n<p>print(loaded_variable)  # 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pickle\u6a21\u5757\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u4fdd\u5b58\u548c\u6062\u590d\u590d\u6742\u7684Python\u5bf9\u8c61\uff0c\u5305\u62ec\u7c7b\u5b9e\u4f8b\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002\u7f3a\u70b9\u662f\u4fdd\u5b58\u7684\u6570\u636e\u662f\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u4e0d\u4fbf\u4e8e\u4eba\u7c7b\u9605\u8bfb\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528json\u6a21\u5757\u8fdb\u884c\u5e8f\u5217\u5316<\/p>\n<p>json\u6a21\u5757\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002JSON\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u53ef\u8bfb\u6027\u9ad8\uff0c\u975e\u5e38\u9002\u5408\u4fdd\u5b58\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>variable = 123<\/p>\n<p>variable_info = {<\/p>\n<p>    &#39;value&#39;: variable,<\/p>\n<p>    &#39;type&#39;: str(type(variable))<\/p>\n<p>}<\/p>\n<p>with open(&#39;variable.json&#39;, &#39;w&#39;) as f:<\/p>\n<p>    json.dump(variable_info, f)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8981\u8bfb\u53d6\u4fdd\u5b58\u7684\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;variable.json&#39;, &#39;r&#39;) as f:<\/p>\n<p>    loaded_variable_info = json.load(f)<\/p>\n<p>loaded_variable = loaded_variable_info[&#39;value&#39;]<\/p>\n<p>loaded_variable_type = loaded_variable_info[&#39;type&#39;]<\/p>\n<p>print(loaded_variable)  # 123<\/p>\n<p>print(loaded_variable_type)  # &lt;class &#39;int&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>json\u6a21\u5757\u7684\u4f18\u70b9\u662f\u4fdd\u5b58\u7684\u6570\u636e\u662f\u6587\u672c\u683c\u5f0f\uff0c\u4fbf\u4e8e\u9605\u8bfb\u548c\u7f16\u8f91\u3002\u7f3a\u70b9\u662f\u4e0d\u80fd\u76f4\u63a5\u4fdd\u5b58Python\u5bf9\u8c61\uff0c\u53ea\u80fd\u4fdd\u5b58\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u81ea\u5b9a\u4e49\u7c7b\u548c\u65b9\u6cd5\u4fdd\u5b58\u53d8\u91cf\u7c7b\u578b<\/p>\n<p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u7c7b\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u4fdd\u5b58\u548c\u7ba1\u7406\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class VariableSaver:<\/p>\n<p>    def __init__(self, variable):<\/p>\n<p>        self.variable = variable<\/p>\n<p>        self.variable_type = type(variable)<\/p>\n<p>    def save(self, filename):<\/p>\n<p>        with open(filename, &#39;w&#39;) as f:<\/p>\n<p>            f.write(f&#39;{self.variable}\\n&#39;)<\/p>\n<p>            f.write(f&#39;{self.variable_type}\\n&#39;)<\/p>\n<p>    def load(self, filename):<\/p>\n<p>        with open(filename, &#39;r&#39;) as f:<\/p>\n<p>            self.variable = f.readline().strip()<\/p>\n<p>            self.variable_type = f.readline().strip()<\/p>\n<p>variable = 123<\/p>\n<p>saver = VariableSaver(variable)<\/p>\n<p>saver.save(&#39;variable.txt&#39;)<\/p>\n<p>saver.load(&#39;variable.txt&#39;)<\/p>\n<p>print(saver.variable)  # 123<\/p>\n<p>print(saver.variable_type)  # &lt;class &#39;int&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9a\u5236\u4fdd\u5b58\u548c\u8bfb\u53d6\u7684\u903b\u8f91\u3002\u7f3a\u70b9\u662f\u9700\u8981\u7f16\u5199\u66f4\u591a\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<p>\u5728Python\u4e2d\u4fdd\u5b58\u53d8\u91cf\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570type()\u3001\u5229\u7528pickle\u6a21\u5757\u8fdb\u884c\u5e8f\u5217\u5316\u3001\u901a\u8fc7json\u6a21\u5757\u5c06\u53d8\u91cf\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b\u548c\u65b9\u6cd5\u4fdd\u5b58\u53d8\u91cf\u7c7b\u578b<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u3002\u5982\u679c\u53ea\u662f\u7b80\u5355\u5730\u83b7\u53d6\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f7f\u7528type()\u51fd\u6570\u662f\u6700\u65b9\u4fbf\u7684\u3002\u5982\u679c\u9700\u8981\u5c06\u53d8\u91cf\u7684\u6570\u636e\u548c\u7c7b\u578b\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0cpickle\u548cjson\u6a21\u5757\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u5982\u679c\u9700\u8981\u9ad8\u5ea6\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b\u548c\u65b9\u6cd5\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>type()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>type(my_variable)<\/code>\u5c06\u8fd4\u56de\u8be5\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u5982\u679c\u9700\u8981\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u5c5e\u4e8e\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u5c06\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u4fdd\u5b58\u5230\u6587\u672c\u6587\u4ef6\u4e2d\u3002\u9996\u5148\uff0c\u901a\u8fc7<code>type()<\/code>\u51fd\u6570\u83b7\u53d6\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff0c\u7136\u540e\u5c06\u5176\u5199\u5165\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u7c7b\u578b\u4fdd\u5b58\u5230\u540d\u4e3a<code>data_type.txt<\/code>\u7684\u6587\u4ef6\u4e2d\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;data_type.txt&#39;, &#39;w&#39;) as file:\n    file.write(str(type(my_variable)))\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8f6c\u6362\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f7f\u7528<code>str()<\/code>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528<code>float()<\/code>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u8f6c\u6362\u65f6\u9700\u8981\u786e\u4fdd\u6570\u636e\u80fd\u591f\u6709\u6548\u5730\u8f6c\u6362\u4e3a\u76ee\u6807\u7c7b\u578b\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a Python\u4fdd\u5b58\u53d8\u91cf\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u6709\u4f7f\u7528\u5185\u7f6e\u51fd\u6570type()\u3001\u5229\u7528pickle\u6a21\u5757\u8fdb\u884c\u5e8f\u5217\u5316\u3001\u901a\u8fc7 [&hellip;]","protected":false},"author":3,"featured_media":1064988,"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\/1064979"}],"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=1064979"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064979\/revisions"}],"predecessor-version":[{"id":1064994,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064979\/revisions\/1064994"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1064988"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1064979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1064979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1064979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}