{"id":1001888,"date":"2024-12-27T10:04:08","date_gmt":"2024-12-27T02:04:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1001888.html"},"modified":"2024-12-27T10:04:11","modified_gmt":"2024-12-27T02:04:11","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e6%b1%89%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1001888.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8868\u793a\u6c49\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075901\/b7d353e6-4942-47b3-a4db-7d8e1cc3fa72.webp\" alt=\"python\u4e2d\u5982\u4f55\u8868\u793a\u6c49\u5b57\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8868\u793a\u6c49\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Unicode\u7f16\u7801\u3001UTF-8\u7f16\u7801\u3001\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u4ee5\u53ca\u5229\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u6765\u5904\u7406\u6c49\u5b57\u3002<\/strong> \u5176\u4e2d\uff0c<strong>Unicode\u7f16\u7801\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f<\/strong>\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u8868\u793a\u51e0\u4e4e\u6240\u6709\u7684\u5b57\u7b26\u96c6\uff0c\u5305\u62ec\u6c49\u5b57\u3002\u8fd9\u662f\u7531\u4e8ePython\u7684str\u7c7b\u578b\u9ed8\u8ba4\u662fUnicode\u7f16\u7801\u7684\uff0c\u5141\u8bb8\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u6c49\u5b57\u5b57\u7b26\uff0c\u907f\u514d\u4e86\u7f16\u7801\u95ee\u9898\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001UNICODE\u7f16\u7801<\/p>\n<\/p>\n<p><p>Unicode\u662f\u4e00\u79cd\u5b57\u7b26\u7f16\u7801\u6807\u51c6\uff0c\u80fd\u591f\u8868\u793a\u5168\u7403\u8303\u56f4\u5185\u7684\u6587\u5b57\u548c\u7b26\u53f7\u3002Python\u7684str\u7c7b\u578b\u9ed8\u8ba4\u4f7f\u7528Unicode\u7f16\u7801\uff0c\u8fd9\u4f7f\u5f97\u5728Python\u4e2d\u5904\u7406\u6c49\u5b57\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u76f4\u63a5\u4f7f\u7528Unicode\u5b57\u7b26\u4e32<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a<code>u<\/code>\u524d\u7f00\u6765\u8868\u793aUnicode\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = u&quot;\u6c49\u5b57&quot;<\/p>\n<p>print(s)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5b57\u7b26\u4e32\u662f\u4ee5Unicode\u683c\u5f0f\u5b58\u50a8\u548c\u5904\u7406\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Unicode\u8f6c\u4e49\u5e8f\u5217<\/strong><\/p>\n<\/p>\n<p><p>Unicode\u8f6c\u4e49\u5e8f\u5217\u662f\u4e00\u79cd\u8868\u793a\u5b57\u7b26\u7684\u5341\u516d\u8fdb\u5236\u7f16\u7801\u65b9\u6cd5\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>\\u<\/code>\u540e\u9762\u8ddf\u968f\u56db\u4f4d\u5341\u516d\u8fdb\u5236\u6570\u6765\u8868\u793a\u4e00\u4e2aUnicode\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u6c49\u5b57\u201c\u6c49\u201d\u7684Unicode\u7f16\u7801\u662f<code>\\u6c49<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;\\u6c49\\u5b57&quot;<\/p>\n<p>print(s)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001UTF-8\u7f16\u7801<\/p>\n<\/p>\n<p><p>UTF-8\u662f\u4e00\u79cd\u53ef\u53d8\u957f\u5ea6\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u5f0f\uff0c\u517c\u5bb9ASCII\u7f16\u7801\uff0c\u5e7f\u6cdb\u7528\u4e8e\u7f51\u9875\u548c\u5176\u4ed6\u6587\u6863\u4e2d\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528UTF-8\u7f16\u7801\u6765\u5904\u7406\u6c49\u5b57\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u7801\u548c\u89e3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>encode()<\/code>\u548c<code>decode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5728\u4e0d\u540c\u7f16\u7801\u4e4b\u95f4\u8f6c\u6362\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;\u6c49\u5b57&quot;<\/p>\n<p>s_utf8 = s.encode(&#39;utf-8&#39;)<\/p>\n<p>print(s_utf8)  # \u8f93\u51fa\u4e3a\u5b57\u8282\u7c7b\u578b<\/p>\n<p>s_decoded = s_utf8.decode(&#39;utf-8&#39;)<\/p>\n<p>print(s_decoded)  # \u8f93\u51fa\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528PYTHON\u5185\u7f6e\u51fd\u6570\u548c\u5e93<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\u548c\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u6c49\u5b57\u548c\u5176\u4ed6\u5b57\u7b26\u96c6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528<code>ord()<\/code>\u548c<code>chr()<\/code>\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>ord()<\/code>\u51fd\u6570\u8fd4\u56de\u5b57\u7b26\u7684Unicode\u7801\u70b9\uff0c\u800c<code>chr()<\/code>\u51fd\u6570\u8fd4\u56de\u5bf9\u5e94\u4e8eUnicode\u7801\u70b9\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(ord(&#39;\u6c49&#39;))  # \u8f93\u51fa\u4e3a27721<\/p>\n<p>print(chr(27721))  # \u8f93\u51fa\u4e3a\u6c49<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528<code>unicodedata<\/code>\u5e93<\/strong><\/p>\n<\/p>\n<p><p><code>unicodedata<\/code>\u5e93\u63d0\u4f9b\u4e86\u5bf9Unicode\u5b57\u7b26\u7684\u8be6\u7ec6\u4fe1\u606f\u548c\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u83b7\u53d6\u6c49\u5b57\u7684\u540d\u79f0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unicodedata<\/p>\n<p>print(unicodedata.name(&#39;\u6c49&#39;))  # \u8f93\u51fa\u4e3aCJK UNIFIED IDEOGRAPH-6C49<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6c49\u5b57\u53ef\u4ee5\u4f5c\u4e3a\u666e\u901a\u5b57\u7b26\u4e32\u8fdb\u884c\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5207\u7247\u3001\u8fde\u63a5\u3001\u67e5\u627e\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b57\u7b26\u4e32\u5207\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u50cf\u5904\u7406\u5176\u4ed6\u5b57\u7b26\u4e32\u4e00\u6837\uff0c\u5bf9\u6c49\u5b57\u5b57\u7b26\u4e32\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;\u6211\u7231Python\u7f16\u7a0b&quot;<\/p>\n<p>print(s[1:3])  # \u8f93\u51fa\u4e3a\u7231P<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u7b26\u4e32\u67e5\u627e<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>find()<\/code>\u65b9\u6cd5\u67e5\u627e\u6c49\u5b57\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;\u6211\u7231Python\u7f16\u7a0b&quot;<\/p>\n<p>pos = s.find(&quot;\u7f16\u7a0b&quot;)<\/p>\n<p>print(pos)  # \u8f93\u51fa\u4e3a6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5904\u7406\u6c49\u5b57\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6c49\u5b57\u65f6\uff0c\u7279\u522b\u662f\u5728\u6587\u4ef6\u8bfb\u5199\u548c\u7f51\u7edc\u4f20\u8f93\u4e2d\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u7f16\u7801\u95ee\u9898\uff0c\u4ee5\u907f\u514d\u4e71\u7801\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6587\u4ef6\u8bfb\u5199<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u548c\u5199\u5165\u5305\u542b\u6c49\u5b57\u7684\u6587\u4ef6\u65f6\uff0c\u5e94\u660e\u786e\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as f:<\/p>\n<p>    f.write(&quot;\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u6587\u4ef6\u3002&quot;)<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:<\/p>\n<p>    content = f.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f51\u7edc\u4f20\u8f93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u7f51\u7edc\u4f20\u8f93\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u4f7f\u7528UTF-8\u8fdb\u884c\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>s.connect((&#39;example.com&#39;, 80))<\/p>\n<p>message = &quot;\u8bf7\u6c42\u6570\u636e&quot;.encode(&#39;utf-8&#39;)<\/p>\n<p>s.sendall(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528Unicode\u7f16\u7801\u3001UTF-8\u7f16\u7801\u4ee5\u53caPython\u7684\u5185\u7f6e\u51fd\u6570\u548c\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728Python\u4e2d\u8868\u793a\u548c\u5904\u7406\u6c49\u5b57\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u6d89\u53ca\u6587\u4ef6\u64cd\u4f5c\u548c\u7f51\u7edc\u4f20\u8f93\u65f6\uff0c\u5fc5\u987b\u660e\u786e\u7f16\u7801\u683c\u5f0f\uff0c\u4ee5\u786e\u4fdd\u6c49\u5b57\u80fd\u591f\u6b63\u786e\u5730\u7f16\u7801\u548c\u89e3\u7801\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5728\u5904\u7406\u6c49\u5b57\u65f6\u51fa\u73b0\u4e71\u7801\u95ee\u9898\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u6c49\u5b57\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6c49\u5b57\u53ef\u4ee5\u901a\u8fc7UTF-8\u7f16\u7801\u6765\u5904\u7406\u3002\u786e\u4fdd\u5728\u4ee3\u7801\u6587\u4ef6\u7684\u5f00\u5934\u6dfb\u52a0<code># -*- coding: utf-8 -*-<\/code>\uff0c\u6216\u8005\u5728\u4f7f\u7528Python 3\u65f6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5df2\u652f\u6301Unicode\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6c49\u5b57\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32<code>&quot;\u4f60\u597d&quot;<\/code>\u53ef\u4ee5\u76f4\u63a5\u5199\u6210<code>greeting = &quot;\u4f60\u597d&quot;<\/code>\u3002\u8fd9\u6837\u5c31\u80fd\u6b63\u5e38\u663e\u793a\u548c\u64cd\u4f5c\u6c49\u5b57\u4e86\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u8bfb\u53d6\u548c\u5199\u5165\u5305\u542b\u6c49\u5b57\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u8bfb\u53d6\u548c\u5199\u5165\u5305\u542b\u6c49\u5b57\u7684\u6587\u4ef6\u65f6\uff0c\u52a1\u5fc5\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u6dfb\u52a0<code>encoding=&#39;utf-8&#39;<\/code>\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u8bfb\u53d6\u6587\u4ef6\u65f6\u53ef\u4ee5\u4f7f\u7528<code>with open(&#39;file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:<\/code>\u3002\u5199\u5165\u65f6\u4e5f\u540c\u6837\u8bbe\u7f6e\u7f16\u7801\u65b9\u5f0f\uff0c\u786e\u4fdd\u6c49\u5b57\u6b63\u786e\u5b58\u50a8\u548c\u663e\u793a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u6c49\u5b57\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5bf9\u6c49\u5b57\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u83b7\u53d6\u6c49\u5b57\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u5224\u65ad\u67d0\u4e2a\u6c49\u5b57\u662f\u5426\u5728\u5b57\u7b26\u4e32\u4e2d\uff0c\u6216\u8005\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\u66ff\u6362\u7279\u5b9a\u7684\u6c49\u5b57\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6c49\u5b57\u7684\u5904\u7406\u4e0e\u5176\u4ed6\u5b57\u7b26\u6ca1\u6709\u5dee\u522b\uff0cPython\u4f1a\u81ea\u52a8\u8bc6\u522bUnicode\u5b57\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8868\u793a\u6c49\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Unicode\u7f16\u7801\u3001UTF-8\u7f16\u7801\u3001\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u4ee5\u53ca\u5229\u7528Python\u7684\u5185 [&hellip;]","protected":false},"author":3,"featured_media":1001902,"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\/1001888"}],"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=1001888"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001888\/revisions"}],"predecessor-version":[{"id":1001908,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001888\/revisions\/1001908"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1001902"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1001888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1001888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1001888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}