{"id":410,"date":"2025-08-01T20:04:48","date_gmt":"2025-08-01T12:04:48","guid":{"rendered":"https:\/\/www.rbfwq.com\/blog\/?p=410"},"modified":"2025-08-01T20:04:48","modified_gmt":"2025-08-01T12:04:48","slug":"python-%e5%ad%97%e8%8a%82%e4%b8%b2-b-%e8%bd%ac%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e5%ae%9e%e7%94%a8%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.idc61.com\/blog\/410.html","title":{"rendered":"Python \u5b57\u8282\u4e32 b'' \u8f6c\u4e3a\u5b57\u7b26\u4e32\u7684\u5b9e\u7528\u6307\u5357"},"content":{"rendered":"<p><p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u6709\u65f6\u6211\u4eec\u4f1a\u9047\u5230\u5b57\u8282\u4e32\uff08bytes\uff09\u683c\u5f0f\u7684\u6570\u636e\u3002\u5b57\u8282\u4e32\u901a\u5e38\u4ee5<b>b&#8221;<\/b>\u7684\u5f62\u5f0f\u5b58\u5728\uff0c\u800c\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u8fd9\u4e9b\u5b57\u8282\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff08str\uff09\u7c7b\u578b\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u548c\u5206\u6790\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5c06\u5b57\u8282\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u5e2e\u52a9\u7528\u6237\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.idc61.com\/blog\/wp-content\/uploads\/2025\/06\/35WV8B.jpg\" alt=\"Python \u5b57\u8282\u4e32 b'' \u8f6c\u4e3a\u5b57\u7b26\u4e32\u7684\u5b9e\u7528\u6307\u5357\" title=\"Python \u5b57\u8282\u4e32 b'' \u8f6c\u4e3a\u5b57\u7b26\u4e32\u7684\u5b9e\u7528\u6307\u5357\"><\/p>\n<\/p>\n<p><h2>1. \u64cd\u4f5c\u524d\u7684\u51c6\u5907<\/h2>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u8f6c\u6362\u4e4b\u524d\uff0c\u4e86\u89e3\u5b57\u8282\u4e32\u4e0e\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u533a\u522b\u662f\u5f88\u91cd\u8981\u7684\u3002\u5b57\u8282\u4e32\u662f\u4e00\u7cfb\u5217\u5b57\u8282\uff080-255\uff09\u7ec4\u6210\u7684\u6570\u636e\uff0c\u800c\u5b57\u7b26\u4e32\u5219\u662f\u4ee5\u5b57\u7b26\u7684\u5f62\u5f0f\u8868\u73b0\u51fa\u6765\u3002Python\u4f7f\u7528Unicode\u7f16\u7801\u8868\u793a\u5b57\u7b26\uff0c\u56e0\u6b64\u5b57\u8282\u4e32\u9700\u8981\u901a\u8fc7\u4e00\u5b9a\u7684\u7f16\u7801\u65b9\u5f0f\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u6709UTF-8\u3001ASCII\u3001ISO-8859-1\u7b49\u3002<\/p>\n<\/p>\n<p><h2>2. \u5b8c\u6210\u4efb\u52a1\u6240\u9700\u7684\u8be6\u7ec6\u3001\u5206\u6b65\u64cd\u4f5c\u6307\u5357<\/h2>\n<\/p>\n<p><h3>\u6b65\u9aa4\u4e00\uff1a\u521b\u5efa\u5b57\u8282\u4e32<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u4e32\u3002\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528Python\u7684<b>bytes()<\/b>\u51fd\u6570\u6216\u7b80\u5355\u5730\u5c06\u5b57\u7b26\u4e32\u524d\u52a0<b>b<\/b>\u6765\u751f\u6210\u3002<\/p>\n<\/p>\n<p><pre><code># \u65b9\u6cd51\uff1a\u4f7f\u7528 bytes() \u51fd\u6570<\/p>\r\n<p>byte_string = bytes(\"Hello, World!\", 'utf-8')<\/p>\r\n\r\n<p># \u65b9\u6cd52\uff1a\u4f7f\u7528 b'' \u8bed\u6cd5<\/p>\r\n<p>byte_string = b'Hello, World!'<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6b65\u9aa4\u4e8c\uff1a\u5c06\u5b57\u8282\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u8981\u5c06\u5b57\u8282\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u9700\u8981\u4f7f\u7528<b>decode()<\/b>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u7f16\u7801\u53c2\u6570\uff0c\u5e38\u89c1\u7684\u5982<b>utf-8<\/b>\u3001<b>ascii<\/b>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code># \u4f7f\u7528 decode() \u65b9\u6cd5\u5c06\u5b57\u8282\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\r\n<p>string_value = byte_string.decode('utf-8')<\/p>\r\n<p>print(string_value)  # \u8f93\u51fa: Hello, World!<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6b65\u9aa4\u4e09\uff1a\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5728\u89e3\u7801\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u51e0\u79cd\u5e38\u89c1\u9519\u8bef\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>UnicodeDecodeError\uff1a<\/strong> \u5f53\u5b57\u8282\u4e32\u5305\u542b\u65e0\u6cd5\u89e3\u7801\u4e3a\u7ed9\u5b9a\u7f16\u7801\u7684\u5b57\u8282\u65f6\uff0c\u4f1a\u5f15\u53d1\u6b64\u9519\u8bef\u3002<\/li>\n<li><strong>TypeError\uff1a<\/strong> \u5982\u679c\u5c1d\u8bd5\u5728\u4e0d\u662f\u5b57\u8282\u4e32\u7684\u5bf9\u8c61\u4e0a\u8c03\u7528<b>decode()<\/b>\u65b9\u6cd5\uff0c\u4e5f\u4f1a\u629b\u51fa\u6b64\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><p>\u5728\u6b64\u5efa\u8bae\u4f7f\u7528<b>try..except<\/b>\u8bed\u53e5\u5904\u7406\u5f02\u5e38\uff0c\u4ee5\u9632\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><pre><code>try:<\/p>\r\n<p>    string_value = byte_string.decode('utf-8')<\/p>\r\n<p>except UnicodeDecodeError as e:<\/p>\r\n<p>    print(f\"\u89e3\u7801\u9519\u8bef: {e}\")<\/p>\r\n<p>except TypeError as e:<\/p>\r\n<p>    print(f\"\u7c7b\u578b\u9519\u8bef: {e}\")<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3. \u6d89\u53ca\u7684\u5173\u952e\u547d\u4ee4\u3001\u4ee3\u7801\u6216\u914d\u7f6e\u793a\u4f8b<\/h2>\n<\/p>\n<p><p>\u6574\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u53ef\u4ee5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code># \u521b\u5efa\u5b57\u8282\u4e32<\/p>\r\n<p>byte_string = b'Hello, World!'<\/p>\r\n\r\n<p># \u5c06\u5b57\u8282\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\r\n<p>try:<\/p>\r\n<p>    string_value = byte_string.decode('utf-8')<\/p>\r\n<p>    print(string_value)  # \u8f93\u51fa: Hello, World!<\/p>\r\n<p>except UnicodeDecodeError as e:<\/p>\r\n<p>    print(f\"\u89e3\u7801\u9519\u8bef: {e}\")<\/p>\r\n<p>except TypeError as e:<\/p>\r\n<p>    print(f\"\u7c7b\u578b\u9519\u8bef: {e}\")<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h2>4. \u5bf9\u547d\u4ee4\u3001\u4ee3\u7801\u6216\u91cd\u8981\u6982\u5ff5\u7684\u6e05\u6670\u89e3\u91ca<\/h2>\n<\/p>\n<p><p><strong>decode()<\/strong>\u65b9\u6cd5\u662fPython\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u4e00\u4e2a\u91cd\u8981\u65b9\u6cd5\uff0c\u5141\u8bb8\u6211\u4eec\u5c06\u5b57\u8282\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u89e3\u7801\u65b9\u5f0f\u5fc5\u987b\u4e0e\u5b57\u8282\u4e32\u7684\u7f16\u7801\u65b9\u5f0f\u5339\u914d\u3002\u5982\u679c\u5b57\u8282\u4e32\u662f\u7528b&#8217;utf-8&#8217;\u7f16\u7801\u7684\uff0c\u5fc5\u987b\u7528<b>decode(&#8216;utf-8&#8217;)<\/b>\u6765\u89e3\u6790\uff0c\u5426\u5219\u5c06\u4f1a\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h2>5. \u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u3001\u6ce8\u610f\u4e8b\u9879\u6216\u76f8\u5173\u7684\u5b9e\u7528\u6280\u5de7<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u5b57\u8282\u4e32\u548c\u5b57\u7b26\u4e32\u65f6\u7684\u4e00\u4e9b\u5c0f\u6280\u5de7\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528<b>utf-8<\/b>\u7f16\u7801\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u51e0\u4e4e\u6240\u6709\u8bed\u8a00\u7684\u5b57\u7b26\u96c6\u3002<\/li>\n<li>\u5728\u89e3\u7801\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<b>errors<\/b>\u53c2\u6570\u6765\u5904\u7406\u9519\u8bef\uff0c\u4f8b\u5982<b>errors=&#8217;ignore&#8217;<\/b>\u53ef\u4ee5\u5ffd\u7565\u65e0\u6cd5\u89e3\u7801\u7684\u5b57\u8282\uff0c<b>errors=&#8217;replace&#8217;<\/b>\u4f1a\u7528\u66ff\u4ee3\u5b57\u7b26\u53d6\u4ee3\u65e0\u6cd5\u89e3\u7801\u7684\u5b57\u8282\u3002<\/li>\n<li>\u8bb0\u5f97\u68c0\u67e5\u5b57\u8282\u4e32\u7684\u7c7b\u578b\uff0c\u786e\u4fdd\u5728\u5bf9\u5176\u8c03\u7528<b>decode()<\/b>\u4e4b\u524d\uff0c\u5b83\u786e\u5b9e\u662f\u4e00\u4e2a\u5b57\u8282\u4e32\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><p>\u603b\u7ed3\u800c\u8a00\uff0c\u5b57\u8282\u4e32\u5230\u5b57\u7b26\u4e32\u7684\u8f6c\u6362\u662fPython\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u57fa\u672c\u6280\u80fd\uff0c\u800c\u638c\u63e1\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u540e\uff0c\u60a8\u5c31\u80fd\u5728\u5b9e\u65f6\u6570\u636e\u5904\u7406\u3001\u7f51\u7edc\u7f16\u7a0b\u7b49\u5e94\u7528\u573a\u666f\u4e2d\u6e38\u5203\u6709\u4f59\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u6709\u65f6\u6211\u4eec\u4f1a\u9047\u5230\u5b57\u8282\u4e32\uff08bytes\uff09\u683c\u5f0f\u7684\u6570\u636e\u3002\u5b57\u8282\u4e32\u901a\u5e38\u4ee5b&#8221;\u7684\u5f62\u5f0f\u5b58\u5728\uff0c\u800c\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u8fd9\u4e9b\u5b57\u8282\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff08str\uff09\u7c7b\u578b\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u548c\u5206\u6790\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5c06\u5b57\u8282\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76&#8230;<\/p>\n","protected":false},"author":1,"featured_media":411,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[59,164,163],"topic":[],"class_list":["post-410","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jishu","tag-python","tag-164","tag-163"],"_links":{"self":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts\/410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/comments?post=410"}],"version-history":[{"count":1,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts\/410\/revisions"}],"predecessor-version":[{"id":413,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts\/410\/revisions\/413"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/media\/411"}],"wp:attachment":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/media?parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/categories?post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/tags?post=410"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/topic?post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}