{"id":940171,"date":"2024-12-26T20:54:43","date_gmt":"2024-12-26T12:54:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/940171.html"},"modified":"2024-12-26T20:54:45","modified_gmt":"2024-12-26T12:54:45","slug":"python-%e5%a6%82%e4%bd%95%e8%bd%acunicode","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/940171.html","title":{"rendered":"python \u5982\u4f55\u8f6cunicode"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074924\/a9e0635e-60e7-4bc1-a86c-291f5a9a6022.webp\" alt=\"python \u5982\u4f55\u8f6cunicode\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8f6c\u4e3aUnicode\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a\u4f7f\u7528<code>str.encode()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528<code>unicode_literals<\/code>\u5bfc\u5165\u3001\u4f7f\u7528<code>chr()<\/code>\u548c<code>ord()<\/code>\u51fd\u6570\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528<code>str.encode()<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06Python\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282\uff0c\u7136\u540e\u518d\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3aUnicode\u5b57\u7b26\u4e32\u3002\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>str.encode()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662fUnicode\u5b57\u7b26\u4e32\uff0c\u4f46\u6709\u65f6\u9700\u8981\u5c06\u5176\u8f6c\u4e3a\u7279\u5b9a\u7f16\u7801\u7684Unicode\u5b57\u7b26\u4e32\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>str.encode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u4e32\uff0c\u7136\u540e\u518d\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u4e3aUnicode\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># Python 3.x<\/p>\n<p>original_string = &quot;Hello, \u4e16\u754c&quot;<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3aUTF-8\u5b57\u8282\u4e32<\/strong><\/h2>\n<p>byte_string = original_string.encode(&#39;utf-8&#39;)<\/p>\n<h2><strong>\u5c06\u5b57\u8282\u4e32\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>unicode_string = byte_string.decode(&#39;utf-8&#39;)<\/p>\n<p>print(unicode_string)  # \u8f93\u51fa: Hello, \u4e16\u754c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>original_string.encode(&#39;utf-8&#39;)<\/code>\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3aUTF-8\u5b57\u8282\u4e32\uff0c\u800c<code>byte_string.decode(&#39;utf-8&#39;)<\/code>\u5219\u5c06\u5176\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u5b57\u7b26\u4e32\u5728\u4e0d\u540c\u7f16\u7801\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>unicode_literals<\/code>\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c\u9ed8\u8ba4\u7684\u5b57\u7b26\u4e32\u4e0d\u662fUnicode\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u9700\u8981\u901a\u8fc7<code>from __future__ import unicode_literals<\/code>\u6765\u81ea\u52a8\u5c06\u6240\u6709\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u89c6\u4e3aUnicode\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># Python 2.x<\/p>\n<p>from __future__ import unicode_literals<\/p>\n<p>original_string = &quot;Hello, \u4e16\u754c&quot;<\/p>\n<p>print(type(original_string))  # \u8f93\u51fa: &lt;type &#39;unicode&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5bfc\u5165<code>unicode_literals<\/code>\uff0cPython 2\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u90fd\u88ab\u89c6\u4e3aUnicode\u5b57\u7b26\u4e32\uff0c\u8fd9\u4f7f\u5f97\u5904\u7406Unicode\u5b57\u7b26\u4e32\u53d8\u5f97\u66f4\u4e3a\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>chr()<\/code>\u548c<code>ord()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406Unicode\u5b57\u7b26\u65f6\uff0c<code>chr()<\/code>\u548c<code>ord()<\/code>\u51fd\u6570\u4e5f\u975e\u5e38\u6709\u7528\u3002<code>chr()<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u5bf9\u5e94\u7684Unicode\u5b57\u7b26\u4e32\uff0c\u800c<code>ord()<\/code>\u51fd\u6570\u8fd4\u56de\u5b57\u7b26\u7684Unicode\u7801\u70b9\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># Python 3.x<\/p>\n<p>unicode_char = chr(20320)  # \u8fd4\u56de\u5b57\u7b26 &#39;\u4f60&#39;<\/p>\n<p>unicode_code_point = ord(&#39;\u4f60&#39;)  # \u8fd4\u56de 20320<\/p>\n<p>print(unicode_char)  # \u8f93\u51fa: \u4f60<\/p>\n<p>print(unicode_code_point)  # \u8f93\u51fa: 20320<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u5b57\u7b26\u548c\u5176Unicode\u7801\u70b9\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u8fd9\u5728\u5904\u7406\u7279\u5b9aUnicode\u5b57\u7b26\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8f6c\u4e3aUnicode\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u9009\u62e9\uff0c\u5177\u4f53\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f7f\u7528\u7684Python\u7248\u672c\u548c\u5177\u4f53\u9700\u6c42\u3002\u5728Python 3\u4e2d\uff0c\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662fUnicode\u5b57\u7b26\u4e32\uff0c\u800c\u5728Python 2\u4e2d\u9700\u8981\u7279\u522b\u5904\u7406\u3002\u65e0\u8bba\u662f\u901a\u8fc7<code>str.encode()<\/code>\u65b9\u6cd5\u3001<code>unicode_literals<\/code>\u5bfc\u5165\uff0c\u8fd8\u662f\u4f7f\u7528<code>chr()<\/code>\u548c<code>ord()<\/code>\u51fd\u6570\uff0c\u90fd\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0Unicode\u5b57\u7b26\u4e32\u7684\u8f6c\u6362\u548c\u5904\u7406\u3002\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u7075\u6d3b\u8fd0\u7528\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u597d\u5730\u5904\u7406Unicode\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUnicode\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6240\u6709\u5b57\u7b26\u4e32\u90fd\u662fUnicode\u5b57\u7b26\u4e32\u3002\u5982\u679c\u4f60\u60f3\u5c06\u4e00\u4e2a\u666e\u901a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUnicode\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.encode()<\/code>\u65b9\u6cd5\u3002\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5b57\u8282\u4e32\u3002\u4f8b\u5982\uff0c<code>my_string = &quot;hello&quot;<\/code>\uff0c\u4f7f\u7528<code>unicode_string = my_string.encode(&#39;utf-8&#39;)<\/code>\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801\u7684\u5b57\u8282\u4e32\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728Python 3\u4e2d\uff0c\u5b57\u7b26\u4e32\u9ed8\u8ba4\u5c31\u662fUnicode\u3002<\/p>\n<p><strong>Unicode\u7f16\u7801\u6709\u4ec0\u4e48\u5e38\u89c1\u7684\u7528\u9014\uff1f<\/strong><br \/>Unicode\u7f16\u7801\u5728\u5904\u7406\u4e0d\u540c\u8bed\u8a00\u548c\u7b26\u53f7\u65f6\u975e\u5e38\u91cd\u8981\u3002\u5b83\u53ef\u4ee5\u786e\u4fdd\u6587\u672c\u5728\u4e0d\u540c\u5e73\u53f0\u548c\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u7684\u4e00\u81f4\u6027\uff0c\u7279\u522b\u662f\u5728\u6d89\u53ca\u591a\u79cd\u8bed\u8a00\u7684\u60c5\u51b5\u4e0b\u3002\u5f00\u53d1\u8005\u901a\u5e38\u4f7f\u7528Unicode\u6765\u4fdd\u8bc1\u5b57\u7b26\u7684\u6b63\u786e\u663e\u793a\uff0c\u907f\u514d\u51fa\u73b0\u4e71\u7801\u95ee\u9898\u3002\u5728Web\u5f00\u53d1\u4e2d\uff0cUnicode\u4e5f\u4f7f\u5f97\u7f51\u7ad9\u80fd\u591f\u652f\u6301\u5168\u7403\u7528\u6237\u3002<\/p>\n<p><strong>\u5982\u4f55\u67e5\u770bPython\u4e2d\u5b57\u7b26\u4e32\u7684Unicode\u7f16\u7801\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5355\u4e2a\u5b57\u7b26\u7684Unicode\u7801\u70b9\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff0c\u53ef\u4ee5\u901a\u8fc7<code>ord(&#39;A&#39;)<\/code>\u83b7\u5f97\u5176Unicode\u7801\u70b9\u3002\u5bf9\u4e8e\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u904d\u5386\u6bcf\u4e2a\u5b57\u7b26\u5e76\u6253\u5370\u5176Unicode\u503c\uff0c\u4f8b\u5982\uff1a<code>[ord(char) for char in my_string]<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u4e86\u89e3\u5b57\u7b26\u5728Unicode\u6807\u51c6\u4e2d\u7684\u8868\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8f6c\u4e3aUnicode\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a\u4f7f\u7528str.encode()\u65b9\u6cd5\u3001\u4f7f\u7528unic [&hellip;]","protected":false},"author":3,"featured_media":940174,"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\/940171"}],"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=940171"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940171\/revisions"}],"predecessor-version":[{"id":940175,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940171\/revisions\/940175"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/940174"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=940171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=940171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=940171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}