{"id":50210,"date":"2024-12-03T10:14:04","date_gmt":"2024-12-03T02:14:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50210\/"},"modified":"2024-12-03T10:14:04","modified_gmt":"2024-12-03T02:14:04","slug":"python-%e5%a6%82%e4%bd%95%e8%a7%a3%e7%a0%81%e5%ad%97%e7%ac%a6%e4%b8%b2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50210\/","title":{"rendered":"Python \u5982\u4f55\u89e3\u7801\u5b57\u7b26\u4e32\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5982\u4f55\u89e3\u7801\u5b57\u7b26\u4e32\uff1f<\/h1>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aPython \u5982\u4f55\u89e3\u7801\u5b57\u7b26\u4e32\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241114\/173156673667359c905044b.jpg\" class=\"aligncenter\" title=\"Python \u5982\u4f55\u89e3\u7801\u5b57\u7b26\u4e32\uff1f\u63d2\u56fe\" alt=\"Python \u5982\u4f55\u89e3\u7801\u5b57\u7b26\u4e32\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>python \u89e3\u7801\u5b57\u7b26\u4e32<\/strong><\/p>\n<p>\u5728 python \u4e2d\uff0c\u4f7f\u7528 encode() \u548c decode() \u65b9\u6cd5\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u975e\u5e38\u5e38\u89c1\u3002encode() \u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5e8f\u5217\uff0c\u800c decode() \u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u8282\u5e8f\u5217\u8fd8\u539f\u4e3a\u5b57\u7b26\u4e32\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u53ea\u6709\u5b57\u7b26\u4e32\u7c7b\u578b\u53ef\u4ee5\u8c03\u7528 encode() \u65b9\u6cd5\uff0c\u800c\u53ea\u6709\u5b57\u8282\u7c7b\u578b\u53ef\u4ee5\u8c03\u7528 decode() \u65b9\u6cd5\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>e1 = \"\u4e2d\u56fd\".encode('gbk')  # b'\\xd6\\xd0\\xb9\\xfa' \u4e3a\u5b57\u8282\u7c7b\u578b\ne2 = str(e1)  # b'\\xd6\\xd0\\xb9\\xfa' \u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b<\/pre>\n<p>\u73b0\u5728\uff0c\u8981\u5c06 e2 \u8868\u793a\u7684\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a\u539f\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528 decode() \u65b9\u6cd5\uff1a<\/p>\n<pre>e1 = \"\u4e2d\u56fd\".encode('gbk')\ne2 = e1.decode(\"gbk\")\nprint(e1, e2)<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>b'\\xd6\\xd0\\xb9\\xfa' \u4e2d\u56fd<\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>e1 \u662f\u5b57\u8282\u7c7b\u578b\uff0c\u8868\u793a\u7f16\u7801\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<li>e2 \u662f\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u8868\u793a\u89e3\u7801\u540e\u7684\u539f\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aPython \u5982\u4f55\u89e3\u7801\u5b57\u7b26\u4e32\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5982\u4f55\u89e3\u7801\u5b57\u7b26\u4e32\uff1f \u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aPython \u5982\u4f55\u89e3\u7801\u5b57\u7b26\u4e32\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01 python \u89e3\u7801\u5b57\u7b26\u4e32 \u5728 python \u4e2d\uff0c\u4f7f\u7528 encode() \u548c decode() \u65b9\u6cd5\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u975e\u5e38\u5e38\u89c1\u3002encode() \u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5e8f\u5217\uff0c\u800c decode() \u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u8282\u5e8f\u5217\u8fd8\u539f\u4e3a\u5b57\u7b26\u4e32\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u53ea\u6709\u5b57\u7b26\u4e32\u7c7b\u578b\u53ef\u4ee5\u8c03\u7528 encode() \u65b9\u6cd5\uff0c\u800c\u53ea\u6709\u5b57\u8282\u7c7b\u578b\u53ef\u4ee5\u8c03\u7528 decode() \u65b9\u6cd5\u3002 \u4f8b\u5982\uff1a e1 = &#8220;\u4e2d\u56fd&#8221;.encode(&#8216;gbk&#8217;) # b&#8217;\\xd6\\xd0\\xb9\\xfa&#8217; \u4e3a\u5b57\u8282\u7c7b\u578b e2 = str(e1) # b&#8217;\\xd6\\xd0\\xb9\\xfa&#8217; \u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b \u73b0\u5728\uff0c\u8981\u5c06 e2 \u8868\u793a\u7684\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a\u539f\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528 decode() \u65b9\u6cd5\uff1a e1 = &#8220;\u4e2d\u56fd&#8221;.encode(&#8216;gbk&#8217;) e2 = e1.decode(&#8220;gbk&#8221;) print(e1, e2) \u8f93\u51fa\uff1a b&#8217;\\xd6\\xd0\\xb9\\xfa&#8217; \u4e2d\u56fd \u5176\u4e2d\uff1a e1 \u662f\u5b57\u8282\u7c7b\u578b\uff0c\u8868\u793a\u7f16\u7801\u540e\u7684\u5b57\u7b26\u4e32\u3002 e2 \u662f\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u8868\u793a\u89e3\u7801\u540e\u7684\u539f\u5185\u5bb9\u3002 \u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aPython [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-50210","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=50210"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50210\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}