{"id":1095929,"date":"2025-01-08T14:56:09","date_gmt":"2025-01-08T06:56:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095929.html"},"modified":"2025-01-08T14:56:24","modified_gmt":"2025-01-08T06:56:24","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e6%b5%ae%e7%82%b9%e8%bd%ac%e5%8c%96%e4%b8%ba%e5%ad%97%e8%8a%82-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1095929.html","title":{"rendered":"python\u5982\u4f55\u5c06\u6d6e\u70b9\u8f6c\u5316\u4e3a\u5b57\u8282"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211356\/e18abed2-37ce-4713-a651-2ddb1a89469c.webp\" alt=\"python\u5982\u4f55\u5c06\u6d6e\u70b9\u8f6c\u5316\u4e3a\u5b57\u8282\" \/><\/p>\n<p><p> <strong>Python\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528struct\u6a21\u5757\u3001\u4f7f\u7528\u5185\u7f6e\u7684float.hex()\u65b9\u6cd5\u3001\u4f7f\u7528pickle\u6a21\u5757\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528struct\u6a21\u5757\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5bf9C\u8bed\u8a00\u7ed3\u6784\u4f53\u4e2d\u5c01\u88c5\u7684\u6570\u636e\u8fdb\u884c\u6253\u5305\u548c\u89e3\u5305\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528struct\u6a21\u5757\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\uff0c\u5177\u4f53\u65b9\u6cd5\u662f\u4f7f\u7528struct.pack()\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u5c06\u6570\u636e\u6253\u5305\u6210\u5b57\u8282\u5bf9\u8c61\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528struct\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1. struct\u6a21\u5757\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>struct\u6a21\u5757\u7528\u4e8e\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u53ef\u4ee5\u5c06Python\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3aC\u8bed\u8a00\u4e2d\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u5b83\u63d0\u4f9b\u4e86pack()\u548cunpack()\u4e24\u4e2a\u4e3b\u8981\u51fd\u6570\uff0c\u5206\u522b\u7528\u4e8e\u6253\u5305\u548c\u89e3\u5305\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528struct.pack()\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u8282<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<h2><strong>\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>float_number = 3.14159<\/p>\n<h2><strong>\u5c06\u6d6e\u70b9\u6570\u6253\u5305\u6210\u5b57\u8282\u5bf9\u8c61<\/strong><\/h2>\n<p>byte_data = struct.pack(&#39;f&#39;, float_number)<\/p>\n<p>print(byte_data)  # \u8f93\u51fa\uff1ab&#39;\\xd0\\x0fI@&#39;<\/p>\n<h2><strong>\u89e3\u5305\u5b57\u8282\u5bf9\u8c61\u4e3a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>unpacked_float = struct.unpack(&#39;f&#39;, byte_data)[0]<\/p>\n<p>print(unpacked_float)  # \u8f93\u51fa\uff1a3.141590118408203<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>struct.pack(&#39;f&#39;, float_number)<\/code>\u5c06\u6d6e\u70b9\u6570\u6253\u5305\u6210\u5b57\u8282\u5bf9\u8c61\uff0c\u5176\u4e2d<code>&#39;f&#39;<\/code>\u8868\u793a\u5355\u7cbe\u5ea6\u6d6e\u70b9\u6570\u683c\u5f0f\u3002\u5982\u679c\u9700\u8981\u6253\u5305\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>&#39;d&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3. \u591a\u4e2a\u6d6e\u70b9\u6570\u6253\u5305<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u4e00\u6b21\u6027\u6253\u5305\u591a\u4e2a\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f20\u9012\u591a\u4e2a\u53c2\u6570\u7ed9<code>struct.pack()<\/code>\u51fd\u6570\uff0c\u5e76\u4f7f\u7528\u76f8\u5e94\u7684\u683c\u5f0f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">float_numbers = [3.14159, 2.71828, 1.61803]<\/p>\n<h2><strong>\u5c06\u591a\u4e2a\u6d6e\u70b9\u6570\u6253\u5305\u6210\u5b57\u8282\u5bf9\u8c61<\/strong><\/h2>\n<p>byte_data = struct.pack(&#39;3f&#39;, *float_numbers)<\/p>\n<p>print(byte_data)<\/p>\n<h2><strong>\u89e3\u5305\u5b57\u8282\u5bf9\u8c61\u4e3a\u6d6e\u70b9\u6570\u5217\u8868<\/strong><\/h2>\n<p>unpacked_floats = struct.unpack(&#39;3f&#39;, byte_data)<\/p>\n<p>print(unpacked_floats)  # \u8f93\u51fa\uff1a(3.141590118408203, 2.718280076980591, 1.618030071258545)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528float.hex()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1. float.hex()\u65b9\u6cd5\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p><code>float.hex()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a16\u8fdb\u5236\u8868\u793a\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u53ef\u4ee5\u5c06\u8be5\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982struct\u6a21\u5757\u5e38\u7528\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528float.hex()\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u8282<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6d6e\u70b9\u6570<\/p>\n<p>float_number = 3.14159<\/p>\n<h2><strong>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a16\u8fdb\u5236\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>hex_str = float_number.hex()<\/p>\n<p>print(hex_str)  # \u8f93\u51fa\uff1a0x1.921f9f01b866ep+1<\/p>\n<h2><strong>\u5c0616\u8fdb\u5236\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282<\/strong><\/h2>\n<p>byte_data = hex_str.encode(&#39;utf-8&#39;)<\/p>\n<p>print(byte_data)  # \u8f93\u51fa\uff1ab&#39;0x1.921f9f01b866ep+1&#39;<\/p>\n<h2><strong>\u89e3\u7801\u5b57\u8282\u4e3a16\u8fdb\u5236\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>decoded_hex_str = byte_data.decode(&#39;utf-8&#39;)<\/p>\n<h2><strong>\u5c0616\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u56de\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>unpacked_float = float.fromhex(decoded_hex_str)<\/p>\n<p>print(unpacked_float)  # \u8f93\u51fa\uff1a3.14159<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>float.hex()<\/code>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a16\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528<code>str.encode(&#39;utf-8&#39;)<\/code>\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pickle\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1. pickle\u6a21\u5757\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>pickle\u6a21\u5757\u7528\u4e8e\u5c06Python\u5bf9\u8c61\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\uff0c\u53ef\u4ee5\u5c06\u4efb\u4f55Python\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c\u5e76\u53ef\u4ee5\u4ece\u5b57\u8282\u5bf9\u8c61\u6062\u590dPython\u5bf9\u8c61\u3002\u867d\u7136pickle\u6a21\u5757\u4e0d\u662f\u4e13\u95e8\u4e3a\u6d6e\u70b9\u6570\u8bbe\u8ba1\u7684\uff0c\u4f46\u5b83\u53ef\u4ee5\u5904\u7406\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ec\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528pickle\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u8282<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<h2><strong>\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>float_number = 3.14159<\/p>\n<h2><strong>\u5c06\u6d6e\u70b9\u6570\u5e8f\u5217\u5316\u4e3a\u5b57\u8282\u5bf9\u8c61<\/strong><\/h2>\n<p>byte_data = pickle.dumps(float_number)<\/p>\n<p>print(byte_data)  # \u8f93\u51fa\uff1ab&#39;\\x80\\x04\\x95\\n\\x00\\x00\\x00\\x00\\x00\\x00\\x00G@\\t!\\xf9\\xf0\\x1b\\x86n.&#39;<\/p>\n<h2><strong>\u53cd\u5e8f\u5217\u5316\u5b57\u8282\u5bf9\u8c61\u4e3a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>unpacked_float = pickle.loads(byte_data)<\/p>\n<p>print(unpacked_float)  # \u8f93\u51fa\uff1a3.14159<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pickle.dumps()<\/code>\u5c06\u6d6e\u70b9\u6570\u5e8f\u5217\u5316\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>pickle.loads()<\/code>\u5c06\u5b57\u8282\u5bf9\u8c61\u53cd\u5e8f\u5217\u5316\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d<strong>\u4f7f\u7528struct\u6a21\u5757\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u6cd5<\/strong>\u3002struct\u6a21\u5757\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6253\u5305\u548c\u89e3\u5305\u6d6e\u70b9\u6570\u53ca\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002<strong>\u4f7f\u7528float.hex()\u65b9\u6cd5\u548cpickle\u6a21\u5757<\/strong>\u4e5f\u662f\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u6765\u51b3\u5b9a\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u60a8\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\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>struct<\/code>\u6a21\u5757\u6765\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>struct.pack()<\/code>\u51fd\u6570\uff0c\u6307\u5b9a\u683c\u5f0f\u5b57\u7b26\u6765\u8fdb\u884c\u8f6c\u6362\u3002\u4f8b\u5982\uff0c<code>struct.pack(&#39;f&#39;, your_float)<\/code>\u53ef\u4ee5\u5c06\u5355\u7cbe\u5ea6\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\uff0c\u800c<code>struct.pack(&#39;d&#39;, your_float)<\/code>\u5219\u7528\u4e8e\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u4ee5\u8fdb\u884c\u7f51\u7edc\u4f20\u8f93\u6216\u6587\u4ef6\u5b58\u50a8\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5c06\u5b57\u8282\u8f6c\u6362\u56de\u6d6e\u70b9\u6570\uff1f<\/strong><br \/>\u8981\u5c06\u5b57\u8282\u8f6c\u6362\u56de\u6d6e\u70b9\u6570\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528<code>struct<\/code>\u6a21\u5757\u3002\u4f7f\u7528<code>struct.unpack()<\/code>\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u683c\u5f0f\u5b57\u7b26\u4e0e\u8f6c\u6362\u65f6\u7684\u5b57\u7b26\u76f8\u540c\uff0c\u4f8b\u5982\uff0c<code>struct.unpack(&#39;f&#39;, your_bytes)<\/code>\u5c06\u5b57\u8282\u8f6c\u6362\u56de\u5355\u7cbe\u5ea6\u6d6e\u70b9\u6570\uff0c\u800c<code>struct.unpack(&#39;d&#39;, your_bytes)<\/code>\u7528\u4e8e\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u3002\u8bb0\u5f97\u4f20\u5165\u7684\u5b57\u8282\u6570\u636e\u5e94\u8be5\u662f\u4e0e\u6253\u5305\u65f6\u76f8\u540c\u957f\u5ea6\u7684\u5b57\u8282\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u6d6e\u70b9\u6570\u548c\u5b57\u8282\u8f6c\u6362\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u6ce8\u610f\uff1f<\/strong><br \/>\u5e38\u89c1\u9519\u8bef\u5305\u62ec\u683c\u5f0f\u5b57\u7b26\u4e0d\u5339\u914d\u3001\u5b57\u8282\u957f\u5ea6\u4e0d\u8db3\u6216\u8fc7\u957f\u3001\u4ee5\u53ca\u5728\u4e0d\u540c\u5e73\u53f0\u95f4\u7684\u5b57\u8282\u5e8f\u95ee\u9898\u3002\u786e\u4fdd\u5728\u8f6c\u6362\u65f6\u4f7f\u7528\u6b63\u786e\u7684\u683c\u5f0f\u5b57\u7b26\uff0c\u5e76\u4e14\u5728\u89e3\u5305\u65f6\u5b57\u8282\u957f\u5ea6\u4e0e\u539f\u59cb\u6570\u636e\u4e00\u81f4\u3002\u540c\u65f6\uff0c\u5f53\u5728\u4e0d\u540c\u8ba1\u7b97\u673a\u67b6\u6784\u95f4\u4f20\u8f93\u6570\u636e\u65f6\uff0c\u6ce8\u610f\u5b57\u8282\u5e8f\uff08\u5927\u7aef\u6216\u5c0f\u7aef\uff09\u7684\u5dee\u5f02\uff0c\u4ee5\u907f\u514d\u6570\u636e\u89e3\u91ca\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528struct\u6a21\u5757\u3001\u4f7f\u7528\u5185\u7f6e\u7684float.hex()\u65b9\u6cd5\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1095954,"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\/1095929"}],"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=1095929"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095929\/revisions"}],"predecessor-version":[{"id":1095962,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095929\/revisions\/1095962"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095954"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}