{"id":968768,"date":"2024-12-27T05:10:03","date_gmt":"2024-12-26T21:10:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/968768.html"},"modified":"2024-12-27T05:10:04","modified_gmt":"2024-12-26T21:10:04","slug":"python%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2%e4%b8%babytes","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/968768.html","title":{"rendered":"python\u5982\u4f55\u8f6c\u6362\u4e3abytes"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183348\/630ab49e-1ccc-43bf-af48-14a162e408a2.webp\" alt=\"python\u5982\u4f55\u8f6c\u6362\u4e3abytes\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u3001<code>bytes()<\/code>\u51fd\u6570\u3001<code>struct<\/code>\u6a21\u5757\u3001<code>pickle<\/code>\u6a21\u5757\u548c<code>bytearray()<\/code>\u51fd\u6570\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\uff08bytes\uff09\u3002\u8fd9\u4e9b\u65b9\u6cd5\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u5e8f\u5217\u5316\u5bf9\u8c61\u7b49\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002<\/strong>\u5176\u4e2d\uff0c<code>encode()<\/code>\u65b9\u6cd5\u5e38\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c<code>bytes()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u6574\u6570\u548c\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c<code>encode()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><code>encode()<\/code>\u65b9\u6cd5\u662f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002Python\u4e2d\u7684\u5b57\u7b26\u4e32\u662fUnicode\u5b57\u7b26\u4e32\uff0c\u4e3a\u4e86\u8fdb\u884c\u7f51\u7edc\u4f20\u8f93\u6216\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u3002\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u5982UTF-8\u3001ASCII\u7b49\u3002UTF-8\u662f\u4e00\u79cd\u5e38\u7528\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u517c\u5bb9\u6027\u597d\u4e14\u80fd\u8868\u793a\u51e0\u4e4e\u6240\u6709\u7684\u5b57\u7b26\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801\uff1a\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61<\/p>\n<p>string_data = &quot;Hello, World!&quot;<\/p>\n<p>bytes_data = string_data.encode(&#39;utf-8&#39;)<\/p>\n<p>print(bytes_data)  # \u8f93\u51fa\uff1ab&#39;Hello, World!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>encode(&#39;utf-8&#39;)<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32<code>&quot;Hello, World!&quot;<\/code>\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61<code>b&#39;Hello, World!&#39;<\/code>\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528 <code>encode()<\/code> \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><code>encode()<\/code>\u65b9\u6cd5\u662fPython\u4e2d\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u7684\u5e38\u7528\u65b9\u6cd5\u3002\u6bcf\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61\u90fd\u5305\u542b\u6b64\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u65b9\u5f0f\u3002\u5e38\u7528\u7684\u7f16\u7801\u65b9\u5f0f\u5305\u62ecUTF-8\u3001ASCII\u3001Latin-1\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u662f\u4ee5Unicode\u8868\u793a\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u800c\u5b57\u8282\u5bf9\u8c61\u662f\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u7684\u5e8f\u5217\u3002\u56e0\u6b64\uff0c\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u65f6\uff0c\u901a\u5e38\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>bytes_obj = string.encode(&#39;utf-8&#39;)<\/p>\n<p>print(bytes_obj)  # \u8f93\u51fa\uff1ab&#39;Hello, World!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>encode(&#39;utf-8&#39;)<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32<code>string<\/code>\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801\u7684\u5b57\u8282\u5bf9\u8c61<code>bytes_obj<\/code>\u3002UTF-8\u662f\u6700\u5e38\u7528\u7684\u7f16\u7801\u65b9\u5f0f\u4e4b\u4e00\uff0c\u80fd\u591f\u8868\u793a\u51e0\u4e4e\u6240\u6709\u7684\u5b57\u7b26\u96c6\u3002<\/p>\n<\/p>\n<p><h3>\u4e0d\u540c\u7f16\u7801\u65b9\u5f0f\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301\u591a\u79cd\u7f16\u7801\u65b9\u5f0f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u65b9\u5f0f\u3002\u4e0d\u540c\u7f16\u7801\u65b9\u5f0f\u4e4b\u95f4\u7684\u5dee\u5f02\u5728\u4e8e\u5b83\u4eec\u8868\u793a\u5b57\u7b26\u7684\u5b57\u8282\u6570\u548c\u5b57\u7b26\u96c6\u7684\u8986\u76d6\u8303\u56f4\u3002\u4f8b\u5982\uff0cASCII\u7f16\u7801\u53ea\u80fd\u8868\u793a128\u4e2a\u5b57\u7b26\uff0c\u800cUTF-8\u7f16\u7801\u53ef\u4ee5\u8868\u793a\u51e0\u4e4e\u6240\u6709\u7684\u5b57\u7b26\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u5f0f<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>utf8_bytes = string.encode(&#39;utf-8&#39;)<\/p>\n<p>ascii_bytes = string.encode(&#39;ascii&#39;)<\/p>\n<p>latin1_bytes = string.encode(&#39;latin-1&#39;)<\/p>\n<p>print(utf8_bytes)  # \u8f93\u51fa\uff1ab&#39;Hello, World!&#39;<\/p>\n<p>print(ascii_bytes)  # \u8f93\u51fa\uff1ab&#39;Hello, World!&#39;<\/p>\n<p>print(latin1_bytes)  # \u8f93\u51fa\uff1ab&#39;Hello, World!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32<code>string<\/code>\u88ab\u5206\u522b\u8f6c\u6362\u4e3aUTF-8\u3001ASCII\u548cLatin-1\u7f16\u7801\u7684\u5b57\u8282\u5bf9\u8c61\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u975eASCII\u5b57\u7b26\uff0c\u800c\u9009\u62e9\u7684\u7f16\u7801\u65b9\u5f0f\u4e0d\u652f\u6301\u8fd9\u4e9b\u5b57\u7b26\uff0c\u5219\u4f1a\u5f15\u53d1<code>UnicodeEncodeError<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528 <code>bytes()<\/code> \u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>bytes()<\/code>\u51fd\u6570\u662fPython\u4e2d\u7528\u4e8e\u521b\u5efa\u5b57\u8282\u5bf9\u8c61\u7684\u5185\u7f6e\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u4ece\u6574\u6570\u3001\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\uff09\u6216\u5b57\u7b26\u4e32\uff08\u9700\u8981\u6307\u5b9a\u7f16\u7801\uff09\u521b\u5efa\u5b57\u8282\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u4ece\u6574\u6570\u521b\u5efa\u5b57\u8282\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u5f53\u4f7f\u7528<code>bytes()<\/code>\u51fd\u6570\u4ece\u6574\u6570\u521b\u5efa\u5b57\u8282\u5bf9\u8c61\u65f6\uff0c\u6574\u6570\u8868\u793a\u5b57\u8282\u5bf9\u8c61\u7684\u957f\u5ea6\uff0c\u5b57\u8282\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5b57\u8282\u90fd\u88ab\u521d\u59cb\u5316\u4e3a\u96f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u6574\u6570\u521b\u5efa\u5b57\u8282\u5bf9\u8c61<\/p>\n<p>length = 5<\/p>\n<p>bytes_obj = bytes(length)<\/p>\n<p>print(bytes_obj)  # \u8f93\u51fa\uff1ab&#39;\\x00\\x00\\x00\\x00\\x00&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>bytes(length)<\/code>\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a5\u7684\u5b57\u8282\u5bf9\u8c61<code>bytes_obj<\/code>\uff0c\u6bcf\u4e2a\u5b57\u8282\u90fd\u88ab\u521d\u59cb\u5316\u4e3a\u96f6\u3002<\/p>\n<\/p>\n<p><h3>\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u521b\u5efa\u5b57\u8282\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p><code>bytes()<\/code>\u51fd\u6570\u8fd8\u53ef\u4ee5\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\uff09\u521b\u5efa\u5b57\u8282\u5bf9\u8c61\u3002\u6b64\u65f6\uff0c\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5fc5\u987b\u662f\u6574\u6570\uff0c\u4e14\u8303\u56f4\u57280\u5230255\u4e4b\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u521b\u5efa\u5b57\u8282\u5bf9\u8c61<\/p>\n<p>iterable = [72, 101, 108, 108, 111]<\/p>\n<p>bytes_obj = bytes(iterable)<\/p>\n<p>print(bytes_obj)  # \u8f93\u51fa\uff1ab&#39;Hello&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>bytes(iterable)<\/code>\u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u5bf9\u8c61<code>bytes_obj<\/code>\uff0c\u5b57\u8282\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5b57\u8282\u5bf9\u5e94\u4e8e\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528 <code>struct<\/code> \u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>struct<\/code>\u6a21\u5757\u7528\u4e8e\u5728Python\u4e2d\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5e38\u7528\u4e8e\u5c06\u6570\u636e\u6253\u5305\u6210\u5b57\u8282\u5bf9\u8c61\u6216\u4ece\u5b57\u8282\u5bf9\u8c61\u89e3\u5305\u6570\u636e\u3002\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\u548c\u683c\u5f0f\u5b57\u7b26\uff0c\u4ee5\u4fbf\u4e8e\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u6570\u636e\u6253\u5305\u6210\u5b57\u8282\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p><code>struct.pack()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u6570\u636e\u6253\u5305\u6210\u5b57\u8282\u5bf9\u8c61\u3002\u5b83\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u683c\u5f0f\u5b57\u7b26\u4e32\u548c\u8981\u6253\u5305\u7684\u6570\u636e\u3002\u683c\u5f0f\u5b57\u7b26\u4e32\u6307\u5b9a\u4e86\u6570\u636e\u7684\u7c7b\u578b\u548c\u5b57\u8282\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<h2><strong>\u5c06\u6570\u636e\u6253\u5305\u6210\u5b57\u8282\u5bf9\u8c61<\/strong><\/h2>\n<p>data = (1, b&#39;A&#39;, 2.5)<\/p>\n<p>bytes_obj = struct.pack(&#39;i c f&#39;, *data)<\/p>\n<p>print(bytes_obj)  # \u8f93\u51fa\uff1ab&#39;\\x01\\x00\\x00\\x00A\\x00\\x00\\x00\\x00\\x00 @&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>struct.pack(&#39;i c f&#39;, *data)<\/code>\u5c06\u6574\u6570\u3001\u5b57\u7b26\u548c\u6d6e\u70b9\u6570\u6253\u5305\u6210\u5b57\u8282\u5bf9\u8c61<code>bytes_obj<\/code>\u3002\u683c\u5f0f\u5b57\u7b26\u4e32<code>&#39;i c f&#39;<\/code>\u6307\u5b9a\u4e86\u6570\u636e\u7684\u7c7b\u578b\u548c\u987a\u5e8f\uff0c\u5176\u4e2d<code>&#39;i&#39;<\/code>\u8868\u793a\u6574\u6570\uff0c<code>&#39;c&#39;<\/code>\u8868\u793a\u5b57\u7b26\uff0c<code>&#39;f&#39;<\/code>\u8868\u793a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4ece\u5b57\u8282\u5bf9\u8c61\u89e3\u5305\u6570\u636e<\/h3>\n<\/p>\n<p><p><code>struct.unpack()<\/code>\u51fd\u6570\u7528\u4e8e\u4ece\u5b57\u8282\u5bf9\u8c61\u89e3\u5305\u6570\u636e\u3002\u5b83\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u683c\u5f0f\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u5bf9\u8c61\u3002\u683c\u5f0f\u5b57\u7b26\u4e32\u6307\u5b9a\u4e86\u5b57\u8282\u5bf9\u8c61\u4e2d\u6570\u636e\u7684\u7c7b\u578b\u548c\u5b57\u8282\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<h2><strong>\u4ece\u5b57\u8282\u5bf9\u8c61\u89e3\u5305\u6570\u636e<\/strong><\/h2>\n<p>bytes_obj = b&#39;\\x01\\x00\\x00\\x00A\\x00\\x00\\x00\\x00\\x00 @&#39;<\/p>\n<p>data = struct.unpack(&#39;i c f&#39;, bytes_obj)<\/p>\n<p>print(data)  # \u8f93\u51fa\uff1a(1, b&#39;A&#39;, 2.5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>struct.unpack(&#39;i c f&#39;, bytes_obj)<\/code>\u4ece\u5b57\u8282\u5bf9\u8c61<code>bytes_obj<\/code>\u89e3\u5305\u51fa\u6574\u6570\u3001\u5b57\u7b26\u548c\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528 <code>pickle<\/code> \u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>pickle<\/code>\u6a21\u5757\u7528\u4e8e\u5728Python\u4e2d\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u5bf9\u8c61\u3002\u5e8f\u5217\u5316\u662f\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u7684\u8fc7\u7a0b\uff0c\u800c\u53cd\u5e8f\u5217\u5316\u662f\u4ece\u5b57\u8282\u5bf9\u8c61\u6062\u590d\u5bf9\u8c61\u7684\u8fc7\u7a0b\u3002<code>pickle<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\uff0c\u9002\u5408\u4e8e\u5c06Python\u5bf9\u8c61\u6301\u4e45\u5316\u5b58\u50a8\u3002<\/p>\n<\/p>\n<p><h3>\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a\u5b57\u8282\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p><code>pickle.dumps()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06Python\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a\u5b57\u8282\u5bf9\u8c61\u3002\u5b83\u9700\u8981\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u8981\u5e8f\u5217\u5316\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<h2><strong>\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a\u5b57\u8282\u5bf9\u8c61<\/strong><\/h2>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>bytes_obj = pickle.dumps(data)<\/p>\n<p>print(bytes_obj)  # \u8f93\u51fa\uff1a\u5b57\u8282\u5bf9\u8c61\u8868\u793a\u7684\u5e8f\u5217\u5316\u6570\u636e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>pickle.dumps(data)<\/code>\u5c06\u5b57\u5178\u5bf9\u8c61<code>data<\/code>\u5e8f\u5217\u5316\u4e3a\u5b57\u8282\u5bf9\u8c61<code>bytes_obj<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4ece\u5b57\u8282\u5bf9\u8c61\u53cd\u5e8f\u5217\u5316\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p><code>pickle.loads()<\/code>\u51fd\u6570\u7528\u4e8e\u4ece\u5b57\u8282\u5bf9\u8c61\u53cd\u5e8f\u5217\u5316\u5bf9\u8c61\u3002\u5b83\u9700\u8981\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u5b57\u8282\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<h2><strong>\u4ece\u5b57\u8282\u5bf9\u8c61\u53cd\u5e8f\u5217\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>bytes_obj = b&#39;\\x80\\x03}q\\x00(X\\x04\\x00\\x00\\x00nameq\\x01X\\x05\\x00\\x00\\x00Aliceq\\x02X\\x03\\x00\\x00\\x00ageq\\x03K\\x19X\\x04\\x00\\x00\\x00cityq\\x04X\\x08\\x00\\x00\\x00New Yorkq\\x05u.&#39;<\/p>\n<p>data = pickle.loads(bytes_obj)<\/p>\n<p>print(data)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>pickle.loads(bytes_obj)<\/code>\u4ece\u5b57\u8282\u5bf9\u8c61<code>bytes_obj<\/code>\u53cd\u5e8f\u5217\u5316\u51fa\u5b57\u5178\u5bf9\u8c61<code>data<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528 <code>bytearray()<\/code> \u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>bytearray()<\/code>\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u53ef\u53d8\u7684\u5b57\u8282\u6570\u7ec4\u3002\u4e0e<code>bytes()<\/code>\u51fd\u6570\u4e0d\u540c\uff0c<code>bytearray()<\/code>\u521b\u5efa\u7684\u5bf9\u8c61\u662f\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u8fdb\u884c\u4fee\u6539\u3002<code>bytearray()<\/code>\u51fd\u6570\u53ef\u4ee5\u4ece\u6574\u6570\u3001\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6216\u5b57\u7b26\u4e32\uff08\u9700\u8981\u6307\u5b9a\u7f16\u7801\uff09\u521b\u5efa\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4ece\u6574\u6570\u521b\u5efa\u5b57\u8282\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u5f53\u4f7f\u7528<code>bytearray()<\/code>\u51fd\u6570\u4ece\u6574\u6570\u521b\u5efa\u5b57\u8282\u6570\u7ec4\u65f6\uff0c\u6574\u6570\u8868\u793a\u5b57\u8282\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5b57\u8282\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5b57\u8282\u90fd\u88ab\u521d\u59cb\u5316\u4e3a\u96f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u6574\u6570\u521b\u5efa\u5b57\u8282\u6570\u7ec4<\/p>\n<p>length = 5<\/p>\n<p>byte_array = bytearray(length)<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;\\x00\\x00\\x00\\x00\\x00&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>bytearray(length)<\/code>\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a5\u7684\u5b57\u8282\u6570\u7ec4<code>byte_array<\/code>\uff0c\u6bcf\u4e2a\u5b57\u8282\u90fd\u88ab\u521d\u59cb\u5316\u4e3a\u96f6\u3002<\/p>\n<\/p>\n<p><h3>\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u521b\u5efa\u5b57\u8282\u6570\u7ec4<\/h3>\n<\/p>\n<p><p><code>bytearray()<\/code>\u51fd\u6570\u8fd8\u53ef\u4ee5\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\uff09\u521b\u5efa\u5b57\u8282\u6570\u7ec4\u3002\u6b64\u65f6\uff0c\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5fc5\u987b\u662f\u6574\u6570\uff0c\u4e14\u8303\u56f4\u57280\u5230255\u4e4b\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u521b\u5efa\u5b57\u8282\u6570\u7ec4<\/p>\n<p>iterable = [72, 101, 108, 108, 111]<\/p>\n<p>byte_array = bytearray(iterable)<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;Hello&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>bytearray(iterable)<\/code>\u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4<code>byte_array<\/code>\uff0c\u5b57\u8282\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5b57\u8282\u5bf9\u5e94\u4e8e\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4fee\u6539\u5b57\u8282\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u7531\u4e8e<code>bytearray<\/code>\u662f\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u4fee\u6539\u5176\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6216\u5207\u7247\u6765\u4fee\u6539\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fee\u6539\u5b57\u8282\u6570\u7ec4<\/p>\n<p>byte_array = bytearray(b&#39;Hello&#39;)<\/p>\n<p>byte_array[0] = 74  # \u5c06&#39;H&#39;\u4fee\u6539\u4e3a&#39;J&#39;<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;Jello&#39;)<\/p>\n<p>byte_array[1:3] = b&#39;oy&#39;  # \u4fee\u6539\u5207\u7247<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;Joylo&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u7d22\u5f15\u548c\u5207\u7247\u4fee\u6539\u5b57\u8282\u6570\u7ec4<code>byte_array<\/code>\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\uff08bytes\uff09\uff0c\u5305\u62ec\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u3001<code>bytes()<\/code>\u51fd\u6570\u3001<code>struct<\/code>\u6a21\u5757\u3001<code>pickle<\/code>\u6a21\u5757\u548c<code>bytearray()<\/code>\u51fd\u6570\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u7c7b\u578b\u548c\u8f6c\u6362\u7684\u5177\u4f53\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3abytes\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3abytes\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>my_string = &quot;Hello, World!&quot;<\/code>\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>my_bytes = my_string.encode(&#39;utf-8&#39;)<\/code>\u5c06\u5176\u8f6c\u6362\u4e3abytes\u3002\u8fd9\u91cc\u7684<code>&#39;utf-8&#39;<\/code>\u662f\u6307\u5b9a\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\u5982<code>&#39;ascii&#39;<\/code>\u6216<code>&#39;latin-1&#39;<\/code>\u4e5f\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u6574\u6570\u8f6c\u6362\u4e3abytes\uff1f<\/strong><br \/>\u8981\u5c06\u6574\u6570\u8f6c\u6362\u4e3abytes\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int.to_bytes()<\/code>\u65b9\u6cd5\u3002\u8bed\u6cd5\u4e3a<code>int_value.to_bytes(length, byteorder)<\/code>\uff0c\u5176\u4e2d<code>length<\/code>\u662f\u76ee\u6807bytes\u7684\u957f\u5ea6\uff0c<code>byteorder<\/code>\u53ef\u4ee5\u662f<code>&#39;big&#39;<\/code>\u6216<code>&#39;little&#39;<\/code>\uff0c\u8868\u793a\u5b57\u8282\u7684\u987a\u5e8f\u3002\u4f8b\u5982\uff0c<code>my_int = 123<\/code>\u53ef\u4ee5\u901a\u8fc7<code>my_bytes = my_int.to_bytes(2, &#39;big&#39;)<\/code>\u8f6c\u6362\u4e3abytes\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3abytes\u683c\u5f0f\uff1f<\/strong><br \/>\u5c06\u5217\u8868\u8f6c\u6362\u4e3abytes\u53ef\u4ee5\u4f7f\u7528<code>bytearray<\/code>\u6216<code>bytes()<\/code>\u6784\u9020\u51fd\u6570\u3002\u82e5\u5217\u8868\u5305\u542b\u6574\u6570\uff08\u6bcf\u4e2a\u6574\u6570\u5e94\u57280\u5230255\u4e4b\u95f4\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528<code>bytes(my_list)<\/code>\uff0c\u5982<code>my_list = [65, 66, 67]<\/code>\uff0c\u90a3\u4e48<code>my_bytes = bytes(my_list)<\/code>\u4f1a\u5f97\u5230<code>b&#39;ABC&#39;<\/code>\u3002\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\uff0c\u901a\u5e38\u9700\u8981\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u53ef\u8f6c\u5316\u4e3abytes\u7684\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528encode()\u65b9\u6cd5\u3001bytes()\u51fd\u6570\u3001struct\u6a21\u5757\u3001pickle\u6a21\u5757\u548cbyt [&hellip;]","protected":false},"author":3,"featured_media":968772,"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\/968768"}],"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=968768"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968768\/revisions"}],"predecessor-version":[{"id":968773,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968768\/revisions\/968773"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/968772"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=968768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=968768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=968768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}