{"id":305592,"date":"2024-05-20T19:50:20","date_gmt":"2024-05-20T11:50:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/305592.html"},"modified":"2024-05-20T19:50:39","modified_gmt":"2024-05-20T11:50:39","slug":"python3-%e7%bc%96%e7%a8%8b%e4%bb%a3%e7%a0%81%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0-unicode-%e4%bb%a3%e7%90%86%e7%bc%96%e7%a8%8b-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/305592.html","title":{"rendered":"python3 \u7f16\u7a0b\u4ee3\u7801\u5982\u4f55\u5b9e\u73b0 Unicode \u4ee3\u7406\u7f16\u7a0b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26221829\/6b61d49b-b7d1-47eb-a031-32a5caa499c9.webp\" alt=\"python3 \u7f16\u7a0b\u4ee3\u7801\u5982\u4f55\u5b9e\u73b0 Unicode \u4ee3\u7406\u7f16\u7a0b\" \/><\/p>\n<p><p>\u5728Python3\u4e2d\u5b9e\u73b0Unicode\u4ee3\u7406\u7f16\u7a0b\u4e3b\u8981\u6d89\u53ca\u5230<strong>Unicode\u5b57\u7b26\u4e32\u5904\u7406\u3001\u7406\u89e3\u4ee3\u7406\u5bf9\u3001\u4ee5\u53ca\u5b57\u7b26\u7f16\u7801\u8f6c\u6362<\/strong>\u3002Python3\u9ed8\u8ba4\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\uff0c\u4f7f\u5f97\u5904\u7406\u56fd\u9645\u5316\u6587\u672c\u53d8\u5f97\u5bb9\u6613\u3002Unicode\u4ee3\u7406\u5bf9\u662f\u6307\u4e00\u5bf916\u4f4d\u7801\u5355\u5143\uff0c\u7528\u4ee5\u7f16\u7801\u8f83\u5927\u7684\u5b57\u7b26\uff0c\u8fd9\u5bf9\u4e8e\u7406\u89e3\u548c\u64cd\u4f5c\u8303\u56f4\u5728U+10000\u5230U+10FFFF\u4e4b\u95f4\u7684\u5b57\u7b26\u5c24\u5176\u5173\u952e\u3002\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\u5219\u662f\u6307\u5c06\u5b57\u7b26\u4e32\u4ece\u4e00\u79cd\u7f16\u7801\u8f6c\u6362\u5230\u53e6\u4e00\u79cd\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><p>\u5728\u7406\u89e3\u4ee3\u7406\u5bf9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e86\u89e3Python\u4e2d\u7684<code>char<\/code>\u7c7b\u578b\u5982\u4f55\u5904\u7406\u4ee3\u7406\u5bf9\u81f3\u5173\u91cd\u8981\u3002\u4ee3\u7406\u5bf9\u5141\u8bb8Unicode\u6807\u51c6\u5305\u542b\u8d85\u8fc765536\u4e2a\u5b57\u7b26\uff0c\u8fd9\u662f\u901a\u8fc7\u5c06\u8fd9\u4e9b\u8f83\u5927\u7684\u5b57\u7b26\u7f16\u7801\u4e3a\u4e00\u5bf9\u66f4\u5c0f\u7684\u5b57\u7b26\u5b9e\u73b0\u7684\u3002\u5728Python\u4e2d\uff0c\u5f53\u4f60\u4f7f\u7528\u5982<code>\\U0001F600<\/code>\u8fd9\u6837\u7684\u8868\u60c5\u5b57\u7b26\u65f6\uff0cPython\u5185\u90e8\u5b9e\u9645\u4e0a\u4f7f\u7528\u4e00\u5bf916\u4f4d\u7684\u5b57\u7b26\u6765\u8868\u793a\u5b83\u3002\u7406\u89e3\u8fd9\u4e2a\u539f\u7406\u662f\u5b9e\u73b0Unicode\u4ee3\u7406\u7f16\u7a0b\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u7406\u89e3Unicode\u548c\u4ee3\u7406\u5bf9<\/h2>\n<\/p>\n<p><p>\u5728Unicode\u4e2d\uff0c\u5b57\u7b26\u96c6\u5927\u4e8e16\u4f4d\uff08\u537365536\uff09\u65f6\uff0c\u5c31\u9700\u8981\u4f7f\u7528\u4ee3\u7406\u5bf9\u6765\u8868\u8fbe\u8fd9\u4e9b\u5b57\u7b26\u3002\u4ee3\u7406\u5bf9\u662f\u7531\u4e24\u4e2a16\u4f4d\u7684\u7f16\u7801\u5355\u5143\u7ec4\u6210\uff0c\u4f7f\u7528\u8303\u56f4\u5728U+D800\u5230U+DFFF\u4e4b\u95f4\u3002\u5f53\u5904\u7406\u5305\u542b\u8fd9\u4e9b\u4ee3\u7801\u70b9\u7684\u6587\u672c\u65f6\uff0c\u7406\u89e3\u548c\u6b63\u786e\u5904\u7406\u4ee3\u7406\u5bf9\u5c31\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728Python\u4e2d\u5904\u7406\u5305\u542b\u4ee3\u7406\u5bf9\u7684\u5b57\u7b26\u4e32\uff0c\u9996\u5148\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u89e3\u7801\u6216\u7f16\u7801\u3002Python\u7684<code>str<\/code>\u7c7b\u578b\u5728\u5185\u90e8\u4f7f\u7528Unicode\uff0c\u800c\u5904\u7406\u5916\u90e8\u6570\u636e\u65f6\uff0c\u5982\u6587\u4ef6I\/O\uff0c\u7f51\u7edc\u901a\u4fe1\u7b49\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u975eUnicode\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u65f6\uff0c\u5c31\u9700\u8981\u4f7f\u7528Python\u7684\u7f16\u7801\u548c\u89e3\u7801\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u7f16\u7801\u4e0e\u89e3\u7801Unicode\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u7f16\u7801\u662f\u5c06Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u7279\u5b9a\u7f16\u7801\u7684\u5b57\u8282\u5e8f\u5217\u7684\u8fc7\u7a0b\uff0c\u800c\u89e3\u7801\u662f\u5c06\u8fd9\u4e9b\u5b57\u8282\u5e8f\u5217\u8f6c\u6362\u56deUnicode\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\u3002Python\u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u652f\u6301\u6765\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u7f16\u7801Unicode\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u7f16\u7801\u4e00\u4e2a\u5b57\u7b26\u4e32\u901a\u5e38\u4f7f\u7528<code>str.encode()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5c06<code>str<\/code>\u7c7b\u578b\u7684Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a<code>bytes<\/code>\u5bf9\u8c61\uff0c\u8fd9\u662f\u4e00\u4e2a\u5b57\u8282\u5e8f\u5217\uff0c\u8868\u793a\u4f7f\u7528\u7279\u5b9a\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;\u7f16\u7801\u6d4b\u8bd5&quot;<\/p>\n<p>encoded_text = text.encode(&#039;utf-8&#039;)<\/p>\n<p>print(encoded_text)  # b&#039;\\xe7\\xbc\\x96\\xe7\\xa0\\x81\\xe6\\xb5\\x8b\\xe8\\xaf\\x95&#039;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u89e3\u7801Unicode\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u76f8\u53cd\u5730\uff0c\u89e3\u7801\u4e00\u4e2a\u5b57\u7b26\u4e32\u901a\u5e38\u4f7f\u7528<code>bytes.decode()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5c06<code>bytes<\/code>\u5bf9\u8c61\u8f6c\u6362\u56de<code>str<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">encoded_text = b&#039;\\xe7\\xbc\\x96\\xe7\\xa0\\x81\\xe6\\xb5\\x8b\\xe8\\xaf\\x95&#039;<\/p>\n<p>decoded_text = encoded_text.decode(&#039;utf-8&#039;)<\/p>\n<p>print(decoded_text)  # \u7f16\u7801\u6d4b\u8bd5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u64cd\u4f5cUnicode\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u5904\u7406Unicode\u5b57\u7b26\u4e32\u65f6\uff0c\u4e86\u89e3\u5982\u4f55\u64cd\u4f5c\u5176\u4e2d\u7684\u5b57\u7b26\u662f\u5fc5\u8981\u7684\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u65b9\u6cd5\u6765\u641c\u7d22\u3001\u5206\u5272\u3001\u66ff\u6362\u7b49\u64cd\u4f5c\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u641c\u7d22\u548c\u66ff\u6362\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u63d0\u4f9b\u4e86<code>find()<\/code>\u548c<code>replace()<\/code>\u65b9\u6cd5\uff0c\u7528\u4e8e\u641c\u7d22\u548c\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World!&quot;<\/p>\n<p>print(text.find(&quot;World&quot;))  # \u8f93\u51fa 7<\/p>\n<p>print(text.replace(&quot;Hello&quot;, &quot;Hi&quot;))  # \u8f93\u51fa Hi, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5206\u5272\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u7684<code>split()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;a,b,c&quot;<\/p>\n<p>print(text.split(&quot;,&quot;))  # \u8f93\u51fa [&#039;a&#039;, &#039;b&#039;, &#039;c&#039;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5904\u7406\u4ee3\u7406\u5bf9<\/h2>\n<\/p>\n<p><p>\u5f53\u6d89\u53ca\u5230\u5904\u7406\u8d85\u51fa\u57fa\u672c\u591a\u8bed\u8a00\u5e73\u9762\u7684\u5b57\u7b26\u65f6\uff0c\u4ee3\u7406\u5bf9\u7684\u5904\u7406\u5c31\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002Python\u7684Unicode\u5904\u7406\u673a\u5236\u5df2\u7ecf\u7b80\u5316\u4e86\u8bb8\u591a\u76f8\u5173\u7684\u64cd\u4f5c\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4e86\u89e3\u5982\u4f55\u8bc6\u522b\u548c\u64cd\u4f5c\u4ee3\u7406\u5bf9\u4ecd\u7136\u5f88\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3>\u8bc6\u522b\u4ee3\u7406\u5bf9<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>char<\/code>\u7c7b\u578b\u7684<code>is_surrogate<\/code> \u65b9\u6cd5\u6765\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u662f\u5426\u662f\u4ee3\u7406\u5bf9\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u521b\u5efa\u548c\u4f7f\u7528\u4ee3\u7406\u5bf9<\/h3>\n<\/p>\n<p><p>\u5728\u9700\u8981\u8868\u793a\u8303\u56f4\u5728U+10000\u5230U+10FFFF\u4e4b\u95f4\u7684\u5b57\u7b26\u65f6\u4f7f\u7528\u4ee3\u7406\u5bf9\u3002\u5728Python\u4e2d\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7Unicode\u8f6c\u4e49\u5e8f\u5217\u6765\u521b\u5efa\u8fd9\u4e9b\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u8868\u793a\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;\\U0001F600&quot;  # \u8868\u60c5\u7b26\u53f7 \ud83d\ude00<\/p>\n<p>print(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5904\u7406Unicode\u548c\u4ee3\u7406\u5bf9\u9700\u8981\u6b63\u786e\u7406\u89e3Unicode\u6807\u51c6\u4ee5\u53caPython\u4e2d\u7684\u5b9e\u73b0\u7ec6\u8282\u3002\u901a\u8fc7\u6b63\u786e\u4f7f\u7528\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3001\u4ee5\u53ca\u719f\u6089\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\u548c\u4ee3\u7406\u5bf9\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5728Python3\u4e2d\u5b9e\u73b0Unicode\u4ee3\u7406\u7f16\u7a0b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u8bf7\u95ee\u5728 Python3 \u4e2d\u5982\u4f55\u5b9e\u73b0 Unicode \u4ee3\u7406\u7f16\u7a0b\uff1f<\/strong><\/p>\n<p>Unicode \u4ee3\u7406\u7f16\u7a0b\u662f\u6307\u4f7f\u7528 Python3 \u7f16\u7a0b\u8bed\u8a00\u5904\u7406 Unicode \u5b57\u7b26\u4ee3\u7406\u5bf9\u7684\u8fc7\u7a0b\u3002\u5728 Python3 \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 \\u \u548c \\U \u8f6c\u4e49\u5e8f\u5217\u6765\u8868\u793a Unicode \u5b57\u7b26\u3002\u4f8b\u5982\uff0c\\u \u7684\u683c\u5f0f\u662f \\uXXXX \uff0c\u5176\u4e2d XXXX \u662f\u4e00\u4e2a\u56db\u4f4d\u7684\u5341\u516d\u8fdb\u5236\u6570\uff0c\u8868\u793a\u4e00\u4e2a Unicode \u5b57\u7b26\u7684\u4ee3\u7406\u5bf9\u7684\u524d\u534a\u90e8\u5206\u3002\u800c \\U \u7684\u683c\u5f0f\u662f \\UXXXXXXXX \uff0c\u5176\u4e2d XXXXXXXX \u662f\u4e00\u4e2a\u516b\u4f4d\u7684\u5341\u516d\u8fdb\u5236\u6570\uff0c\u8868\u793a\u4e00\u4e2a Unicode \u5b57\u7b26\u7684\u4ee3\u7406\u5bf9\u7684\u5b8c\u6574\u5185\u5bb9\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728 Python3 \u7f16\u7a0b\u4e2d\u5904\u7406 Unicode \u5b57\u7b26\u7684\u4ee3\u7406\u5bf9\uff1f<\/strong><\/p>\n<p>\u5728 Python3 \u4e2d\uff0c\u5982\u679c\u4f60\u8981\u5904\u7406\u4e00\u4e2a\u5305\u542b\u4ee3\u7406\u5bf9\u7684\u5b8c\u6574 Unicode \u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 encode() \u65b9\u6cd5\u5c06\u5176\u7f16\u7801\u4e3a\u6307\u5b9a\u7684\u7f16\u7801\u5f62\u5f0f\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 &quot;UTF-8&quot; \u7f16\u7801\u5c06\u5176\u7f16\u7801\u4e3a\u5b57\u8282\u5e8f\u5217\u3002\u7136\u540e\u4f7f\u7528 decode() \u65b9\u6cd5\u5c06\u5b57\u8282\u5e8f\u5217\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u65f6\uff0cPython3 \u4f1a\u81ea\u52a8\u5c06\u4ee3\u7406\u5bf9\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684 Unicode \u5b57\u7b26\u3002<\/p>\n<p><strong>3. \u5728 Python3 \u7f16\u7a0b\u4e2d\uff0c\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u662f\u5426\u4e3a Unicode \u4ee3\u7406\u7f16\u7801\uff1f<\/strong><\/p>\n<p>\u8981\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u662f\u5426\u4e3a Unicode \u4ee3\u7406\u7f16\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528 Python3 \u5185\u7f6e\u7684\u51fd\u6570 ord() \u6765\u83b7\u53d6\u8be5\u5b57\u7b26\u7684 Unicode \u7f16\u7801\u3002\u5982\u679c\u8be5\u5b57\u7b26\u7684\u7f16\u7801\u5728\u8303\u56f4 0xD800-0xDFFF \u4e4b\u95f4\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u4e00\u4e2a\u4ee3\u7406\u7f16\u7801\u3002\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u6761\u4ef6\u8bed\u53e5\u6765\u5224\u65ad\u5b57\u7b26\u662f\u5426\u6ee1\u8db3\u8be5\u6761\u4ef6\uff0c\u4ee5\u5b9e\u73b0\u5224\u65ad Unicode \u5b57\u7b26\u662f\u5426\u4e3a\u4ee3\u7406\u7f16\u7801\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python3\u4e2d\u5b9e\u73b0Unicode\u4ee3\u7406\u7f16\u7a0b\u4e3b\u8981\u6d89\u53ca\u5230Unicode\u5b57\u7b26\u4e32\u5904\u7406\u3001\u7406\u89e3\u4ee3\u7406\u5bf9\u3001\u4ee5\u53ca\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\u3002 [&hellip;]","protected":false},"author":3,"featured_media":305613,"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\/305592"}],"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=305592"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/305592\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/305613"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=305592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=305592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=305592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}