{"id":1045415,"date":"2024-12-31T13:22:52","date_gmt":"2024-12-31T05:22:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1045415.html"},"modified":"2024-12-31T13:22:54","modified_gmt":"2024-12-31T05:22:54","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86byte%e8%bd%ac%e6%8d%a2%e4%b8%balist","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1045415.html","title":{"rendered":"python\u5982\u4f55\u5c06byte\u8f6c\u6362\u4e3alist"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/f887e597-1235-44ad-a74c-5ac5f3435b8e.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06byte\u8f6c\u6362\u4e3alist\" \/><\/p>\n<p><p> \u8981\u5c06Python\u4e2d\u7684\u5b57\u8282\uff08byte\uff09\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\uff08list\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002<strong>\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684 <code>list()<\/code> \u51fd\u6570\u3001\u4f7f\u7528 <code>bytearray<\/code>\u3001\u4ee5\u53ca\u901a\u8fc7\u624b\u52a8\u8fed\u4ee3\u5b57\u8282\u5bf9\u8c61\u3002<\/strong> \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u8ba8\u8bba\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u5728\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u3001\u6587\u4ef6\u64cd\u4f5c\u3001\u7f51\u7edc\u6570\u636e\u4f20\u8f93\u7b49\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u9010\u4e00\u4ecb\u7ecd\u5404\u79cd\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u6765\u8bf4\u660e\u5b83\u4eec\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528 <code>list()<\/code> \u51fd\u6570<\/p>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u5185\u7f6e\u51fd\u6570 <code>list()<\/code>\uff0c\u5b83\u53ef\u4ee5\u5c06\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u5b57\u8282\u5bf9\u8c61\u662f\u53ef\u8fed\u4ee3\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>list()<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>byte_data = b&#39;\\x00\\x01\\x02\\x03\\x04&#39;<\/p>\n<p>list_data = list(byte_data)<\/p>\n<p>print(list_data)  # \u8f93\u51fa: [0, 1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u548c\u7b80\u6d01\u3002\u5b83\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528 <code>bytearray<\/code><\/p>\n<\/p>\n<p><p><code>bytearray<\/code> \u662f\u4e00\u4e2a\u53ef\u53d8\u7684\u5b57\u8282\u5e8f\u5217\uff0c\u4e0e <code>bytes<\/code> \u7c7b\u4f3c\uff0c\u4f46\u5141\u8bb8\u4fee\u6539\u5176\u5185\u5bb9\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a <code>bytearray<\/code>\uff0c\u7136\u540e\u518d\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>byte_data = b&#39;\\x00\\x01\\x02\\x03\\x04&#39;<\/p>\n<p>byte_array = bytearray(byte_data)<\/p>\n<p>list_data = list(byte_array)<\/p>\n<p>print(list_data)  # \u8f93\u51fa: [0, 1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u5bf9\u5b57\u8282\u6570\u636e\u8fdb\u884c\u4fee\u6539\u65f6\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a <code>bytearray<\/code> \u662f\u53ef\u53d8\u7684\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u624b\u52a8\u8fed\u4ee3\u5b57\u8282\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u7279\u5b9a\u7684\u9700\u6c42\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u624b\u52a8\u8fed\u4ee3\u5b57\u8282\u5bf9\u8c61\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5b57\u8282\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u66f4\u52a0\u7075\u6d3b\uff0c\u4f46\u4e5f\u66f4\u7e41\u7410\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>byte_data = b&#39;\\x00\\x01\\x02\\x03\\x04&#39;<\/p>\n<p>list_data = [byte for byte in byte_data]<\/p>\n<p>print(list_data)  # \u8f93\u51fa: [0, 1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u6211\u4eec\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u989d\u5916\u7684\u5904\u7406\uff0c\u5982\u8fc7\u6ee4\u6216\u8f6c\u6362\u7279\u5b9a\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4f7f\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4f7f\u7528\u573a\u666f\uff0c\u5e76\u7ed3\u5408\u5177\u4f53\u793a\u4f8b\u6765\u8bf4\u660e\u5982\u4f55\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6587\u4ef6\u64cd\u4f5c<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c\u7136\u540e\u8fdb\u884c\u5904\u7406\u3002\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u53ef\u4ee5\u65b9\u4fbf\u6211\u4eec\u5bf9\u6bcf\u4e2a\u5b57\u8282\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>with open(&#39;example.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    byte_data = file.read()<\/p>\n<p>list_data = list(byte_data)<\/p>\n<h2><strong>\u5bf9\u5217\u8868\u4e2d\u7684\u5b57\u8282\u8fdb\u884c\u64cd\u4f5c<\/strong><\/h2>\n<p>print(list_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7f51\u7edc\u6570\u636e\u4f20\u8f93<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u63a5\u6536\u5230\u5b57\u8282\u6d41\u6570\u636e\u3002\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u53ef\u4ee5\u65b9\u4fbf\u6211\u4eec\u89e3\u6790\u548c\u5904\u7406\u63a5\u6536\u5230\u7684\u6570\u636e\u5305\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import socket<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aTCP\/IP\u5957\u63a5\u5b57<\/strong><\/h2>\n<p>sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u670d\u52a1\u5668<\/strong><\/h2>\n<p>server_address = (&#39;localhost&#39;, 10000)<\/p>\n<p>sock.connect(server_address)<\/p>\n<p>try:<\/p>\n<p>    # \u53d1\u9001\u6570\u636e<\/p>\n<p>    message = b&#39;This is a message.&#39;<\/p>\n<p>    sock.sendall(message)<\/p>\n<p>    # \u63a5\u6536\u54cd\u5e94<\/p>\n<p>    data = sock.recv(1024)<\/p>\n<p>    list_data = list(data)<\/p>\n<p>    print(&#39;Received:&#39;, list_data)<\/p>\n<p>finally:<\/p>\n<p>    # \u5173\u95ed\u8fde\u63a5<\/p>\n<p>    sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6570\u636e\u5904\u7406\u548c\u5206\u6790<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u4ee5\u4fbf\u4f7f\u7528 Python \u7684\u6570\u636e\u5206\u6790\u5e93\uff08\u5982 NumPy\u3001Pandas\uff09\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import numpy as np<\/p>\n<p>byte_data = b&#39;\\x00\\x01\\x02\\x03\\x04&#39;<\/p>\n<p>list_data = list(byte_data)<\/p>\n<p>np_array = np.array(list_data)<\/p>\n<p>print(np_array)  # \u8f93\u51fa: [0 1 2 3 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u5728\u5f88\u591a\u5b9e\u9645\u5e94\u7528\u4e2d\u90fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u64cd\u4f5c\u3002<strong>\u4f7f\u7528\u5185\u7f6e\u7684 <code>list()<\/code> \u51fd\u6570\u3001<code>bytearray<\/code>\u3001\u4ee5\u53ca\u624b\u52a8\u8fed\u4ee3\u90fd\u662f\u6709\u6548\u7684\u65b9\u6cd5<\/strong>\uff0c\u5b83\u4eec\u5404\u6709\u4f18\u7f3a\u70b9\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5e76\u4e86\u89e3\u4e86\u5b83\u4eec\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u7684\u5e94\u7528\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406\u5b57\u8282\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u6bcf\u4e2a\u5b57\u8282\u5c06\u4f5c\u4e3a\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u8868\u793a\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u8282\u5bf9\u8c61<code>b&#39;abc&#39;<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>list(b&#39;abc&#39;)<\/code>\u5f97\u5230<code>[97, 98, 99]<\/code>\uff0c\u5176\u4e2d97\u300198\u300199\u662f\u5b57\u6bcd&#39;a&#39;\u3001&#39;b&#39;\u3001&#39;c&#39;\u7684ASCII\u503c\u3002<\/p>\n<p><strong>\u5b57\u8282\u8f6c\u6362\u4e3a\u5217\u8868\u540e\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u8f6c\u6362\u4e3a\u5217\u8868\u540e\uff0c\u60a8\u53ef\u4ee5\u6267\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u5bf9\u5143\u7d20\u8fdb\u884c\u904d\u5386\u3001\u4fee\u6539\u6216\u8fc7\u6ee4\u3002\u5982\u679c\u9700\u8981\u5c06\u5b57\u8282\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u5f62\u5f0f\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>chr()<\/code>\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<code>[chr(byte) for byte in byte_array]<\/code>\uff0c\u8fd9\u5c06\u628a\u5b57\u8282\u503c\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5b57\u7b26\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u7684\u5217\u8868\u80fd\u5426\u518d\u8f6c\u6362\u56de\u5b57\u8282\u5bf9\u8c61\uff1f<\/strong><br \/>\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u56de\u5b57\u8282\u5bf9\u8c61\u3002\u4f7f\u7528<code>bytes()<\/code>\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u6574\u6570\u5217\u8868<code>[97, 98, 99]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>bytes(list_of_integers)<\/code>\u5c06\u5176\u8f6c\u6362\u56de\u5b57\u8282\u5bf9\u8c61<code>b&#39;abc&#39;<\/code>\u3002\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u6709\u6548\u7684\u5b57\u8282\u503c\uff080-255\u4e4b\u95f4\u7684\u6574\u6570\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u4e2d\u7684\u5b57\u8282\uff08byte\uff09\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\uff08list\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684 l [&hellip;]","protected":false},"author":3,"featured_media":1045421,"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\/1045415"}],"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=1045415"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1045415\/revisions"}],"predecessor-version":[{"id":1045422,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1045415\/revisions\/1045422"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1045421"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1045415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1045415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1045415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}