{"id":1177367,"date":"2025-01-15T17:57:13","date_gmt":"2025-01-15T09:57:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1177367.html"},"modified":"2025-01-15T17:57:15","modified_gmt":"2025-01-15T09:57:15","slug":"python2%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e6%b1%89%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1177367.html","title":{"rendered":"python2\u5982\u4f55\u6253\u5370\u6c49\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25112315\/72f5402d-67bf-4f3b-8cde-f0ad0d4eeb2a.webp\" alt=\"python2\u5982\u4f55\u6253\u5370\u6c49\u5b57\" \/><\/p>\n<p><p> \u5728Python 2\u4e2d\uff0c\u6253\u5370\u6c49\u5b57\u9700\u8981\u6ce8\u610f\u7f16\u7801\u95ee\u9898\u3002<strong>\u9996\u5148\uff0c\u8981\u786e\u4fdd\u6e90\u4ee3\u7801\u6587\u4ef6\u4fdd\u5b58\u4e3aUTF-8\u7f16\u7801\u3001\u5176\u6b21\uff0c\u8981\u5728\u6587\u4ef6\u5934\u90e8\u58f0\u660e\u7f16\u7801\u3001\u6700\u540e\uff0c\u8981\u5c06\u5b57\u7b26\u4e32\u8fdb\u884c\u89e3\u7801<\/strong>\u3002\u6211\u4eec\u8be6\u7ec6\u5c55\u5f00\u5176\u4e2d\u7684\u6587\u4ef6\u5934\u90e8\u58f0\u660e\u7f16\u7801\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c\u9ed8\u8ba4\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u662fASCII\u7f16\u7801\u7684\u5b57\u8282\u4e32\uff0c\u8fd9\u610f\u5473\u7740\u5982\u679c\u8981\u5904\u7406\u975eASCII\u5b57\u7b26\uff08\u5982\u6c49\u5b57\uff09\uff0c\u5219\u9700\u8981\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3002\u4e3a\u4e86\u907f\u514d\u7f16\u7801\u95ee\u9898\uff0c\u5728\u6587\u4ef6\u5934\u90e8\u4f7f\u7528\u7f16\u7801\u58f0\u660e\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u7684\u5f00\u5934\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u662f\u4e00\u4e2a\u7f16\u7801\u58f0\u660e\uff0c\u544a\u8bc9Python\u89e3\u91ca\u5668\u8fd9\u4e2a\u6587\u4ef6\u662fUTF-8\u7f16\u7801\u7684\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u76f4\u63a5\u5199\u5165\u548c\u5904\u7406\u6c49\u5b57\u5b57\u7b26\u4e86\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8bbe\u7f6e\u6587\u4ef6\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c\u9ed8\u8ba4\u7684\u5b57\u7b26\u7f16\u7801\u662fASCII\uff0c\u8fd9\u6837\u4f1a\u5bfc\u81f4\u5728\u5904\u7406\u975eASCII\u5b57\u7b26\uff08\u5982\u6c49\u5b57\uff09\u65f6\u51fa\u73b0\u95ee\u9898\u3002\u4e3a\u4e86\u80fd\u591f\u6b63\u786e\u5904\u7406\u6c49\u5b57\u5b57\u7b26\uff0c\u9700\u8981\u5728\u6587\u4ef6\u7684\u5934\u90e8\u58f0\u660e\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u544a\u8bc9Python\u89e3\u91ca\u5668\uff0c\u8fd9\u4e2a\u6587\u4ef6\u4f7f\u7528\u7684\u662fUTF-8\u7f16\u7801\u3002\u8fd9\u6837\uff0cPython\u89e3\u91ca\u5668\u5c31\u80fd\u591f\u6b63\u786e\u5730\u8bfb\u53d6\u548c\u89e3\u91ca\u6587\u4ef6\u4e2d\u7684\u6c49\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u58f0\u660e\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u5934\u90e8\u58f0\u660e\u7f16\u7801\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u76f4\u63a5\u4f7f\u7528\u6c49\u5b57\u5b57\u7b26\u4e86\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>print &quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0cPython\u89e3\u91ca\u5668\u5c31\u4f1a\u6b63\u786e\u5730\u5904\u7406\u548c\u6253\u5370\u6c49\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Unicode\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c\u9ed8\u8ba4\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u662fASCII\u7f16\u7801\u7684\u5b57\u8282\u4e32\u3002\u4e3a\u4e86\u6b63\u786e\u5904\u7406\u6c49\u5b57\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u3002\u5728Python 2\u4e2d\uff0cUnicode\u5b57\u7b26\u4e32\u4ee5<code>u<\/code>\u524d\u7f00\u8868\u793a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>print u&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0cPython\u89e3\u91ca\u5668\u5c31\u4f1a\u5c06\u5b57\u7b26\u4e32\u89e3\u91ca\u4e3aUnicode\u5b57\u7b26\u4e32\uff0c\u5e76\u6b63\u786e\u5904\u7406\u548c\u6253\u5370\u6c49\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7f16\u7801\u548c\u89e3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6c49\u5b57\u5b57\u7b26\u65f6\uff0c\u6709\u65f6\u9700\u8981\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3002\u7f16\u7801\u662f\u5c06Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u4e32\uff0c\u89e3\u7801\u662f\u5c06\u5b57\u8282\u4e32\u8f6c\u6362\u4e3aUnicode\u5b57\u7b26\u4e32\u3002\u5e38\u7528\u7684\u7f16\u7801\u65b9\u5f0f\u6709UTF-8\u548cGBK\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>s = u&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/p>\n<p>s_utf8 = s.encode(&#39;utf-8&#39;)<\/p>\n<p>s_gbk = s.encode(&#39;gbk&#39;)<\/p>\n<p>print s_utf8<\/p>\n<p>print s_gbk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u5c06Unicode\u5b57\u7b26\u4e32<code>s<\/code>\u7f16\u7801\u4e3aUTF-8\u548cGBK\u5b57\u8282\u4e32\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528sys\u6a21\u5757\u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8bbe\u7f6ePython\u89e3\u91ca\u5668\u7684\u9ed8\u8ba4\u7f16\u7801\uff0c\u4ee5\u4fbf\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>import sys<\/p>\n<p>reload(sys)<\/p>\n<p>sys.setdefaultencoding(&#39;utf-8&#39;)<\/p>\n<p>print &quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u5bfc\u5165\u5e76\u91cd\u65b0\u52a0\u8f7d<code>sys<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5c06\u9ed8\u8ba4\u7f16\u7801\u8bbe\u7f6e\u4e3aUTF-8\u3002\u8fd9\u6837\uff0c\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u6240\u6709\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u90fd\u4f1a\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u4e5f\u9700\u8981\u6ce8\u610f\u7f16\u7801\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u8bfb\u53d6\u548c\u5199\u5165\u5305\u542b\u6c49\u5b57\u5b57\u7b26\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>import codecs<\/p>\n<h2><strong>\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p>with codecs.open(&#39;test.txt&#39;, &#39;w&#39;, &#39;utf-8&#39;) as f:<\/p>\n<p>    f.write(u&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6<\/strong><\/h2>\n<p>with codecs.open(&#39;test.txt&#39;, &#39;r&#39;, &#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<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>codecs<\/code>\u6a21\u5757\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u6307\u5b9a\u7f16\u7801\u4e3aUTF-8\u3002\u8fd9\u6837\uff0cPython\u89e3\u91ca\u5668\u5c31\u80fd\u591f\u6b63\u786e\u5730\u8bfb\u53d6\u548c\u5199\u5165\u5305\u542b\u6c49\u5b57\u5b57\u7b26\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u7528\u6237\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u4e5f\u9700\u8981\u6ce8\u610f\u7f16\u7801\u95ee\u9898\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>name = raw_input(&quot;\u8bf7\u8f93\u5165\u4f60\u7684\u540d\u5b57: &quot;)<\/p>\n<p>print u&quot;\u4f60\u597d\uff0c{}&quot;.format(name.decode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u4f7f\u7528<code>raw_input<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u7136\u540e\u5c06\u8f93\u5165\u7684\u5b57\u8282\u4e32\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\u6253\u5370\u6c49\u5b57\u5b57\u7b26\u9700\u8981\u6ce8\u610f\u7f16\u7801\u95ee\u9898\u3002\u901a\u8fc7\u5728\u6587\u4ef6\u5934\u90e8\u58f0\u660e\u7f16\u7801\u3001\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u3001\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3001\u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801\u3001\u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c\u4ee5\u53ca\u5904\u7406\u7528\u6237\u8f93\u5165\uff0c\u6211\u4eec\u53ef\u4ee5\u6b63\u786e\u5730\u5904\u7406\u548c\u6253\u5370\u6c49\u5b57\u5b57\u7b26\u3002\u5e0c\u671b\u8fd9\u4e9b\u6280\u5de7\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u6df1\u5165\u63a2\u8ba8Python 2\u7f16\u7801\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u7f16\u7801\u95ee\u9898\u5728Python 2\u4e2d\u7ecf\u5e38\u4f1a\u5f15\u8d77\u56f0\u6270\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u521d\u5b66\u8005\u3002\u4e3a\u4e86\u66f4\u6df1\u5165\u5730\u7406\u89e3\u7f16\u7801\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u4e00\u4e9b\u57fa\u7840\u77e5\u8bc6\u548c\u5e38\u89c1\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f16\u7801\u548c\u89e3\u7801\u7684\u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u7f16\u7801\u662f\u6307\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5b57\u8282\u4e32\u7684\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2aUnicode\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUTF-8\u5b57\u8282\u4e32\u3002\u89e3\u7801\u662f\u6307\u5c06\u5b57\u8282\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u7684\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2aUTF-8\u5b57\u8282\u4e32\u8f6c\u6362\u4e3aUnicode\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662fASCII\u7f16\u7801\u7684\u5b57\u8282\u4e32\uff0c\u800cUnicode\u5b57\u7b26\u4e32\u4ee5<code>u<\/code>\u524d\u7f00\u8868\u793a\u3002\u4e3a\u4e86\u6b63\u786e\u5904\u7406\u6c49\u5b57\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u5728\u9002\u5f53\u7684\u65f6\u5019\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2. \u7f16\u7801\u58f0\u660e\u7684\u91cd\u8981\u6027<\/h4>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u5934\u90e8\u58f0\u660e\u7f16\u7801\u662f\u5904\u7406\u6c49\u5b57\u5b57\u7b26\u7684\u5173\u952e\u6b65\u9aa4\u3002\u5982\u679c\u6ca1\u6709\u58f0\u660e\u7f16\u7801\uff0cPython\u89e3\u91ca\u5668\u4f1a\u9ed8\u8ba4\u4f7f\u7528ASCII\u7f16\u7801\uff0c\u8fd9\u6837\u4f1a\u5bfc\u81f4\u5728\u5904\u7406\u6c49\u5b57\u5b57\u7b26\u65f6\u51fa\u73b0\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u5728\u5904\u7406\u5305\u542b\u6c49\u5b57\u5b57\u7b26\u7684\u6587\u4ef6\u65f6\uff0c\u52a1\u5fc5\u5728\u6587\u4ef6\u5934\u90e8\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\u3002<\/p>\n<\/p>\n<p><h4>3. Unicode\u5b57\u7b26\u4e32\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0cUnicode\u5b57\u7b26\u4e32\u4ee5<code>u<\/code>\u524d\u7f00\u8868\u793a\u3002\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u53ef\u4ee5\u907f\u514d\u7f16\u7801\u95ee\u9898\uff0c\u5e76\u4e14\u80fd\u591f\u6b63\u786e\u5904\u7406\u548c\u6253\u5370\u6c49\u5b57\u5b57\u7b26\u3002\u5728\u5904\u7406\u6c49\u5b57\u5b57\u7b26\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>4. \u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u5728\u5904\u7406\u6c49\u5b57\u5b57\u7b26\u65f6\u975e\u5e38\u91cd\u8981\u3002\u7f16\u7801\u662f\u5c06Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u4e32\uff0c\u89e3\u7801\u662f\u5c06\u5b57\u8282\u4e32\u8f6c\u6362\u4e3aUnicode\u5b57\u7b26\u4e32\u3002\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3001\u7f51\u7edc\u4f20\u8f93\u3001\u7528\u6237\u8f93\u5165\u7b49\u64cd\u4f5c\u65f6\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>5. \u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8bbe\u7f6ePython\u89e3\u91ca\u5668\u7684\u9ed8\u8ba4\u7f16\u7801\uff0c\u4ee5\u4fbf\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002\u901a\u8fc7\u5bfc\u5165\u5e76\u91cd\u65b0\u52a0\u8f7d<code>sys<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5c06\u9ed8\u8ba4\u7f16\u7801\u8bbe\u7f6e\u4e3aUTF-8\uff0c\u53ef\u4ee5\u786e\u4fdd\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u6240\u6709\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u90fd\u4f1a\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h4>6. \u6587\u4ef6\u64cd\u4f5c\u4e2d\u7684\u7f16\u7801\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u4e5f\u9700\u8981\u6ce8\u610f\u7f16\u7801\u95ee\u9898\u3002\u4f7f\u7528<code>codecs<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\uff0c\u8fd9\u6837Python\u89e3\u91ca\u5668\u5c31\u80fd\u591f\u6b63\u786e\u5730\u8bfb\u53d6\u548c\u5199\u5165\u5305\u542b\u6c49\u5b57\u5b57\u7b26\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>7. \u7528\u6237\u8f93\u5165\u7684\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u4e5f\u9700\u8981\u6ce8\u610f\u7f16\u7801\u95ee\u9898\u3002\u4f7f\u7528<code>raw_input<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u540e\uff0c\u901a\u5e38\u9700\u8981\u5c06\u8f93\u5165\u7684\u5b57\u8282\u4e32\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32\uff0c\u8fd9\u6837\u624d\u80fd\u6b63\u786e\u5904\u7406\u548c\u6253\u5370\u6c49\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>8. \u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6c49\u5b57\u5b57\u7b26\u65f6\uff0c\u5e38\u89c1\u7684\u95ee\u9898\u5305\u62ec\u7f16\u7801\u9519\u8bef\u3001\u89e3\u7801\u9519\u8bef\u3001\u5b57\u7b26\u663e\u793a\u9519\u8bef\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7f16\u7801\u9519\u8bef<\/strong>\uff1a\u901a\u5e38\u662f\u7531\u4e8e\u6ca1\u6709\u5728\u6587\u4ef6\u5934\u90e8\u58f0\u660e\u7f16\u7801\u6216\u6ca1\u6709\u8fdb\u884c\u9002\u5f53\u7684\u7f16\u7801\u64cd\u4f5c\u3002\u89e3\u51b3\u65b9\u6848\u662f\u5728\u6587\u4ef6\u5934\u90e8\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8fdb\u884c\u7f16\u7801\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u89e3\u7801\u9519\u8bef<\/strong>\uff1a\u901a\u5e38\u662f\u7531\u4e8e\u6ca1\u6709\u8fdb\u884c\u9002\u5f53\u7684\u89e3\u7801\u64cd\u4f5c\u3002\u89e3\u51b3\u65b9\u6848\u662f\u5728\u9700\u8981\u65f6\u8fdb\u884c\u89e3\u7801\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u5b57\u7b26\u663e\u793a\u9519\u8bef<\/strong>\uff1a\u901a\u5e38\u662f\u7531\u4e8e\u6ca1\u6709\u6b63\u786e\u8bbe\u7f6e\u7f16\u7801\u6216\u6ca1\u6709\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u3002\u89e3\u51b3\u65b9\u6848\u662f\u8bbe\u7f6e\u6b63\u786e\u7684\u7f16\u7801\uff0c\u5e76\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p><h3>\u5341\u3001\u5b9e\u6218\u6848\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5177\u4f53\u7684\u6848\u4f8b\u5206\u6790\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u5728Python 2\u4e2d\u5904\u7406\u6c49\u5b57\u5b57\u7b26\u7684\u6280\u5de7\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5177\u4f53\u7684\u5b9e\u6218\u6848\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u5904\u7406\u548c\u6253\u5370\u6c49\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>\u6848\u4f8b1\uff1a\u5904\u7406\u5305\u542b\u6c49\u5b57\u5b57\u7b26\u7684\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u6c49\u5b57\u5b57\u7b26\u7684\u6587\u672c\u6587\u4ef6\uff0c\u9700\u8981\u8bfb\u53d6\u5176\u4e2d\u7684\u5185\u5bb9\u5e76\u6253\u5370\u51fa\u6765\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>import codecs<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6<\/strong><\/h2>\n<p>with codecs.open(&#39;test.txt&#39;, &#39;r&#39;, &#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<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>codecs<\/code>\u6a21\u5757\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u6307\u5b9a\u7f16\u7801\u4e3aUTF-8\u3002\u8fd9\u6837\uff0cPython\u89e3\u91ca\u5668\u5c31\u80fd\u591f\u6b63\u786e\u5730\u8bfb\u53d6\u548c\u6253\u5370\u6587\u4ef6\u4e2d\u7684\u6c49\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>\u6848\u4f8b2\uff1a\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6c49\u5b57\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6c49\u5b57\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>name = raw_input(&quot;\u8bf7\u8f93\u5165\u4f60\u7684\u540d\u5b57: &quot;)<\/p>\n<p>print u&quot;\u4f60\u597d\uff0c{}&quot;.format(name.decode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u4f7f\u7528<code>raw_input<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u7136\u540e\u5c06\u8f93\u5165\u7684\u5b57\u8282\u4e32\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h4>\u6848\u4f8b3\uff1a\u5904\u7406\u7f51\u7edc\u4f20\u8f93\u4e2d\u7684\u6c49\u5b57\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u6c49\u5b57\u5b57\u7b26\uff0c\u5e76\u5728\u63a5\u6536\u7aef\u6253\u5370\u51fa\u6765\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>import socket<\/p>\n<h2><strong>\u670d\u52a1\u5668\u7aef<\/strong><\/h2>\n<p>def server():<\/p>\n<p>    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>    server_socket.bind((&#39;localhost&#39;, 8080))<\/p>\n<p>    server_socket.listen(1)<\/p>\n<p>    conn, addr = server_socket.accept()<\/p>\n<p>    data = conn.recv(1024)<\/p>\n<p>    print u&quot;\u63a5\u6536\u5230\u7684\u6d88\u606f: {}&quot;.format(data.decode(&#39;utf-8&#39;))<\/p>\n<p>    conn.close()<\/p>\n<h2><strong>\u5ba2\u6237\u7aef<\/strong><\/h2>\n<p>def client():<\/p>\n<p>    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>    client_socket.connect((&#39;localhost&#39;, 8080))<\/p>\n<p>    message = u&quot;\u4f60\u597d\uff0c\u670d\u52a1\u5668&quot;.encode(&#39;utf-8&#39;)<\/p>\n<p>    client_socket.send(message)<\/p>\n<p>    client_socket.close()<\/p>\n<h2><strong>\u8fd0\u884c\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef<\/strong><\/h2>\n<p>import threading<\/p>\n<p>server_thread = threading.Thread(target=server)<\/p>\n<p>server_thread.start()<\/p>\n<p>client_thread = threading.Thread(target=client)<\/p>\n<p>client_thread.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u670d\u52a1\u5668\u7aef\u63a5\u6536\u5230\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6c49\u5b57\u5b57\u7b26\u6d88\u606f\uff0c\u7136\u540e\u5c06\u6d88\u606f\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32\u5e76\u6253\u5370\u51fa\u6765\u3002\u5ba2\u6237\u7aef\u5c06Unicode\u5b57\u7b26\u4e32\u7f16\u7801\u4e3aUTF-8\u5b57\u8282\u4e32\u540e\uff0c\u901a\u8fc7\u7f51\u7edc\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\u5904\u7406\u548c\u6253\u5370\u6c49\u5b57\u5b57\u7b26\u9700\u8981\u6ce8\u610f\u7f16\u7801\u95ee\u9898\u3002\u901a\u8fc7\u5728\u6587\u4ef6\u5934\u90e8\u58f0\u660e\u7f16\u7801\u3001\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u3001\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3001\u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801\u3001\u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c\u4ee5\u53ca\u5904\u7406\u7528\u6237\u8f93\u5165\uff0c\u6211\u4eec\u53ef\u4ee5\u6b63\u786e\u5730\u5904\u7406\u548c\u6253\u5370\u6c49\u5b57\u5b57\u7b26\u3002\u5e0c\u671b\u8fd9\u4e9b\u6280\u5de7\u548c\u5b9e\u6218\u6848\u4f8b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python 2\u4e2d\u5904\u7406\u6c49\u5b57\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python 2\u4e2d\u5904\u7406\u6c49\u5b57\u5b57\u7b26\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u6b63\u786e\u3002\u53ef\u4ee5\u4f7f\u7528<code>u&#39;\u6c49\u5b57&#39;<\/code>\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49Unicode\u5b57\u7b26\u4e32\u3002\u786e\u4fdd\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0<code># -*- coding: utf-8 -*-<\/code>\u6765\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u8fd9\u6837Python\u80fd\u591f\u6b63\u786e\u5730\u8bc6\u522b\u548c\u5904\u7406\u6c49\u5b57\u3002<\/p>\n<p><strong>\u5728Python 2\u4e2d\u5982\u4f55\u8f93\u51fa\u6c49\u5b57\u5230\u63a7\u5236\u53f0\uff1f<\/strong><br \/>\u8981\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u6c49\u5b57\uff0c\u786e\u4fdd\u4f60\u7684\u7ec8\u7aef\u652f\u6301UTF-8\u7f16\u7801\u3002\u5728\u6253\u5370\u6c49\u5b57\u4e4b\u524d\uff0c\u4f7f\u7528<code>print u&#39;\u6c49\u5b57&#39;<\/code>\u7684\u683c\u5f0f\u6765\u8f93\u51faUnicode\u5b57\u7b26\u4e32\u3002\u5982\u679c\u7ec8\u7aef\u8bbe\u7f6e\u6b63\u786e\uff0c\u5c06\u4f1a\u6b63\u786e\u663e\u793a\u6c49\u5b57\u3002<\/p>\n<p><strong>\u5982\u679c\u9047\u5230\u6c49\u5b57\u6253\u5370\u9519\u8bef\uff0c\u5e94\u8be5\u5982\u4f55\u8c03\u8bd5\uff1f<\/strong><br \/>\u5982\u679c\u6253\u5370\u6c49\u5b57\u65f6\u51fa\u73b0\u4e71\u7801\uff0c\u53ef\u4ee5\u68c0\u67e5\u51e0\u4e2a\u65b9\u9762\uff1a\u786e\u8ba4\u6e90\u6587\u4ef6\u7f16\u7801\u4e3aUTF-8\uff0c\u786e\u4fdd\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\uff08\u524d\u7f00\u4e3au\uff09\uff0c\u4ee5\u53ca\u68c0\u67e5\u63a7\u5236\u53f0\u6216IDE\u7684\u7f16\u7801\u8bbe\u7f6e\u662f\u5426\u652f\u6301UTF-8\u3002\u5982\u679c\u95ee\u9898\u4f9d\u65e7\u5b58\u5728\uff0c\u8003\u8651\u5728\u6253\u5370\u524d\u8fdb\u884c\u7f16\u7801\u8f6c\u6362\uff0c\u4f7f\u7528<code>encode(&#39;utf-8&#39;)<\/code>\u8fdb\u884c\u5b57\u7b26\u7f16\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python 2\u4e2d\uff0c\u6253\u5370\u6c49\u5b57\u9700\u8981\u6ce8\u610f\u7f16\u7801\u95ee\u9898\u3002\u9996\u5148\uff0c\u8981\u786e\u4fdd\u6e90\u4ee3\u7801\u6587\u4ef6\u4fdd\u5b58\u4e3aUTF-8\u7f16\u7801\u3001\u5176\u6b21\uff0c\u8981\u5728\u6587\u4ef6\u5934\u90e8 [&hellip;]","protected":false},"author":3,"featured_media":1177372,"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\/1177367"}],"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=1177367"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177367\/revisions"}],"predecessor-version":[{"id":1177373,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177367\/revisions\/1177373"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1177372"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1177367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1177367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1177367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}