{"id":1076198,"date":"2025-01-08T11:51:17","date_gmt":"2025-01-08T03:51:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076198.html"},"modified":"2025-01-08T11:51:21","modified_gmt":"2025-01-08T03:51:21","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%9d%e5%a7%8b%e5%8c%96byte%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1076198.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u521d\u59cb\u5316byte\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180902\/6b342b86-bc2c-4536-9aa6-a32334f25574.webp\" alt=\"python\u4e2d\u5982\u4f55\u521d\u59cb\u5316byte\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u521d\u59cb\u5316\u4e00\u4e2abyte\u6570\u7ec4\u3001\u4f7f\u7528bytes()\u51fd\u6570\u3001\u4f7f\u7528bytearray()\u51fd\u6570\u3001\u901a\u8fc7\u5217\u8868\u751f\u6210\u5668\u6765\u521b\u5efa\u3001\u4ece\u5b57\u7b26\u4e32\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/strong> \u5176\u4e2d\uff0c<strong>bytearray()\u51fd\u6570<\/strong>\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u521d\u59cb\u5316\u4e00\u4e2a\u53ef\u53d8\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u5e76\u4e14\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><p>\u8981\u8be6\u7ec6\u63cf\u8ff0\u4e00\u4e0b\uff0c<strong>bytearray()\u51fd\u6570<\/strong>\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u521d\u59cb\u5316\uff0c\u6bd4\u5982\u901a\u8fc7\u6574\u6570\u3001\u5217\u8868\u3001\u5b57\u7b26\u4e32\u7b49\u3002\u901a\u8fc7\u6574\u6570\u521d\u59cb\u5316\u65f6\uff0cbytearray()\u4f1a\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u957f\u5ea6\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u5e76\u5c06\u6240\u6709\u5b57\u8282\u503c\u521d\u59cb\u5316\u4e3a0\u3002\u901a\u8fc7\u5217\u8868\u521d\u59cb\u5316\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u6bcf\u4e2a\u5b57\u8282\u7684\u503c\u3002\u901a\u8fc7\u5b57\u7b26\u4e32\u521d\u59cb\u5316\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u7f16\u7801\u683c\u5f0f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u89e3\u91ca\u6bcf\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528bytes()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>bytes()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u4e0d\u53ef\u53d8\u7684\u5b57\u8282\u6570\u7ec4\u3002\u5b83\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u6574\u6570\uff0c\u6765\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u957f\u5ea6\u4e14\u6240\u6709\u5b57\u8282\u503c\u4e3a0\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u6216\u8005\u901a\u8fc7\u4f20\u5165\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6765\u521b\u5efa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a5\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u6240\u6709\u5b57\u8282\u503c\u90fd\u4e3a0<\/p>\n<p>byte_array = bytes(5)<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1ab&#39;\\x00\\x00\\x00\\x00\\x00&#39;<\/p>\n<h2><strong>\u901a\u8fc7\u53ef\u8fed\u4ee3\u5bf9\u8c61\u521d\u59cb\u5316\u5b57\u8282\u6570\u7ec4<\/strong><\/h2>\n<p>byte_array = bytes([1, 2, 3, 4, 5])<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1ab&#39;\\x01\\x02\\x03\\x04\\x05&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528bytearray()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>bytearray()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u53ef\u53d8\u7684\u5b57\u8282\u6570\u7ec4\u3002\u5b83\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u6574\u6570\uff0c\u6765\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u957f\u5ea6\u4e14\u6240\u6709\u5b57\u8282\u503c\u4e3a0\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u6216\u8005\u901a\u8fc7\u4f20\u5165\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6765\u521b\u5efa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a5\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u6240\u6709\u5b57\u8282\u503c\u90fd\u4e3a0<\/p>\n<p>byte_array = bytearray(5)<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;\\x00\\x00\\x00\\x00\\x00&#39;)<\/p>\n<h2><strong>\u901a\u8fc7\u53ef\u8fed\u4ee3\u5bf9\u8c61\u521d\u59cb\u5316\u5b57\u8282\u6570\u7ec4<\/strong><\/h2>\n<p>byte_array = bytearray([1, 2, 3, 4, 5])<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;\\x01\\x02\\x03\\x04\\x05&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u5217\u8868\u751f\u6210\u5668\u6765\u521b\u5efa<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u751f\u6210\u5668\u6765\u52a8\u6001\u751f\u6210\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\u7684\u521d\u59cb\u503c\uff0c\u518d\u4f7f\u7528<code>bytearray()<\/code>\u51fd\u6570\u6216<code>bytes()<\/code>\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5b57\u8282\u6570\u7ec4<\/p>\n<p>byte_array = bytearray([x for x in range(10)])<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4ece\u5b57\u7b26\u4e32\u8fdb\u884c\u521d\u59cb\u5316<\/h3>\n<\/p>\n<p><p>\u5f53\u4ece\u5b57\u7b26\u4e32\u521d\u59cb\u5316\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4e00\u822c\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528UTF-8\u7f16\u7801\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4<\/p>\n<p>byte_array = bytearray(&quot;Hello, World!&quot;, &quot;utf-8&quot;)<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u901a\u8fc7\u7ed3\u6784\u5316\u6570\u636e\u521d\u59cb\u5316<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u4ece\u7ed3\u6784\u5316\u6570\u636e\uff08\u5982\u6587\u4ef6\u6570\u636e\uff09\u4e2d\u521d\u59cb\u5316\u5b57\u8282\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>struct<\/code>\u6a21\u5757\u6765\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u7ec4\u7ed3\u6784\u5316\u6570\u636e\uff0c\u4f8b\u5982\u4e24\u4e2a\u6574\u6570\u548c\u4e00\u4e2a\u77ed\u6574\u6570<\/strong><\/h2>\n<p>data = struct.pack(&#39;iih&#39;, 1, 2, 3)<\/p>\n<p>byte_array = bytearray(data)<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;\\x01\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x03\\x00&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u901a\u8fc7\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u521d\u59cb\u5316<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u5b57\u8282\u6570\u7ec4\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6709\u4e00\u4e2a\u6587\u4ef6&#39;test.bin&#39;\u5305\u542b\u4e00\u4e9b\u5b57\u8282\u6570\u636e<\/p>\n<p>with open(&#39;test.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    byte_array = bytearray(file.read())<\/p>\n<p>print(byte_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u901a\u8fc7\u5faa\u73af\u52a8\u6001\u521d\u59cb\u5316<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u57fa\u4e8e\u67d0\u4e9b\u6761\u4ef6\u52a8\u6001\u751f\u6210\u5b57\u8282\u6570\u7ec4\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u6765\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a\u5305\u542b\u5076\u6570\u7684\u5b57\u8282\u6570\u7ec4<\/p>\n<p>byte_array = bytearray()<\/p>\n<p>for i in range(10):<\/p>\n<p>    if i % 2 == 0:<\/p>\n<p>        byte_array.append(i)<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;\\x00\\x02\\x04\\x06\\x08&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u901a\u8fc7\u5185\u5b58\u89c6\u56fe\u521d\u59cb\u5316<\/h3>\n<\/p>\n<p><p>\u5185\u5b58\u89c6\u56fe\uff08memoryview\uff09\u63d0\u4f9b\u4e86\u5bf9\u5b57\u8282\u6570\u7ec4\u7684\u89c6\u56fe\uff0c\u53ef\u4ee5\u4ece\u4e00\u4e2a\u73b0\u6709\u7684\u5b57\u8282\u6570\u7ec4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u539f\u59cb\u5b57\u8282\u6570\u7ec4<\/p>\n<p>original_array = bytearray([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5185\u5b58\u89c6\u56fe<\/strong><\/h2>\n<p>mem_view = memoryview(original_array)<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u7684\u5b57\u8282\u6570\u7ec4<\/strong><\/h2>\n<p>new_byte_array = bytearray(mem_view)<\/p>\n<p>print(new_byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;\\x01\\x02\\x03\\x04\\x05&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u901a\u8fc7\u6570\u7ec4\u6a21\u5757\u521d\u59cb\u5316<\/h3>\n<\/p>\n<p><p>Python\u7684<code>array<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u8be5\u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a<code>bytearray<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import array<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>arr = array.array(&#39;B&#39;, [1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4<\/strong><\/h2>\n<p>byte_array = bytearray(arr)<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;\\x01\\x02\\x03\\x04\\x05&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u901a\u8fc7Numpy\u5e93\u521d\u59cb\u5316<\/h3>\n<\/p>\n<p><p>Numpy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528Numpy\u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a<code>bytearray<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aNumpy\u6570\u7ec4<\/strong><\/h2>\n<p>np_array = np.array([1, 2, 3, 4, 5], dtype=np.uint8)<\/p>\n<h2><strong>\u5c06Numpy\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4<\/strong><\/h2>\n<p>byte_array = bytearray(np_array)<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;\\x01\\x02\\x03\\x04\\x05&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u901a\u8fc7Pandas\u5e93\u521d\u59cb\u5316<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a<code>bytearray<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aPandas\u7cfb\u5217<\/strong><\/h2>\n<p>pd_series = pd.Series([1, 2, 3, 4, 5], dtype=&#39;uint8&#39;)<\/p>\n<h2><strong>\u5c06Pandas\u7cfb\u5217\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4<\/strong><\/h2>\n<p>byte_array = bytearray(pd_series)<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;\\x01\\x02\\x03\\x04\\x05&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u901a\u8fc7\u4f7f\u7528Ctypes\u5e93\u521d\u59cb\u5316<\/h3>\n<\/p>\n<p><p>Ctypes\u5e93\u63d0\u4f9b\u4e86\u4e0eC\u517c\u5bb9\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528Ctypes\u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a<code>bytearray<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aCtypes\u6570\u7ec4<\/strong><\/h2>\n<p>c_array = (ctypes.c_ubyte * 5)(1, 2, 3, 4, 5)<\/p>\n<h2><strong>\u5c06Ctypes\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4<\/strong><\/h2>\n<p>byte_array = bytearray(c_array)<\/p>\n<p>print(byte_array)  # \u8f93\u51fa\uff1abytearray(b&#39;\\x01\\x02\\x03\\x04\\x05&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u548c\u6570\u636e\u6765\u6e90\u521d\u59cb\u5316\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u6700\u5408\u9002\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\u4ee3\u7801\uff0c\u80fd\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u4e2d\u7684\u5b57\u8282\u6570\u7ec4\u521d\u59cb\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684byte\u6570\u7ec4\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>bytearray()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684byte\u6570\u7ec4\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">byte_array = bytearray()\n<\/code><\/pre>\n<p>\u8fd9\u6837\u4f1a\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a0\u7684byte\u6570\u7ec4\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5411\u5176\u4e2d\u6dfb\u52a0\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7279\u5b9a\u957f\u5ea6\u7684byte\u6570\u7ec4\uff1f<\/strong><br \/>\u8981\u5b9a\u4e49\u4e00\u4e2a\u7279\u5b9a\u957f\u5ea6\u7684byte\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>bytearray<\/code>\u5e76\u6307\u5b9a\u957f\u5ea6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">length = 10\nbyte_array = bytearray(length)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a10\u7684byte\u6570\u7ec4\uff0c\u6240\u6709\u5143\u7d20\u521d\u59cb\u5316\u4e3a0\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3abyte\u6570\u7ec4\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3abyte\u6570\u7ec4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">string = &quot;Hello&quot;\nbyte_array = bytearray(string, &#39;utf-8&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5b57\u7b26\u4e32\u201cHello\u201d\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801\u7684byte\u6570\u7ec4\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5176\u4ed6\u7f16\u7801\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u521d\u59cb\u5316\u4e00\u4e2abyte\u6570\u7ec4\u3001\u4f7f\u7528bytes()\u51fd\u6570\u3001\u4f7f\u7528bytearray()\u51fd [&hellip;]","protected":false},"author":3,"featured_media":1076210,"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\/1076198"}],"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=1076198"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076198\/revisions"}],"predecessor-version":[{"id":1076212,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076198\/revisions\/1076212"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1076210"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1076198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1076198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1076198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}