{"id":1134625,"date":"2025-01-08T21:17:21","date_gmt":"2025-01-08T13:17:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1134625.html"},"modified":"2025-01-08T21:17:25","modified_gmt":"2025-01-08T13:17:25","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2%e4%b8%babytes-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1134625.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\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\/25103514\/28f00cfb-f9e4-4b36-a83c-88c1da3097f6.webp\" alt=\"python\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3abytes\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3abytes\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528encode()\u65b9\u6cd5\u3001\u4f7f\u7528bytes()\u51fd\u6570\u4ee5\u53ca\u4f7f\u7528bytearray()\u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u7b26\u4e32\u7684encode()\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u7684bytes\u5bf9\u8c61\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\u53ca\u5176\u5177\u4f53\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528encode()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5b57\u7b26\u4e32\u662fUnicode\u5b57\u7b26\u4e32\uff0c\u800c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8fd9\u4e9b\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u7b26\u4e32\u7684encode()\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u5141\u8bb8\u4f60\u6307\u5b9a\u7f16\u7801\u65b9\u5f0f\uff0c\u5982&#39;utf-8&#39;\u3001&#39;ascii&#39;\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>encode()\u65b9\u6cd5\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>bytes_obj = string.encode(&#39;utf-8&#39;)<\/p>\n<p>print(bytes_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26\u4e32&quot;Hello, World!&quot;\u88ab\u7f16\u7801\u4e3aUTF-8\u683c\u5f0f\u7684\u5b57\u8282\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6307\u5b9a\u4e0d\u540c\u7f16\u7801\u65b9\u5f0f<\/h4>\n<\/p>\n<p><p>\u867d\u7136UTF-8\u662f\u6700\u5e38\u7528\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u4f46\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u7f16\u7801\u65b9\u5f0f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528ASCII\u7f16\u7801\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>bytes_obj = string.encode(&#39;ascii&#39;)<\/p>\n<p>print(bytes_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u7f16\u7801\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u5f53\u6307\u5b9a\u7684\u7f16\u7801\u65b9\u5f0f\u4e0d\u80fd\u7f16\u7801\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e9b\u5b57\u7b26\u65f6\uff0c\u4f1a\u629b\u51fa\u4e00\u4e2aUnicodeEncodeError\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u6765\u907f\u514d\u8fd9\u4e2a\u9519\u8bef\u3002\u5e38\u89c1\u7684\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u6709&#39;ignore&#39;\u3001&#39;replace&#39;\u3001&#39;xmlcharrefreplace&#39;\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, \u4e16\u754c!&quot;<\/p>\n<p>bytes_obj = string.encode(&#39;ascii&#39;, errors=&#39;ignore&#39;)<\/p>\n<p>print(bytes_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u975eASCII\u5b57\u7b26\u201c\u4e16\u754c\u201d\u5c06\u88ab\u5ffd\u7565\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528bytes()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86encode()\u65b9\u6cd5\uff0cPython\u8fd8\u63d0\u4f9b\u4e86bytes()\u51fd\u6570\u6765\u8f6c\u6362\u5b57\u7b26\u4e32\u4e3a\u5b57\u8282\u5bf9\u8c61\u3002\u8fd9\u4e2a\u51fd\u6570\u4e5f\u652f\u6301\u6307\u5b9a\u7f16\u7801\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>bytes()\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>bytes_obj = bytes(string, &#39;utf-8&#39;)<\/p>\n<p>print(bytes_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u4e0d\u540c\u7f16\u7801\u65b9\u5f0f<\/h4>\n<\/p>\n<p><p>\u4e0eencode()\u65b9\u6cd5\u7c7b\u4f3c\uff0cbytes()\u51fd\u6570\u4e5f\u5141\u8bb8\u4f60\u6307\u5b9a\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>bytes_obj = bytes(string, &#39;ascii&#39;)<\/p>\n<p>print(bytes_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u9519\u8bef\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5c3d\u7ba1bytes()\u51fd\u6570\u63d0\u4f9b\u4e86\u4e0eencode()\u65b9\u6cd5\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u4f46\u5b83\u4e0d\u76f4\u63a5\u652f\u6301\u9519\u8bef\u5904\u7406\u9009\u9879\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u7f16\u7801\u9519\u8bef\uff0c\u5efa\u8bae\u4f7f\u7528encode()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528bytearray()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4e0ebytes()\u51fd\u6570\u7c7b\u4f3c\uff0cbytearray()\u51fd\u6570\u4e5f\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5bf9\u8c61\u3002\u4e0d\u540c\u7684\u662f\uff0cbytearray()\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u53ef\u53d8\u7684\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>bytearray()\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>bytearray_obj = bytearray(string, &#39;utf-8&#39;)<\/p>\n<p>print(bytearray_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4fee\u6539\u5b57\u8282\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u7531\u4e8ebytearray\u662f\u53ef\u53d8\u7684\uff0c\u4f60\u53ef\u4ee5\u5728\u751f\u6210\u5b57\u8282\u6570\u7ec4\u540e\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">bytearray_obj[0] = 72<\/p>\n<p>print(bytearray_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4fee\u6539\u4e86\u5b57\u8282\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u7075\u6d3b\u7684\u9519\u8bef\u5904\u7406\u548c\u66f4\u76f4\u89c2\u7684\u7f16\u7801\u65b9\u5f0f\uff0cencode()\u65b9\u6cd5\u662f\u9996\u9009\u3002\u5982\u679c\u4f60\u9700\u8981\u751f\u6210\u4e00\u4e2a\u53ef\u53d8\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528bytearray()\u51fd\u6570\u3002\u800cbytes()\u51fd\u6570\u5219\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u7f16\u7801\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4e09\u79cd\u65b9\u6cd5\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u5bf9\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3abytes\u6709\u4e86\u6df1\u5165\u7684\u4e86\u89e3\u3002\u65e0\u8bba\u662f\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u3001\u7f51\u7edc\u901a\u4fe1\u8fd8\u662f\u6587\u4ef6\u64cd\u4f5c\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u4e3a\u4f60\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u7684\u6700\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u5982UTF-8\u6216ASCII\u3002\u4f8b\u5982\uff0c<code>my_string.encode(&#39;utf-8&#39;)<\/code>\u5c06\u8fd4\u56de\u4e00\u4e2a\u5b57\u8282\u5bf9\u8c61\u3002\u786e\u4fdd\u9009\u62e9\u9002\u5f53\u7684\u7f16\u7801\uff0c\u4ee5\u4fbf\u6b63\u786e\u8868\u793a\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u6709\u54ea\u4e9b\u7f16\u7801\u683c\u5f0f\u53ef\u4f9b\u9009\u62e9\uff1f<\/strong><br \/>\u5e38\u89c1\u7684\u7f16\u7801\u683c\u5f0f\u5305\u62ecUTF-8\u3001ASCII\u3001UTF-16\u548cISO-8859-1\u7b49\u3002UTF-8\u652f\u6301\u6240\u6709Unicode\u5b57\u7b26\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\uff1bASCII\u4ec5\u652f\u6301\u57fa\u672c\u62c9\u4e01\u5b57\u7b26\uff0c\u9002\u5408\u5904\u7406\u82f1\u6587\u6587\u672c\uff1b\u800cUTF-16\u9002\u5408\u9700\u8981\u66f4\u591aUnicode\u5b57\u7b26\u7684\u5e94\u7528\u573a\u666f\u3002\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u683c\u5f0f\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u6b63\u786e\u6027\u548c\u517c\u5bb9\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b57\u8282\u6d41\u518d\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u8282\u6d41\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\u3002\u53ea\u9700\u8c03\u7528\u5b57\u8282\u5bf9\u8c61\u7684<code>decode()<\/code>\u65b9\u6cd5\uff0c\u5e76\u6307\u5b9a\u76f8\u540c\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4e4b\u524d\u4f7f\u7528\u4e86UTF-8\u7f16\u7801\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_bytes.decode(&#39;utf-8&#39;)<\/code>\u5c06\u5b57\u8282\u6d41\u6062\u590d\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u5728\u5904\u7406\u7f51\u7edc\u6570\u636e\u6216\u6587\u4ef6I\/O\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3abytes\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528encode()\u65b9\u6cd5\u3001\u4f7f\u7528bytes()\u51fd\u6570\u4ee5\u53ca\u4f7f\u7528b [&hellip;]","protected":false},"author":3,"featured_media":1134639,"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\/1134625"}],"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=1134625"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134625\/revisions"}],"predecessor-version":[{"id":1134641,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134625\/revisions\/1134641"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1134639"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1134625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1134625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1134625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}