{"id":50792,"date":"2024-12-03T16:02:19","date_gmt":"2024-12-03T08:02:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50792\/"},"modified":"2024-12-03T16:02:19","modified_gmt":"2024-12-03T08:02:19","slug":"python-%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-struct-%e6%a8%a1%e5%9d%97%e5%86%99%e5%85%a5%e7%9c%9f%e6%ad%a3%e7%9a%84%e4%ba%8c%e8%bf%9b%e5%88%b6%e5%ba%8f%e5%88%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50792\/","title":{"rendered":"Python \u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 struct \u6a21\u5757\u5199\u5165\u771f\u6b63\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 struct \u6a21\u5757\u5199\u5165\u771f\u6b63\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPython \u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 struct \u6a21\u5757\u5199\u5165\u771f\u6b63\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241128\/17327569066747c5aa285a0.jpg\" class=\"aligncenter\" title=\"Python \u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 struct \u6a21\u5757\u5199\u5165\u771f\u6b63\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\uff1f\u63d2\u56fe\" alt=\"Python \u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 struct \u6a21\u5757\u5199\u5165\u771f\u6b63\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>python \u4e8c\u8fdb\u5236\u6587\u4ef6\u5199\u5165<\/strong><\/p>\n<p>\u5728 python \u4e2d\uff0c\u4f7f\u7528 struct \u6a21\u5757\u5b58\u50a8\u5b57\u7b26\u4e32\u65f6\uff0c\u5b9e\u9645\u5199\u5165\u7684\u5185\u5bb9\u4ec5\u4e3a\u5b57\u8282\uff0c\u800c\u975e\u4e8c\u8fdb\u5236\u5e8f\u5217\u3002\u90a3\u4e48\uff0c\u5982\u4f55\u5199\u5165\u771f\u6b63\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u5462\uff1f<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 struct \u6a21\u5757\u5199\u5165\u4e8c\u8fdb\u5236\u5e8f\u5217\uff080101\uff09\uff1a<\/p>\n<pre>import struct\n\na_list = [33, 1, 1, 1, 4]\nwith open(\"d:\\a.bin\", \"wb\") as fp:\n    for x in a_list:\n        s = struct.pack('b', x)\n        fp.write(s)\nfp.close()\n\nprint(\"done\")<\/pre>\n<p><strong>\u89e3\u6790<\/strong><\/p>\n<ul>\n<li>struct.pack(&#8216;b&#8217;, x) \u5c06\u6574\u6570 x \u6253\u5305\u4e3a\u4e00\u4e2a\u5b57\u8282\u3002\u5b57\u8282\u4e2d\u7684\u6700\u4f4e\u4f4d\u8868\u793a x \u7684\u4e8c\u8fdb\u5236\u5f62\u5f0f\u3002<\/li>\n<li>\u4f7f\u7528 open() \u6253\u5f00\u6587\u4ef6\u5e76\u6307\u5b9a &#8220;wb&#8221; \u6a21\u5f0f\uff0c\u8868\u793a\u4ee5\u4e8c\u8fdb\u5236\u5199\u65b9\u5f0f\u6253\u5f00\u3002<\/li>\n<li>\u5faa\u73af\u904d\u5386\u5217\u8868 a_list\uff0c\u5c06\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u8282\u5e76\u5199\u5165\u6587\u4ef6\u4e2d\u3002<\/li>\n<li>ultraedit \u662f\u4e00\u6b3e\u6587\u672c\u7f16\u8f91\u5668\uff0c\u53ef\u4ee5\u4ee5\u5341\u516d\u8fdb\u5236\u67e5\u770b\u6587\u4ef6\u5185\u5bb9\uff0c\u9a8c\u8bc1\u5199\u5165\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPython \u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 struct \u6a21\u5757\u5199\u5165\u771f\u6b63\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 struct \u6a21\u5757\u5199\u5165\u771f\u6b63\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\uff1f \u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPython \u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 struct \u6a21\u5757\u5199\u5165\u771f\u6b63\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002 python \u4e8c\u8fdb\u5236\u6587\u4ef6\u5199\u5165 \u5728 python \u4e2d\uff0c\u4f7f\u7528 struct \u6a21\u5757\u5b58\u50a8\u5b57\u7b26\u4e32\u65f6\uff0c\u5b9e\u9645\u5199\u5165\u7684\u5185\u5bb9\u4ec5\u4e3a\u5b57\u8282\uff0c\u800c\u975e\u4e8c\u8fdb\u5236\u5e8f\u5217\u3002\u90a3\u4e48\uff0c\u5982\u4f55\u5199\u5165\u771f\u6b63\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u5462\uff1f \u89e3\u51b3\u65b9\u6848 \u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 struct \u6a21\u5757\u5199\u5165\u4e8c\u8fdb\u5236\u5e8f\u5217\uff080101\uff09\uff1a import struct a_list = [33, 1, 1, 1, 4] with open(&#8220;d:\\a.bin&#8221;, &#8220;wb&#8221;) as fp: for x in a_list: s = struct.pack(&#8216;b&#8217;, x) fp.write(s) fp.close() print(&#8220;done&#8221;) \u89e3\u6790 struct.pack(&#8216;b&#8217;, x) \u5c06\u6574\u6570 x \u6253\u5305\u4e3a\u4e00\u4e2a\u5b57\u8282\u3002\u5b57\u8282\u4e2d\u7684\u6700\u4f4e\u4f4d\u8868\u793a x \u7684\u4e8c\u8fdb\u5236\u5f62\u5f0f\u3002 \u4f7f\u7528 open() \u6253\u5f00\u6587\u4ef6\u5e76\u6307\u5b9a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-50792","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=50792"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50792\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}