{"id":204665,"date":"2025-05-29T08:30:16","date_gmt":"2025-05-29T00:30:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204665\/"},"modified":"2025-05-29T08:30:16","modified_gmt":"2025-05-29T00:30:16","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-%e8%bf%9b%e8%a1%8c%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%a7%a3%e7%a0%81%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204665\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u8fdb\u884c\u5b57\u7b26\u4e32\u89e3\u7801\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Python \u8fdb\u884c\u5b57\u7b26\u4e32\u89e3\u7801\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5982\u4f55\u4f7f\u7528 Python \u8fdb\u884c\u5b57\u7b26\u4e32\u89e3\u7801\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241112\/17314171626733544ac92b8.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5982\u4f55\u4f7f\u7528 python \u8fdb\u884c\u89e3\u7801<\/p>\n<p>python \u4e2d\u63d0\u4f9b encode() \u548c decode() \u65b9\u6cd5\u6765\u5904\u7406\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u548c\u89e3\u7801\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\u7f16\u7801\uff0c\u53ea\u6709 str \u7c7b\u578b\u53ef\u4ee5\u8c03\u7528 encode() \u65b9\u6cd5\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e (bytes \u7c7b\u578b)\u3002\u800c\u5bf9\u4e8e\u5b57\u7b26\u4e32\u89e3\u7801\uff0c\u53ea\u6709 bytes \u7c7b\u578b\u53ef\u4ee5\u8c03\u7528 decode() \u65b9\u6cd5\uff0c\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u6211\u4eec\u4ee5\u4e00\u4e2a\u793a\u4f8b\u6765\u8bf4\u660e\u5982\u4f55\u89e3\u7801\uff1a<\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a bytes \u7c7b\u578b\u7684\u6570\u636e e1\uff0c\u5b83\u8868\u793a\u7ecf\u8fc7 gbk \u7f16\u7801\u540e\u7684\u4e2d\u6587\u5b57\u7b26\u4e32\u3002\u73b0\u5728\u60f3\u8981\u5c06\u8fd9\u4e2a bytes \u7c7b\u578b\u7684\u6570\u636e\u89e3\u7801\u4e3a\u539f\u59cb\u4e2d\u6587\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<pre>e1 = \"\u4e2d\u56fd\".encode('gbk')\ne2 = e1.decode(\"gbk\")\nprint(e1, e2)<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff1a<\/p>\n<ul>\n<li>e1 \u662f\u7ecf\u8fc7 gbk \u7f16\u7801\u540e\u7684 bytes \u7c7b\u578b\u6570\u636e\u3002<\/li>\n<li>e2 \u662f\u901a\u8fc7\u8c03\u7528 e1.decode(&#8220;gbk&#8221;) \u89e3\u7801\u540e\u7684 str \u7c7b\u578b\u6570\u636e\uff0c\u5b83\u8868\u793a\u539f\u59cb\u4e2d\u6587\u5b57\u7b26\u4e32\u3002<\/li>\n<li>print(e1, e2) \u8f93\u51fa e1 \u548c e2 \u7684\u5185\u5bb9\u3002\u8f93\u51fa\u7ed3\u679c\u4e2d\uff0ce1 \u662f bytes \u7c7b\u578b\u6570\u636e\uff0ce2 \u662f\u89e3\u7801\u540e\u7684\u4e2d\u6587\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Python \u8fdb\u884c\u5b57\u7b26\u4e32\u89e3\u7801\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Python \u8fdb\u884c\u5b57\u7b26&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204665","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204665"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204665\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}