{"id":175912,"date":"2024-05-08T19:00:24","date_gmt":"2024-05-08T11:00:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/175912.html"},"modified":"2024-05-08T19:00:34","modified_gmt":"2024-05-08T11:00:34","slug":"python2-%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e4%b8%80%e4%b8%aa%e7%bb%93%e6%9e%9c%e4%b8%ad%e7%9a%84unicode%e7%bc%96%e7%a0%81%e8%bd%ac%e6%8d%a2%e4%b8%ba%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/175912.html","title":{"rendered":"Python2 \u4e2d\u5982\u4f55\u5c06\u4e00\u4e2a\u7ed3\u679c\u4e2d\u7684Unicode\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27054006\/a9132cb4-bd9a-4c3d-8574-6e95bbc34d46.webp\" alt=\"Python2 \u4e2d\u5982\u4f55\u5c06\u4e00\u4e2a\u7ed3\u679c\u4e2d\u7684Unicode\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587\" \/><\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c\u5c06\u4e00\u4e2a\u7ed3\u679c\u4e2d\u7684Unicode\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587\u662f\u4e00\u9879\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u5728\u5904\u7406\u6587\u672c\u6216\u4e0e\u56fd\u9645\u5316\u76f8\u5173\u7684\u5e94\u7528\u7a0b\u5e8f\u65f6\u3002<strong>\u8981\u5b9e\u73b0\u8fd9\u4e00\u8f6c\u6362\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u3001\u5229\u7528\u5185\u5efa\u7684<code>unicode()<\/code>\u51fd\u6570\u3001\u6216\u8005\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>codecs<\/code>\u3002<\/strong><\/p>\n<\/p>\n<p><p><code>decode()<\/code>\u65b9\u6cd5\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u3002\u5728Python 2\u4e2d\uff0c\u6240\u6709\u5b57\u7b26\u4e32\u9ed8\u8ba4\u4ee5ASCII\u7f16\u7801\uff0c\u800c<code>decode()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u4ece\u6307\u5b9a\u7684\u7f16\u7801\u8f6c\u6362\u4e3aUnicode\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u4f60\u4ece\u6587\u4ef6\u3001\u7f51\u7edc\u6216\u5176\u4ed6\u6e90\u83b7\u53d6\u5230\u4e86\u4ee5Unicode\u7f16\u7801\u7684\u4e2d\u6587\u5b57\u7b26\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u4e2d\u6587\u3002\u5177\u4f53\u800c\u8a00\uff0c\u5982\u679c\u4f60\u83b7\u5f97\u4e86\u4e00\u4e32Unicode\u7f16\u7801\u7684\u6587\u672c\uff0c\u5982<code>u&#039;\\u4e2d\\u6587&#039;<\/code>\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>&#039;u&#039;\\u4e2d\\u6587&#039;.decode(&#039;unicode_escape&#039;)<\/code>\u6765\u5f97\u5230\u4e2d\u6587\u5b57\u7b26&quot;\u4e2d\u6587&quot;\u3002\u8fd9\u662f\u56e0\u4e3a<code>decode(&#039;unicode_escape&#039;)<\/code>\u4f1a\u5c06Unicode\u7f16\u7801\u7684\u8f6c\u4e49\u5e8f\u5217\u8f6c\u6362\u6210\u5bf9\u5e94\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528DECODE()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>decode()<\/code>\u65b9\u6cd5\u662f\u5904\u7406Unicode\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587\u7684\u57fa\u7840\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9700\u8981\u4f60\u77e5\u6653\u6570\u636e\u7684\u539f\u59cb\u7f16\u7801\u683c\u5f0f\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u662fUTF-8\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u9996\u5148\uff0c\u786e\u5b9a\u4f60\u7684\u5b57\u7b26\u4e32\u662f\u4ee5Unicode\u5b58\u50a8\u3002<\/strong> \u5728Python 2\u4e2d\uff0c\u6240\u6709\u5b57\u7b26\u4e32\u9ed8\u8ba4\u4e3aASCII\u7f16\u7801\uff0c\u5982\u679c\u8981\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\uff0c\u4f60\u9700\u8981\u5728\u5b57\u7b26\u4e32\u524d\u9762\u52a0\u4e0a<code>u<\/code>\uff0c\u6bd4\u5982<code>u&quot;\u4f60\u597d\u4e16\u754c&quot;<\/code>\u3002<\/li>\n<li>\u5176\u6b21\uff0c\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u5c06Unicode\u7f16\u7801\u8f6c\u6362\u6210\u2018utf-8\u2019\u7f16\u7801\u7684\u4e2d\u6587\u6587\u672c\u3002\u4f8b\u5982\uff0c<code>u&#039;\\u4e2d\\u6587&#039;.decode(&#039;unicode_escape&#039;)<\/code>\u5c06\u4f1a\u8f93\u51fa\u4e2d\u6587\u201c\u4e2d\u6587\u201d\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528UNICODE()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>unicode()<\/code>\u51fd\u6570\u662f\u53e6\u4e00\u79cd\u5c06Unicode\u7f16\u7801\u8f6c\u6362\u4e3a\u53ef\u8bfb\u4e2d\u6587\u7684\u65b9\u6cd5\u3002\u5b83\u901a\u5e38\u4e0e<code>str<\/code>\u7c7b\u578b\u7684\u5bf9\u8c61\u4e00\u8d77\u4f7f\u7528\uff0c\u7528\u4e8e\u5c06\u4efb\u4f55\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3aUnicode\u7f16\u7801\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5229\u7528<code>unicode()<\/code>\u51fd\u6570\u65f6\uff0c\u4f60\u9700\u8981\u663e\u5f0f\u6307\u5b9a\u539f\u59cb\u7f16\u7801\u5f62\u5f0f\u3002<\/strong> \u4f8b\u5982\uff0c<code>unicode(&#039;\\xe4\\xb8\\xad\\xe6\\x96\\x87&#039;, &#039;utf-8&#039;)<\/code>\u4f1a\u6b63\u786e\u8f6c\u6362\u5e76\u8f93\u51fa\u4e2d\u6587\u201c\u4e2d\u6587\u201d\u3002<\/li>\n<li><strong>\u4e0d\u540c\u4e8e<code>decode()<\/code>\u65b9\u6cd5\uff0c<code>unicode()<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u5e94\u7528\u4e8e\u975eUnicode\u5b57\u7b26\u4e32\u4e0a\uff0c\u65e0\u9700\u9884\u5148\u5c06\u5b57\u7b26\u4e32\u58f0\u660e\u4e3aUnicode\u3002<\/strong><\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u5229\u7528CODECS\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>codecs<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4e3a\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u548c\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u8f6c\u6362\u95ee\u9898\u3002\u5b83\u652f\u6301\u5e7f\u6cdb\u7684\u7f16\u7801\u548c\u89e3\u7801\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u6587\u4ef6IO\u64cd\u4f5c\u65f6\u7684\u7f16\u7801\u8f6c\u6362\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528<code>codecs.open()<\/code>\u51fd\u6570\u66ff\u4ee3\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u8bfb\u53d6\u5177\u6709\u7279\u5b9a\u7f16\u7801\u7684\u6587\u4ef6\u3002<\/strong> \u8fd9\u610f\u5473\u7740\uff0c\u5f53\u4f60\u8bfb\u53d6\u4e00\u4e2a\u4ee5UTF-8\u7f16\u7801\u7684\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>codecs.open()<\/code>\u76f4\u63a5\u4ee5Unicode\u7684\u5f62\u5f0f\u8bfb\u53d6\uff0c\u65e0\u9700\u989d\u5916\u7684\u89e3\u7801\u6b65\u9aa4\u3002<\/li>\n<li><strong>\u5728\u5904\u7406\u6587\u4ef6\u8f93\u51fa\u65f6\uff0c<code>codecs<\/code>\u4e5f\u540c\u6837\u65b9\u4fbf\u3002<\/strong> \u901a\u8fc7\u6307\u5b9a\u7f16\u7801\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u5199\u5165\u6587\u4ef6\u7684\u5185\u5bb9\u662f\u6b63\u786e\u7f16\u7801\u7684\u4e2d\u6587\u5b57\u7b26\uff0c\u907f\u514d\u4e86\u4e71\u7801\u7684\u4ea7\u751f\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5c0f\u7ed3\u548c\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c\u5904\u7406Unicode\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587\u6d89\u53ca\u5230\u77e5\u6653\u7f16\u7801\u683c\u5f0f\u3001\u5408\u7406\u9009\u62e9\u8f6c\u6362\u65b9\u6cd5\u53ca\u5728\u9700\u8981\u65f6\u4f7f\u7528\u76f8\u5e94\u7684\u7b2c\u4e09\u65b9\u5e93\u3002<strong>\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u5bf9\u539f\u59cb\u6570\u636e\u7f16\u7801\u6709\u6e05\u6670\u7684\u8ba4\u8bc6\u3002<\/strong> \u8fd9\u662f\u6b63\u786e\u5904\u7406\u7f16\u7801\u8f6c\u6362\u7684\u524d\u63d0\u3002<\/li>\n<li><strong>\u4f18\u5148\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u548c<code>unicode()<\/code>\u51fd\u6570\u5904\u7406\u5185\u5b58\u4e2d\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u95ee\u9898\uff0c\u4fdd\u8bc1\u6570\u636e\u5904\u7406\u7684\u7075\u6d3b\u6027\u548c\u6548\u7387\u3002<\/strong><\/li>\n<li><strong>\u5728\u5904\u7406\u6587\u4ef6\u8bfb\u5199\u65f6\uff0c\u8003\u8651\u4f7f\u7528<code>codecs<\/code>\u6a21\u5757\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7ba1\u7406\u6587\u4ef6\u7f16\u7801\u95ee\u9898\u3002<\/strong><\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u6709\u6548\u89e3\u51b3Python 2\u4e2dUnicode\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587\u7684\u9700\u6c42\uff0c\u4fdd\u8bc1\u6587\u672c\u6570\u636e\u7684\u6b63\u786e\u5904\u7406\u548c\u663e\u793a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5c06Python2\u4e2d\u7684Unicode\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><\/p>\n<ol>\n<li><strong>\u6211\u5728Python2\u4e2d\u4f7f\u7528Unicode\u7f16\u7801\u8868\u793a\u4e2d\u6587\u5b57\u7b26\uff0c\u5982\u4f55\u5c06\u5176\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><\/li>\n<\/ol>\n<p>\u5728Python2\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>.decode()<\/code>\u65b9\u6cd5\u5c06Unicode\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2aUnicode\u7f16\u7801\u4e3a<code>u&#039;\\u4e2d\\u6587&#039;<\/code>\u7684\u5b57\u7b26\u4e32\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>u&#039;\\u4e2d\\u6587&#039;.decode(&#039;unicode-escape&#039;)<\/code>\u6765\u5f97\u5230\u76f8\u5e94\u7684\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<ol start=\"2\">\n<li><strong>\u5982\u4f55\u5904\u7406Python2\u4e2d UnicodeDecodeError \u7684\u9519\u8bef\uff0c\u4ee5\u4fbf\u6b63\u786e\u8f6c\u6362\u7f16\u7801\uff1f<\/strong><\/li>\n<\/ol>\n<p>\u5728Python2\u4e2d\uff0c\u5728\u5c06Unicode\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230<code>UnicodeDecodeError<\/code>\u9519\u8bef\u3002\u8fd9\u901a\u5e38\u662f\u56e0\u4e3aPython\u65e0\u6cd5\u786e\u5b9a\u6b63\u786e\u7684\u5b57\u7b26\u7f16\u7801\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>.encode()<\/code>\u65b9\u6cd5\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9047\u5230<code>UnicodeDecodeError<\/code>\u9519\u8bef\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528<code>u&#039;\\u4e2d\\u6587&#039;.encode(&#039;utf-8&#039;)<\/code>\u6765\u8f6c\u6362\u7f16\u7801\u3002<\/p>\n<ol start=\"3\">\n<li><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u5728Python2\u4e2d\u5c06Unicode\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><\/li>\n<\/ol>\n<p>\u9664\u4e86\u4f7f\u7528<code>.decode()<\/code>\u548c<code>.encode()<\/code>\u65b9\u6cd5\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>unicode()<\/code>\u548c<code>str()<\/code>\u51fd\u6570\u6765\u5b8c\u6210Unicode\u7f16\u7801\u7684\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>unicode(u&#039;\\u4e2d\\u6587&#039;, &#039;unicode-escape&#039;)<\/code>\u5c06Unicode\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587\u5b57\u7b26\uff0c\u6216\u8005\u4f7f\u7528<code>str(u&#039;\\u4e2d\\u6587&#039;)<\/code>\u6765\u5f97\u5230\u76f8\u540c\u7684\u7ed3\u679c\u3002\u8fd9\u4e9b\u65b9\u6cd5\u63d0\u4f9b\u4e86\u591a\u79cd\u9009\u62e9\uff0c\u4ee5\u4fbf\u5728Python2\u4e2d\u8f6c\u6362Unicode\u7f16\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python 2\u4e2d\uff0c\u5c06\u4e00\u4e2a\u7ed3\u679c\u4e2d\u7684Unicode\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587\u662f\u4e00\u9879\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u5728\u5904\u7406\u6587\u672c\u6216\u4e0e\u56fd\u9645\u5316\u76f8\u5173 [&hellip;]","protected":false},"author":3,"featured_media":175934,"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\/175912"}],"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=175912"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/175912\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/175934"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=175912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=175912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=175912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}