{"id":1143994,"date":"2025-01-08T22:55:46","date_gmt":"2025-01-08T14:55:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1143994.html"},"modified":"2025-01-08T22:55:48","modified_gmt":"2025-01-08T14:55:48","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%8f%96%e4%b8%80%e4%b8%aa%e5%ad%97%e8%8a%82%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1143994.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u53d6\u4e00\u4e2a\u5b57\u8282\u7684\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181327\/662fb650-f421-4e6e-a05d-104aa16f814f.webp\" alt=\"python\u4e2d\u5982\u4f55\u53d6\u4e00\u4e2a\u5b57\u8282\u7684\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53d6\u4e00\u4e2a\u5b57\u8282\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u8282\u5e8f\u5217\uff08bytes\uff09\u3001\u5b57\u8282\u6570\u7ec4\uff08bytearray\uff09\u6216\u901a\u8fc7\u6587\u4ef6\u64cd\u4f5c\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5b57\u8282\u5e8f\u5217\uff08bytes\uff09\u548c\u5b57\u8282\u6570\u7ec4\uff08bytearray\uff09\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5<\/strong>\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u7d22\u5f15\u6216\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u4e00\u4e2a\u5b57\u8282\u7684\u6570\u636e\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u5c55\u5f00\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u8282\u5e8f\u5217\uff08bytes\uff09\u548c\u5b57\u8282\u6570\u7ec4\uff08bytearray\uff09<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684<code>bytes<\/code>\u7c7b\u578b\u662f\u4e0d\u53ef\u53d8\u7684\u5b57\u8282\u5e8f\u5217\uff0c\u800c<code>bytearray<\/code>\u7c7b\u578b\u662f\u53ef\u53d8\u7684\u5b57\u8282\u5e8f\u5217\u3002\u5b83\u4eec\u90fd\u652f\u6301\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5355\u4e2a\u5b57\u8282\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b57\u8282\u5e8f\u5217\uff08bytes\uff09<\/h4>\n<\/p>\n<p><p>\u5b57\u8282\u5e8f\u5217\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u76f4\u89c2\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6216\u5207\u7247\u7684\u65b9\u5f0f\u8bbf\u95ee\u5176\u4e2d\u7684\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u5e8f\u5217<\/p>\n<p>byte_sequence = b&#39;hello world&#39;<\/p>\n<h2><strong>\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6\u4e00\u4e2a\u5b57\u8282<\/strong><\/h2>\n<p>single_byte = byte_sequence[0]  # 104 (&#39;h&#39;\u7684ASCII\u7801)<\/p>\n<p>print(single_byte)<\/p>\n<h2><strong>\u901a\u8fc7\u5207\u7247\u83b7\u53d6\u591a\u4e2a\u5b57\u8282<\/strong><\/h2>\n<p>byte_slice = byte_sequence[1:4]  # b&#39;ell&#39;<\/p>\n<p>print(byte_slice)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b57\u8282\u6570\u7ec4\uff08bytearray\uff09<\/h4>\n<\/p>\n<p><p>\u5b57\u8282\u6570\u7ec4\u4e0e\u5b57\u8282\u5e8f\u5217\u7c7b\u4f3c\uff0c\u4f46\u5b83\u662f\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4<\/p>\n<p>byte_array = bytearray(b&#39;hello world&#39;)<\/p>\n<h2><strong>\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6\u4e00\u4e2a\u5b57\u8282<\/strong><\/h2>\n<p>single_byte = byte_array[0]  # 104 (&#39;h&#39;\u7684ASCII\u7801)<\/p>\n<p>print(single_byte)<\/p>\n<h2><strong>\u4fee\u6539\u4e00\u4e2a\u5b57\u8282<\/strong><\/h2>\n<p>byte_array[0] = 72  # \u5c06&#39;h&#39;\u6539\u4e3a&#39;H&#39;<\/p>\n<p>print(byte_array)  # bytearray(b&#39;Hello world&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u8282<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u9700\u8981\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u8282\u6570\u636e\u3002Python\u5185\u7f6e\u7684<code>open<\/code>\u51fd\u6570\u53ef\u4ee5\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7<code>read<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6\u5355\u4e2a\u5b57\u8282<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6<\/p>\n<p>with open(&#39;example.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u4e00\u4e2a\u5b57\u8282<\/p>\n<p>    single_byte = file.read(1)<\/p>\n<p>    print(single_byte)  # b&#39;\\x00&#39;\uff08\u5047\u8bbe\u6587\u4ef6\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u662f0x00\uff09<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6\u591a\u4e2a\u5b57\u8282<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6<\/p>\n<p>with open(&#39;example.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    # \u8bfb\u53d6\u591a\u4e2a\u5b57\u8282<\/p>\n<p>    byte_data = file.read(4)<\/p>\n<p>    print(byte_data)  # b&#39;\\x00\\x01\\x02\\x03&#39;\uff08\u5047\u8bbe\u6587\u4ef6\u7684\u524d\u56db\u4e2a\u5b57\u8282\u662f0x00, 0x01, 0x02, 0x03\uff09<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u7f51\u7edc\u6570\u636e\u5305\u83b7\u53d6\u5b57\u8282<\/h3>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u4ece\u7f51\u7edc\u6570\u636e\u5305\u4e2d\u63d0\u53d6\u5b57\u8282\u6570\u636e\u3002Python\u7684<code>socket<\/code>\u5e93\u63d0\u4f9b\u4e86\u8fd9\u79cd\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aTCP\/IP socket<\/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 the message.&#39;<\/p>\n<p>    sock.sendall(message)<\/p>\n<p>    # \u63a5\u6536\u6570\u636e<\/p>\n<p>    data = sock.recv(1)  # \u63a5\u6536\u4e00\u4e2a\u5b57\u8282<\/p>\n<p>    print(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<p><h3>\u56db\u3001\u5b57\u8282\u4e0e\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u8282\u6570\u636e\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5728\u5b57\u8282\u548c\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e9b\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b57\u8282\u4e0e\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u8f6c\u6362<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b57\u8282\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\n<p>byte_data = b&#39;hello world&#39;<\/p>\n<p>string_data = byte_data.decode(&#39;utf-8&#39;)<\/p>\n<p>print(string_data)  # &#39;hello world&#39;<\/p>\n<h2><strong>\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282<\/strong><\/h2>\n<p>string_data = &#39;hello world&#39;<\/p>\n<p>byte_data = string_data.encode(&#39;utf-8&#39;)<\/p>\n<p>print(byte_data)  # b&#39;hello world&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b57\u8282\u4e0e\u6574\u6570\u4e4b\u95f4\u7684\u8f6c\u6362<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b57\u8282\u8f6c\u6362\u4e3a\u6574\u6570<\/p>\n<p>byte_data = b&#39;\\x01\\x00&#39;<\/p>\n<p>integer_data = int.from_bytes(byte_data, byteorder=&#39;little&#39;)<\/p>\n<p>print(integer_data)  # 1<\/p>\n<h2><strong>\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u8282<\/strong><\/h2>\n<p>integer_data = 1<\/p>\n<p>byte_data = integer_data.to_bytes(2, byteorder=&#39;little&#39;)<\/p>\n<p>print(byte_data)  # b&#39;\\x01\\x00&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7ed3\u6784\u5316\u6570\u636e\u5904\u7406\u5e93<\/h3>\n<\/p>\n<p><p>Python\u7684<code>struct<\/code>\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u7ed3\u6784\u5316\u7684\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4f8b\u5982\u4ece\u5b57\u8282\u6570\u636e\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<h2><strong>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u6574\u6570\u7684\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>byte_data = b&#39;\\x01\\x00\\x00\\x00\\x02\\x00\\x00\\x00&#39;<\/p>\n<h2><strong>\u4f7f\u7528struct\u5e93\u89e3\u5305\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>unpacked_data = struct.unpack(&#39;ii&#39;, byte_data)<\/p>\n<p>print(unpacked_data)  # (1, 2)<\/p>\n<h2><strong>\u5c06\u6574\u6570\u6253\u5305\u6210\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>packed_data = struct.pack(&#39;ii&#39;, 1, 2)<\/p>\n<p>print(packed_data)  # b&#39;\\x01\\x00\\x00\\x00\\x02\\x00\\x00\\x00&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5904\u7406\u548c\u83b7\u53d6\u4e00\u4e2a\u5b57\u8282\u7684\u6570\u636e\u6709\u591a\u79cd\u65b9\u6cd5\uff0c<strong>\u5305\u62ec\u4f7f\u7528\u5b57\u8282\u5e8f\u5217\uff08bytes\uff09\u548c\u5b57\u8282\u6570\u7ec4\uff08bytearray\uff09\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u3001\u901a\u8fc7\u7f51\u7edc\u6570\u636e\u5305\u83b7\u53d6\u5b57\u8282\u3001\u4ee5\u53ca\u4f7f\u7528\u7ed3\u6784\u5316\u6570\u636e\u5904\u7406\u5e93<\/strong>\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u548c\u64cd\u4f5c\u5b57\u8282\u6570\u636e\u3002\u901a\u8fc7\u5b9e\u9645\u64cd\u4f5c\u548c\u7ecf\u9a8c\u7684\u79ef\u7d2f\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u5b57\u8282\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>bytes<\/code>\u7c7b\u578b\u6765\u5904\u7406\u5b57\u8282\u6570\u636e\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff08\u4f8b\u5982<code>&#39;rb&#39;<\/code>\uff09\u6765\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u5b57\u8282\u6570\u636e\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;file.bin&#39;, &#39;rb&#39;) as file:\n    byte_data = file.read(1)  # \u8bfb\u53d6\u4e00\u4e2a\u5b57\u8282\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u60a8\u83b7\u53d6\u5230\u7684\u662f\u539f\u59cb\u5b57\u8282\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5c06\u5b57\u8282\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5df2\u7ecf\u6709\u4e86\u5b57\u8282\u6570\u636e\u5e76\u5e0c\u671b\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u82e5\u60a8\u6709\u4e00\u4e2a\u5b57\u8282\u5bf9\u8c61<code>b&#39;hello&#39;<\/code>\uff0c\u53ef\u4ee5\u8fd9\u6837\u8f6c\u6362\uff1a<\/p>\n<pre><code class=\"language-python\">byte_data = b&#39;hello&#39;\nstring_data = byte_data.decode(&#39;utf-8&#39;)  # \u5c06\u5b57\u8282\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\n<\/code><\/pre>\n<p>\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u7b26\u7f16\u7801\u6765\u907f\u514d\u6f5c\u5728\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u5b57\u8282\u6570\u636e\u7684\u5207\u7247\uff1f<\/strong><br \/>\u5904\u7406\u5b57\u8282\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u7279\u5b9a\u90e8\u5206\u7684\u5b57\u8282\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u4ece\u5b57\u8282\u5bf9\u8c61\u4e2d\u83b7\u53d6\u524d\u4e24\u4e2a\u5b57\u8282\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-python\">byte_data = b&#39;hello&#39;\nsliced_data = byte_data[:2]  # \u83b7\u53d6\u524d\u4e24\u4e2a\u5b57\u8282\n<\/code><\/pre>\n<p>\u5b57\u8282\u5207\u7247\u4e0e\u5b57\u7b26\u4e32\u5207\u7247\u76f8\u4f3c\uff0c\u652f\u6301\u5e38\u89c4\u7684\u5207\u7247\u8bed\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53d6\u4e00\u4e2a\u5b57\u8282\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u8282\u5e8f\u5217\uff08bytes\uff09\u3001\u5b57\u8282\u6570\u7ec4\uff08bytearray\uff09\u6216\u901a\u8fc7\u6587\u4ef6 [&hellip;]","protected":false},"author":3,"featured_media":1144006,"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\/1143994"}],"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=1143994"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143994\/revisions"}],"predecessor-version":[{"id":1144007,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143994\/revisions\/1144007"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1144006"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1143994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1143994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1143994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}