{"id":1101100,"date":"2025-01-08T15:46:07","date_gmt":"2025-01-08T07:46:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101100.html"},"modified":"2025-01-08T15:46:09","modified_gmt":"2025-01-08T07:46:09","slug":"python%e9%87%8c%e9%9d%a2%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e7%89%b9%e6%ae%8a%e7%ac%a6%e5%8f%b7-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101100.html","title":{"rendered":"python\u91cc\u9762\u5982\u4f55\u8f93\u5165\u7279\u6b8a\u7b26\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064037\/509302e0-1be3-4e8b-9231-888a52470a13.webp\" alt=\"python\u91cc\u9762\u5982\u4f55\u8f93\u5165\u7279\u6b8a\u7b26\u53f7\" \/><\/p>\n<p><p> <strong>\u5728Python\u91cc\u9762\u8f93\u5165\u7279\u6b8a\u7b26\u53f7\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u3001Unicode\u7f16\u7801\u3001\u5185\u7f6e\u7684\u7279\u6b8a\u5b57\u7b26\u51fd\u6570\u3001\u4ee5\u53ca\u4f7f\u7528\u7279\u5b9a\u5e93\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u8f6c\u4e49\u5b57\u7b26\u548cUnicode\u7f16\u7801\u6765\u5b9e\u73b0\u3002<strong>\u8f6c\u4e49\u5b57\u7b26<\/strong>\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u901a\u5e38\u4ee5\u53cd\u659c\u6760\uff08\\\uff09\u5f00\u5934\uff0c\u7528\u4e8e\u8868\u793a\u4e00\u4e9b\u7279\u6b8a\u7684\u5b57\u7b26\u3002<strong>Unicode\u7f16\u7801<\/strong>\u5219\u662f\u4e00\u79cd\u901a\u7528\u7684\u5b57\u7b26\u7f16\u7801\u6807\u51c6\uff0c\u7528\u4e8e\u8868\u793a\u51e0\u4e4e\u6240\u6709\u4e66\u5199\u7cfb\u7edf\u4e2d\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8f6c\u4e49\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u6765\u8f93\u5165\u4e00\u4e9b\u5e38\u89c1\u7684\u7279\u6b8a\u7b26\u53f7\u3002\u4f8b\u5982\uff0c\u6362\u884c\u7b26\uff08\\n\uff09\uff0c\u5236\u8868\u7b26\uff08\\t\uff09\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u8f6c\u4e49\u5b57\u7b26\u53ca\u5176\u542b\u4e49\uff1a<\/p>\n<\/p>\n<ul>\n<li>\\n\uff1a\u6362\u884c\u7b26<\/li>\n<li>\\t\uff1a\u5236\u8868\u7b26<\/li>\n<li>\\\uff1a\u53cd\u659c\u6760<\/li>\n<li>&#39;\uff1a\u5355\u5f15\u53f7<\/li>\n<li>&quot;\uff1a\u53cc\u5f15\u53f7<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello\\nWorld&quot;)  # \u8f93\u51fa Hello \u6362\u884c World<\/p>\n<p>print(&quot;Hello\\tWorld&quot;)  # \u8f93\u51fa Hello \u5236\u8868\u7b26 World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001Unicode\u7f16\u7801<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301Unicode\u7f16\u7801\uff0c\u53ef\u4ee5\u7528\u6765\u8f93\u5165\u548c\u663e\u793a\u51e0\u4e4e\u6240\u6709\u7684\u5b57\u7b26\uff0c\u5305\u62ec\u7279\u6b8a\u7b26\u53f7\u3002Unicode\u5b57\u7b26\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u683c\u5f0f\u8868\u793a\uff1a\\uXXXX\uff0c\u5176\u4e2dXXXX\u662f\u5b57\u7b26\u768416\u8fdb\u5236Unicode\u7f16\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;\\u03B1&quot;)  # \u8f93\u51fa\u5e0c\u814a\u5b57\u6bcd\u03b1<\/p>\n<p>print(&quot;\\u2665&quot;)  # \u8f93\u51fa\u9ed1\u8272\u5fc3\u5f62\u7b26\u53f7\u2665<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93<\/h3>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u4e2d\u6709\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\u548c\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u7279\u6b8a\u7b26\u53f7\u3002\u4f8b\u5982\uff0c<code>chr()<\/code>\u51fd\u6570\u53ef\u4ee5\u6839\u636eUnicode\u7801\u70b9\u8fd4\u56de\u5bf9\u5e94\u7684\u5b57\u7b26\uff0c<code>ord()<\/code>\u51fd\u6570\u5219\u8fd4\u56de\u5b57\u7b26\u7684Unicode\u7801\u70b9\u3002<code>unicodedata<\/code>\u5e93\u53ef\u4ee5\u63d0\u4f9b\u5173\u4e8eUnicode\u5b57\u7b26\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unicodedata<\/p>\n<p>print(chr(945))  # \u8f93\u51fa\u5e0c\u814a\u5b57\u6bcd\u03b1<\/p>\n<p>print(ord(&#39;\u03b1&#39;))  # \u8f93\u51fa945<\/p>\n<p>print(unicodedata.name(&#39;\u03b1&#39;))  # \u8f93\u51faGREEK SMALL LETTER ALPHA<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7279\u5b9a\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u6807\u51c6\u5e93\uff0cPython\u8fd8\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5904\u7406\u7279\u6b8a\u7b26\u53f7\u3002\u4f8b\u5982\uff0c<code>emoji<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u663e\u793a\u548c\u5904\u7406\u8868\u60c5\u7b26\u53f7\uff0c<code>html<\/code>\u5e93\u53ef\u4ee5\u5904\u7406HTML\u7279\u6b8a\u5b57\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import emoji<\/p>\n<p>import html<\/p>\n<p>print(emoji.emojize(&quot;:thumbs_up:&quot;))  # \u8f93\u51fa\ud83d\udc4d<\/p>\n<p>print(html.unescape(&quot;&amp;hearts;&quot;))  # \u8f93\u51fa\u2665<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u7279\u6b8a\u7b26\u53f7\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u6216\u6587\u4ef6\u5185\u5bb9\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u540c\u65f6\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u3001Unicode\u7f16\u7801\u548c\u7279\u5b9a\u5e93\u6765\u786e\u4fdd\u6b63\u786e\u5904\u7406\u6240\u6709\u7279\u6b8a\u7b26\u53f7\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unicodedata<\/p>\n<p>import html<\/p>\n<h2><strong>\u7528\u6237\u8f93\u5165\u5305\u542b\u7279\u6b8a\u7b26\u53f7<\/strong><\/h2>\n<p>user_input = &quot;Hello\\u2665World&quot;<\/p>\n<h2><strong>\u8f6c\u4e49\u5b57\u7b26\u5904\u7406<\/strong><\/h2>\n<p>escaped_input = user_input.replace(&quot;\\u2665&quot;, html.unescape(&quot;&amp;hearts;&quot;))<\/p>\n<h2><strong>\u663e\u793a\u5904\u7406\u540e\u7684\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>print(escaped_input)  # \u8f93\u51faHello\u2665World<\/p>\n<h2><strong>\u83b7\u53d6\u5b57\u7b26\u7684\u8be6\u7ec6\u4fe1\u606f<\/strong><\/h2>\n<p>for char in escaped_input:<\/p>\n<p>    print(f&quot;\u5b57\u7b26: {char}, Unicode\u7801\u70b9: {ord(char)}, \u540d\u79f0: {unicodedata.name(char)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u7279\u6b8a\u7b26\u53f7\u7684\u9700\u6c42\u53ef\u80fd\u51fa\u73b0\u5728\u5404\u79cd\u573a\u666f\u4e2d\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u6e05\u6d17\u548c\u5904\u7406<\/strong>\uff1a\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u5404\u79cd\u7279\u6b8a\u7b26\u53f7\u3002\u4f8b\u5982\uff0c\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u7684\u6587\u672c\u4e2d\u53ef\u80fd\u5305\u542bHTML\u5b9e\u4f53\uff0c\u9700\u8981\u4f7f\u7528<code>html.unescape<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fd\u9645\u5316\u548c\u672c\u5730\u5316<\/strong>\uff1a\u5728\u5f00\u53d1\u652f\u6301\u591a\u8bed\u8a00\u7684\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u9700\u8981\u5904\u7406\u4e0d\u540c\u8bed\u8a00\u4e2d\u7684\u7279\u6b8a\u7b26\u53f7\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u975e\u62c9\u4e01\u5b57\u7b26\u96c6\u7684\u6587\u672c\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528Unicode\u7f16\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6587\u672c\u683c\u5f0f\u5316\u548c\u663e\u793a<\/strong>\uff1a\u5728\u751f\u6210\u548c\u663e\u793a\u6587\u672c\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u6765\u63a7\u5236\u6587\u672c\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u751f\u6210\u5e26\u6709\u6362\u884c\u7b26\u548c\u5236\u8868\u7b26\u7684\u62a5\u8868\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528<code>\\n<\/code>\u548c<code>\\t<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8868\u60c5\u7b26\u53f7\u548c\u56fe\u6807<\/strong>\uff1a\u5728\u804a\u5929\u5e94\u7528\u548c\u793e\u4ea4\u5a92\u4f53\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u8868\u60c5\u7b26\u53f7\u548c\u56fe\u6807\u662f\u5e38\u89c1\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>emoji<\/code>\u5e93\u6765\u663e\u793a\u548c\u5904\u7406\u8868\u60c5\u7b26\u53f7\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8f93\u5165\u548c\u5904\u7406\u7279\u6b8a\u7b26\u53f7\uff0c\u5305\u62ec\u8f6c\u4e49\u5b57\u7b26\u3001Unicode\u7f16\u7801\u3001\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u5404\u79cd\u7279\u6b8a\u7b26\u53f7\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u7075\u6d3b\u5904\u7406\u7279\u6b8a\u7b26\u53f7\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u6b63\u786e\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8f93\u5165\u7279\u6b8a\u7b26\u53f7\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8f93\u5165\u7279\u6b8a\u7b26\u53f7\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u7b26\u8f6c\u4e49\u5e8f\u5217\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u6765\u8f93\u5165\u5982\u6362\u884c\u7b26\uff08\\n\uff09\u3001\u5236\u8868\u7b26\uff08\\t\uff09\u7b49\u3002\u6b64\u5916\uff0cUnicode\u7f16\u7801\u4e5f\u53ef\u4ee5\u7528\u6765\u8f93\u5165\u66f4\u5e7f\u6cdb\u7684\u7279\u6b8a\u7b26\u53f7\uff0c\u6bd4\u5982\u901a\u8fc7 <code>\\u<\/code> \u540e\u63a5\u56db\u4f4d\u5341\u516d\u8fdb\u5236\u6570\u6765\u8868\u793a\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u5fc3\u5f62\u7b26\u53f7\u53ef\u4ee5\u4f7f\u7528 <code>print(&quot;\\u2665&quot;)<\/code>\uff0c\u7ed3\u679c\u4f1a\u663e\u793a\u4e3a\u2764\ufe0f\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u652f\u6301\u76f4\u63a5\u8f93\u5165\u7279\u6b8a\u7b26\u53f7\uff1f<\/strong><br \/>Python\u652f\u6301\u76f4\u63a5\u8f93\u5165\u67d0\u4e9b\u7279\u6b8a\u7b26\u53f7\uff0c\u524d\u63d0\u662f\u4f60\u7684\u6587\u672c\u7f16\u8f91\u5668\u6216IDE\u652f\u6301\u8fd9\u4e9b\u5b57\u7b26\u3002\u53ef\u4ee5\u76f4\u63a5\u590d\u5236\u548c\u7c98\u8d34\u7279\u6b8a\u7b26\u53f7\uff0c\u6216\u8005\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u4f7f\u7528\u5b83\u4eec\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u8f93\u5165\u201c@\u201d\u3001\u201c#\u201d\u3001\u201c$\u201d\u7b49\u7b26\u53f7\uff0cPython\u4f1a\u6b63\u786e\u5904\u7406\u8fd9\u4e9b\u5b57\u7b26\u3002\u5728\u4f7f\u7528\u65f6\uff0c\u8bf7\u786e\u4fdd\u6240\u7528\u7684\u7f16\u7801\u683c\u5f0f\uff08\u5982UTF-8\uff09\u80fd\u591f\u652f\u6301\u4f60\u6240\u8f93\u5165\u7684\u7279\u6b8a\u7b26\u53f7\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8f93\u5165\u7684\u7279\u6b8a\u7b26\u53f7\uff1f<\/strong><br \/>\u5904\u7406\u8f93\u5165\u7684\u7279\u6b8a\u7b26\u53f7\u901a\u5e38\u6d89\u53ca\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u7f16\u7801\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u5982 <code>replace()<\/code> \u6216 <code>translate()<\/code> \u6765\u5904\u7406\u548c\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u6b8a\u7b26\u53f7\u3002\u5bf9\u4e8e\u4ece\u7528\u6237\u8f93\u5165\u6216\u6587\u4ef6\u8bfb\u53d6\u7684\u7279\u6b8a\u7b26\u53f7\uff0c\u786e\u4fdd\u4f7f\u7528\u5408\u9002\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u4ee5\u907f\u514d\u51fa\u73b0\u4e71\u7801\u95ee\u9898\u3002\u4f7f\u7528Python\u7684 <code>encode()<\/code> \u548c <code>decode()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u5728\u4e0d\u540c\u7f16\u7801\u4e4b\u95f4\u8f6c\u6362\uff0c\u786e\u4fdd\u7279\u6b8a\u7b26\u53f7\u80fd\u591f\u88ab\u6b63\u786e\u5904\u7406\u548c\u663e\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u91cc\u9762\u8f93\u5165\u7279\u6b8a\u7b26\u53f7\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u3001Unicode\u7f16\u7801\u3001\u5185\u7f6e\u7684\u7279\u6b8a\u5b57\u7b26\u51fd\u6570\u3001\u4ee5\u53ca\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1101105,"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\/1101100"}],"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=1101100"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101100\/revisions"}],"predecessor-version":[{"id":1101106,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101100\/revisions\/1101106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1101105"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1101100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1101100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1101100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}