{"id":1177883,"date":"2025-01-15T18:03:31","date_gmt":"2025-01-15T10:03:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1177883.html"},"modified":"2025-01-15T18:03:34","modified_gmt":"2025-01-15T10:03:34","slug":"python2%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1177883.html","title":{"rendered":"python2\u5982\u4f55\u8f93\u51fa\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25112653\/6cd8ae73-ea76-4658-a998-651e561bb6c8.webp\" alt=\"python2\u5982\u4f55\u8f93\u51fa\u4e2d\u6587\" \/><\/p>\n<p><p> <strong>Python2\u5982\u4f55\u8f93\u51fa\u4e2d\u6587\uff1a\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u3001\u6307\u5b9a\u8f93\u51fa\u7f16\u7801\u3001\u786e\u4fdd\u6e90\u6587\u4ef6\u7f16\u7801\u6b63\u786e\u3002<\/strong> \u5728Python2\u4e2d\u8f93\u51fa\u4e2d\u6587\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u7f16\u7801\u95ee\u9898\uff0c\u56e0\u4e3aPython2\u9ed8\u8ba4\u4f7f\u7528ASCII\u7f16\u7801\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u6765\u8f93\u51fa\u4e2d\u6587\u3002<\/p>\n<\/p>\n<p><p>\u5728Python2\u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u3002\u5728Python2\u4e2d\uff0c\u666e\u901a\u5b57\u7b26\u4e32\u662f\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u5b58\u50a8\u7684\uff0c\u800cUnicode\u5b57\u7b26\u4e32\u662f\u4ee5\u5b57\u7b26\u4e3a\u5355\u4f4d\u5b58\u50a8\u7684\u3002\u8981\u8f93\u51fa\u4e2d\u6587\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5b57\u7b26\u4e32\u662fUnicode\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u9762\u52a0\u4e0a<code>u<\/code>\u6765\u58f0\u660eUnicode\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1a<code>u&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/code>\u3002\u63a5\u4e0b\u6765\uff0c\u786e\u4fdd\u5728\u8f93\u51fa\u7684\u65f6\u5019\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\uff0c\u6bd4\u5982\u4f7f\u7528<code>sys.stdout.encoding<\/code>\u6765\u6307\u5b9a\u8f93\u51fa\u7f16\u7801\u3002\u6700\u540e\uff0c\u8fd8\u9700\u8981\u786e\u4fdd\u6e90\u6587\u4ef6\u7684\u7f16\u7801\u662fUTF-8\uff0c\u5e76\u5728\u6587\u4ef6\u5f00\u5934\u52a0\u4e0a<code># -*- coding: utf-8 -*-<\/code>\u58f0\u660e\u6587\u4ef6\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u5728Python2\u4e2d\u6b63\u786e\u8f93\u51fa\u4e2d\u6587\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Unicode\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u58f0\u660eUnicode\u5b57\u7b26\u4e32<\/strong>\uff1a\u5728Python2\u4e2d\uff0c\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662f\u5b57\u8282\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u8981\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\uff0c\u9700\u8981\u5728\u5b57\u7b26\u4e32\u524d\u9762\u52a0\u4e0a<code>u<\/code>\u3002<\/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>print s<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u7b26\u7f16\u7801\u8f6c\u6362<\/strong>\uff1a\u6709\u65f6\u5019\u9700\u8981\u5c06Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u7279\u5b9a\u7f16\u7801\u7684\u5b57\u8282\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>encode<\/code>\u65b9\u6cd5\u3002<\/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>print s.encode(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u6307\u5b9a\u8f93\u51fa\u7f16\u7801<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4fee\u6539sys.stdout\u7684\u7f16\u7801<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>sys.stdout<\/code>\u7684\u7f16\u7801\u6765\u786e\u4fdd\u8f93\u51fa\u6b63\u786e\u7f16\u7801\u7684\u5b57\u7b26\u3002<\/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>s = u&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/p>\n<p>print s<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u7f16\u7801<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u786e\u4fdd\u6b63\u786e\u7684\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PYTHONIOENCODING=utf-8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u786e\u4fdd\u6e90\u6587\u4ef6\u7f16\u7801\u6b63\u786e<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u58f0\u660e\u6587\u4ef6\u7f16\u7801<\/strong>\uff1a\u5728Python\u6e90\u6587\u4ef6\u7684\u5f00\u5934\u52a0\u4e0a\u7f16\u7801\u58f0\u660e\uff0c\u786e\u4fdd\u6e90\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/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>print s<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u5408\u9002\u7684\u7f16\u8f91\u5668<\/strong>\uff1a\u786e\u4fdd\u4f7f\u7528\u652f\u6301UTF-8\u7f16\u7801\u7684\u7f16\u8f91\u5668\u4fdd\u5b58\u60a8\u7684Python\u6e90\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python2\u4e2d\u6b63\u786e\u8f93\u51fa\u4e2d\u6587\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u6bcf\u4e00\u4e2a\u6b65\u9aa4\u53ca\u5176\u4ed6\u76f8\u5173\u7684\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Unicode\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728Python2\u4e2d\uff0cUnicode\u5b57\u7b26\u4e32\u662f\u4ee5\u5b57\u7b26\u4e3a\u5355\u4f4d\u5b58\u50a8\u7684\uff0c\u800c\u666e\u901a\u5b57\u7b26\u4e32\uff08str\uff09\u662f\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u5b58\u50a8\u7684\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u8981\u5904\u7406\u975eASCII\u5b57\u7b26\uff08\u5982\u4e2d\u6587\uff09\uff0c\u5fc5\u987b\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u58f0\u660eUnicode\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u58f0\u660e\u4e00\u4e2aUnicode\u5b57\u7b26\u4e32\uff0c\u53ea\u9700\u8981\u5728\u5b57\u7b26\u4e32\u524d\u9762\u52a0\u4e0a<code>u<\/code>\uff0c\u5982\u4e0b\u6240\u793a\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>print s<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u58f0\u660e\u7684\u5b57\u7b26\u4e32<code>u&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/code>\u662f\u4e00\u4e2aUnicode\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b57\u7b26\u7f16\u7801\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u9700\u8981\u5c06Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u7279\u5b9a\u7f16\u7801\u7684\u5b57\u8282\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>encode<\/code>\u65b9\u6cd5\u3002\u5e38\u89c1\u7684\u7f16\u7801\u6709<code>utf-8<\/code>\u3001<code>gbk<\/code>\u7b49\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>print s.encode(&#39;utf-8&#39;)  # \u8f93\u51faUTF-8\u7f16\u7801\u7684\u5b57\u8282\u5b57\u7b26\u4e32<\/p>\n<p>print s.encode(&#39;gbk&#39;)    # \u8f93\u51faGBK\u7f16\u7801\u7684\u5b57\u8282\u5b57\u7b26\u4e32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6ce8\u610f\uff0c<code>encode<\/code>\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u76f4\u63a5\u8f93\u51fa\u5230\u63a7\u5236\u53f0\uff0c\u9700\u8981\u786e\u4fdd\u63a7\u5236\u53f0\u652f\u6301\u8be5\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6307\u5b9a\u8f93\u51fa\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u76f4\u63a5\u8f93\u51faUnicode\u5b57\u7b26\u4e32\u53ef\u80fd\u4f1a\u5f15\u53d1\u7f16\u7801\u9519\u8bef\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u6307\u5b9a\u8f93\u51fa\u7f16\u7801\uff0c\u786e\u4fdd\u8f93\u51fa\u6b63\u786e\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>1. \u4fee\u6539sys.stdout\u7684\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>sys.stdout<\/code>\u7684\u7f16\u7801\u6765\u786e\u4fdd\u8f93\u51fa\u6b63\u786e\u7f16\u7801\u7684\u5b57\u7b26\u3002\u9996\u5148\u9700\u8981\u5bfc\u5165<code>sys<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4fee\u6539\u7f16\u7801\u8bbe\u7f6e\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>s = u&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/p>\n<p>print s<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>reload(sys)<\/code>\u662f\u4e3a\u4e86\u91cd\u65b0\u52a0\u8f7dsys\u6a21\u5757\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801\u3002<code>sys.setdefaultencoding(&#39;utf-8&#39;)<\/code>\u5c06\u9ed8\u8ba4\u7f16\u7801\u8bbe\u7f6e\u4e3aUTF-8\uff0c\u786e\u4fdd\u8f93\u51fa\u65f6\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h4>2. \u901a\u8fc7\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u786e\u4fdd\u6b63\u786e\u7684\u7f16\u7801\u8f93\u51fa\u3002\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PYTHONIOENCODING=utf-8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u8bbe\u7f6e\u540e\uff0cPython\u4f1a\u4f7f\u7528UTF-8\u7f16\u7801\u8f93\u51fa\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u786e\u4fdd\u6e90\u6587\u4ef6\u7f16\u7801\u6b63\u786e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u6e90\u6587\u4ef6\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\uff0c\u9700\u8981\u786e\u4fdd\u6e90\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u5e76\u5728\u6587\u4ef6\u5f00\u5934\u52a0\u4e0a\u7f16\u7801\u58f0\u660e\u3002<\/p>\n<\/p>\n<p><h4>1. \u58f0\u660e\u6587\u4ef6\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u5728Python\u6e90\u6587\u4ef6\u7684\u5f00\u5934\u52a0\u4e0a\u7f16\u7801\u58f0\u660e\uff0c\u786e\u4fdd\u6e90\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\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>print s<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u7684\u7f16\u7801\u58f0\u660e\u544a\u8bc9Python\u89e3\u91ca\u5668\u8be5\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u80fd\u591f\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5408\u9002\u7684\u7f16\u8f91\u5668<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u4f7f\u7528\u652f\u6301UTF-8\u7f16\u7801\u7684\u7f16\u8f91\u5668\u4fdd\u5b58\u60a8\u7684Python\u6e90\u6587\u4ef6\u3002\u5927\u591a\u6570\u73b0\u4ee3\u7f16\u8f91\u5668\uff08\u5982VSCode\u3001Sublime Text\u3001PyCharm\u7b49\uff09\u90fd\u652f\u6301UTF-8\u7f16\u7801\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u4fdd\u5b58\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1. \u6587\u4ef6\u8bfb\u5199<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u6587\u4ef6\u8bfb\u5199\u65f6\uff0c\u4e5f\u9700\u8981\u6ce8\u610f\u7f16\u7801\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u8bfb\u53d6\u4e00\u4e2a\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>with open(&#39;chinese.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<p><p>\u5199\u5165\u6587\u4ef6\u65f6\u540c\u6837\u9700\u8981\u6307\u5b9a\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as f:<\/p>\n<p>    s = u&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/p>\n<p>    f.write(s)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5185\u5bb9\u6b63\u786e\u7f16\u7801\u548c\u89e3\u7801\u3002<\/p>\n<\/p>\n<p><h4>2. \u6570\u636e\u5e93\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u65f6\uff0c\u4e5f\u9700\u8981\u6ce8\u610f\u7f16\u7801\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u4f7f\u7528MySQL\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u5728\u8fde\u63a5\u65f6\u6307\u5b9a\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import MySQLdb<\/p>\n<p>conn = MySQLdb.connect(host=&#39;localhost&#39;, user=&#39;user&#39;, passwd=&#39;passwd&#39;, db=&#39;dbname&#39;, charset=&#39;utf8&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>cursor.execute(&#39;SELECT * FROM table&#39;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>for row in rows:<\/p>\n<p>    print row<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u4f7f\u7528<code>charset=&#39;utf8&#39;<\/code>\u53c2\u6570\uff0c\u4ee5\u4fbf\u6570\u636e\u5e93\u8fde\u63a5\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h4>3. \u7f51\u7edc\u4f20\u8f93<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u7f51\u7edc\u4f20\u8f93\u65f6\uff0c\u5982\u901a\u8fc7HTTP\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\uff0c\u4e5f\u9700\u8981\u786e\u4fdd\u6b63\u786e\u5904\u7406\u7f16\u7801\u3002\u4f7f\u7528requests\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u5934\u6765\u6307\u5b9a\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>headers = {&#39;Content-Type&#39;: &#39;application\/json; charset=utf-8&#39;}<\/p>\n<p>response = requests.post(&#39;http:\/\/example.com\/api&#39;, json={&#39;message&#39;: u&#39;\u4f60\u597d\uff0c\u4e16\u754c&#39;}, headers=headers)<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u53d1\u9001\u548c\u63a5\u6536\u7684\u6570\u636e\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python2\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u7f16\u7801\u95ee\u9898\u3002\u901a\u8fc7\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u3001\u6307\u5b9a\u8f93\u51fa\u7f16\u7801\u3001\u786e\u4fdd\u6e90\u6587\u4ef6\u7f16\u7801\u6b63\u786e\uff0c\u53ef\u4ee5\u6b63\u786e\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\u3002\u6b64\u5916\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u6587\u4ef6\u8bfb\u5199\u3001\u6570\u636e\u5e93\u64cd\u4f5c\u548c\u7f51\u7edc\u4f20\u8f93\u4e2d\u7684\u7f16\u7801\u95ee\u9898\u3002\u5c3d\u7ba1Python2\u9ed8\u8ba4\u4f7f\u7528ASCII\u7f16\u7801\uff0c\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u5728Python2\u4e2d\u6b63\u786e\u5904\u7406\u548c\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u5904\u7406Unicode\u548c\u7f16\u7801\u95ee\u9898\uff0c\u5efa\u8bae\u5c3d\u91cf\u4f7f\u7528Python3\uff0c\u56e0\u4e3aPython3\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u5e76\u4e14\u5bf9Unicode\u7684\u652f\u6301\u66f4\u52a0\u5b8c\u5584\u3002Python3\u4e2d\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u9ed8\u8ba4\u662fUnicode\u5b57\u7b26\u4e32\uff0c\u907f\u514d\u4e86\u8bb8\u591a\u7f16\u7801\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2aPython3\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>s = &quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/p>\n<p>print(s)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python3\u4e2d\uff0c\u65e0\u9700\u663e\u5f0f\u58f0\u660eUnicode\u5b57\u7b26\u4e32\uff0c\u9ed8\u8ba4\u5b57\u7b26\u4e32\u7c7b\u578b\u5c31\u662fUnicode\u5b57\u7b26\u4e32\uff0c\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u65f6\uff0c\u5e38\u5e38\u9700\u8981\u8fdb\u884c\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u683c\u5f0f\u5316\u3002\u5728Python2\u4e2d\uff0c\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u8fdb\u884c\u64cd\u4f5c\u548c\u683c\u5f0f\u5316\uff0c\u53ef\u4ee5\u907f\u514d\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b57\u7b26\u4e32\u8fde\u63a5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u8fdb\u884c\u5b57\u7b26\u4e32\u8fde\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>s1 = u&quot;\u4f60\u597d&quot;<\/p>\n<p>s2 = u&quot;\u4e16\u754c&quot;<\/p>\n<p>s = s1 + u&quot;\uff0c&quot; + s2<\/p>\n<p>print s<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u8fde\u63a5\u540e\u7684\u5b57\u7b26\u4e32\u4ecd\u7136\u662fUnicode\u5b57\u7b26\u4e32\uff0c\u80fd\u591f\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>%<\/code>\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e32\u548c\u53d8\u91cf\u90fd\u662fUnicode\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>name = u&quot;\u4e16\u754c&quot;<\/p>\n<p>s = u&quot;\u4f60\u597d\uff0c%s&quot; % name<\/p>\n<p>print s<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u4f7f\u7528<code>format<\/code>\u65b9\u6cd5\u65f6\u4e5f\u9700\u8981\u786e\u4fdd\u5b57\u7b26\u4e32\u548c\u53d8\u91cf\u90fd\u662fUnicode\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>name = u&quot;\u4e16\u754c&quot;<\/p>\n<p>s = u&quot;\u4f60\u597d\uff0c{}&quot;.format(name)<\/p>\n<p>print s<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u683c\u5f0f\u5316\u540e\u7684\u5b57\u7b26\u4e32\u80fd\u591f\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u65f6\uff0c\u6709\u65f6\u9700\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002Python2\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757<code>re<\/code>\u652f\u6301Unicode\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>1. \u5339\u914d\u4e2d\u6587\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e2d\u6587\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>import re<\/p>\n<p>pattern = re.compile(ur&#39;[\\u4e00-\\u9fa5]+&#39;)<\/p>\n<p>text = u&quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01Hello, world!&quot;<\/p>\n<p>matches = pattern.findall(text)<\/p>\n<p>print matches<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u5339\u914d\u6587\u672c\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\uff0c\u5e76\u8fd4\u56de\u5339\u914d\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2. \u66ff\u6362\u4e2d\u6587\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u4e2d\u6587\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>import re<\/p>\n<p>pattern = re.compile(ur&#39;\u4f60\u597d&#39;)<\/p>\n<p>text = u&quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01Hello, world!&quot;<\/p>\n<p>result = pattern.sub(u&quot;Hello&quot;, text)<\/p>\n<p>print result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u5c06\u6587\u672c\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u66ff\u6362\u4e3a\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7b2c\u4e09\u65b9\u5e93\u652f\u6301<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u53ef\u80fd\u4f1a\u4f7f\u7528\u5230\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u3002\u5927\u591a\u6570\u7b2c\u4e09\u65b9\u5e93\u90fd\u652f\u6301Unicode\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>1. Requests\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528requests\u5e93\u53d1\u9001\u548c\u63a5\u6536\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684HTTP\u8bf7\u6c42\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>import requests<\/p>\n<p>url = &#39;http:\/\/httpbin.org\/post&#39;<\/p>\n<p>data = {&#39;message&#39;: u&#39;\u4f60\u597d\uff0c\u4e16\u754c&#39;}<\/p>\n<p>response = requests.post(url, json=data)<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u53d1\u9001\u548c\u63a5\u6536\u7684\u6570\u636e\u6b63\u786e\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h4>2. JSON\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528json\u5e93\u5904\u7406\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684JSON\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>import json<\/p>\n<p>data = {&#39;message&#39;: u&#39;\u4f60\u597d\uff0c\u4e16\u754c&#39;}<\/p>\n<p>json_str = json.dumps(data, ensure_ascii=False)<\/p>\n<p>print json_str<\/p>\n<p>parsed_data = json.loads(json_str)<\/p>\n<p>print parsed_data<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e<code>ensure_ascii=False<\/code>\uff0c\u53ef\u4ee5\u786e\u4fdd\u751f\u6210\u7684JSON\u5b57\u7b26\u4e32\u5305\u542b\u6b63\u786e\u7684\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u8c03\u8bd5\u548c\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f16\u7801\u9519\u8bef\u3002\u4e86\u89e3\u5982\u4f55\u8c03\u8bd5\u548c\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5feb\u901f\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1. \u5e38\u89c1\u7f16\u7801\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u5e38\u89c1\u7684\u7f16\u7801\u9519\u8bef\u5305\u62ec<code>UnicodeEncodeError<\/code>\u548c<code>UnicodeDecodeError<\/code>\u3002\u8fd9\u4e9b\u9519\u8bef\u901a\u5e38\u662f\u7531\u4e8e\u7f16\u7801\u548c\u89e3\u7801\u4e0d\u5339\u914d\u5bfc\u81f4\u7684\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>try:<\/p>\n<p>    print s.encode(&#39;ascii&#39;)<\/p>\n<p>except UnicodeEncodeError as e:<\/p>\n<p>    print &quot;\u7f16\u7801\u9519\u8bef\uff1a&quot;, e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u7f16\u7801\u9519\u8bef\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h4>2. \u8c03\u8bd5\u6280\u5de7<\/h4>\n<\/p>\n<p><p>\u5728\u8c03\u8bd5\u7f16\u7801\u95ee\u9898\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6280\u5de7\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u6253\u5370\u53d8\u91cf\u7c7b\u578b<\/strong>\uff1a\u901a\u8fc7\u6253\u5370\u53d8\u91cf\u7c7b\u578b\uff0c\u786e\u8ba4\u53d8\u91cf\u662fUnicode\u5b57\u7b26\u4e32\u8fd8\u662f\u5b57\u8282\u5b57\u7b26\u4e32\u3002<\/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>print type(s)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528repr\u51fd\u6570<\/strong>\uff1a\u4f7f\u7528<code>repr<\/code>\u51fd\u6570\u6253\u5370\u5b57\u7b26\u4e32\u7684\u5185\u90e8\u8868\u793a\uff0c\u67e5\u770b\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u60c5\u51b5\u3002<\/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>print repr(s)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u6587\u4ef6\u7f16\u7801<\/strong>\uff1a\u786e\u4fdd\u6e90\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u5e76\u5728\u6587\u4ef6\u5f00\u5934\u52a0\u4e0a\u7f16\u7801\u58f0\u660e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python2\u4e2d\u6b63\u786e\u5904\u7406\u548c\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\u3002\u5c3d\u7ba1Python2\u9ed8\u8ba4\u4f7f\u7528ASCII\u7f16\u7801\uff0c\u901a\u8fc7\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u3001\u6307\u5b9a\u8f93\u51fa\u7f16\u7801\u3001\u786e\u4fdd\u6e90\u6587\u4ef6\u7f16\u7801\u6b63\u786e\uff0c\u53ef\u4ee5\u907f\u514d\u7f16\u7801\u95ee\u9898\uff0c\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5efa\u8bae\u5c3d\u91cf\u4f7f\u7528Python3\uff0c\u56e0\u4e3aPython3\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u5e76\u4e14\u5bf9Unicode\u7684\u652f\u6301\u66f4\u52a0\u5b8c\u5584\uff0c\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python2\u4e2d\uff0c\u5982\u4f55\u6b63\u786e\u8bbe\u7f6e\u7f16\u7801\u4ee5\u8f93\u51fa\u4e2d\u6587\uff1f<\/strong><br \/>\u5728Python2\u4e2d\uff0c\u9ed8\u8ba4\u7f16\u7801\u662fASCII\u3002\u5982\u679c\u8981\u8f93\u51fa\u4e2d\u6587\uff0c\u5fc5\u987b\u8bbe\u7f6e\u6587\u4ef6\u7f16\u7801\u4e3aUTF-8\u3002\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0<code># -*- coding: utf-8 -*-<\/code>\uff0c\u7136\u540e\u5728\u8f93\u51fa\u4e2d\u6587\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982<code>print u&quot;\u4f60\u597d&quot;<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u6b63\u786e\u663e\u793a\u3002<\/p>\n<p><strong>\u4f7f\u7528Python2\u8f93\u51fa\u4e2d\u6587\u65f6\u9047\u5230\u4e71\u7801\uff0c\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u4e71\u7801\u901a\u5e38\u662f\u56e0\u4e3a\u5b57\u7b26\u7f16\u7801\u4e0d\u5339\u914d\u5bfc\u81f4\u7684\u3002\u5728\u8f93\u51fa\u4e2d\u6587\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684\u7ec8\u7aef\u6216IDE\u652f\u6301UTF-8\u7f16\u7801\u3002\u4f7f\u7528<code>sys.setdefaultencoding(&#39;utf-8&#39;)<\/code>\u6765\u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801\u4e5f\u53ef\u4ee5\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5efa\u8bae\u5728\u6bcf\u6b21\u8f93\u51fa\u524d\u8f6c\u6362\u5b57\u7b26\u4e32\u7f16\u7801\uff0c\u4f8b\u5982\u4f7f\u7528<code>str.encode(&#39;utf-8&#39;)<\/code>\u3002<\/p>\n<p><strong>Python2\u4e2d\uff0c\u5982\u4f55\u4ece\u6587\u4ef6\u8bfb\u53d6\u4e2d\u6587\u5e76\u6b63\u786e\u8f93\u51fa\uff1f<\/strong><br \/>\u8bfb\u53d6\u4e2d\u6587\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>codecs<\/code>\u6a21\u5757\u4ee5\u6307\u5b9a\u7f16\u7801\u8bfb\u53d6\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>import codecs<\/code>\u548c<code>codecs.open(&#39;file.txt&#39;, &#39;r&#39;, &#39;utf-8&#39;)<\/code>\u6765\u6253\u5f00\u6587\u4ef6\u3002\u8bfb\u53d6\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u6253\u5370\u6216\u5904\u7406\u8fd9\u4e9b\u5b57\u7b26\u4e32\uff0c\u786e\u4fdd\u5728\u8f93\u51fa\u65f6\u4f7f\u7528<code>print u&quot;\u4e2d\u6587\u5185\u5bb9&quot;<\/code>\u7684\u683c\u5f0f\u6765\u907f\u514d\u4e71\u7801\u3002<\/p>\n<p><strong>\u5728Python2\u4e2d\uff0c\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u4e2d\u6587\u8f93\u51fa\uff1f<\/strong><br \/>\u9664\u4e86\u5185\u7f6e\u7684<code>codecs<\/code>\u6a21\u5757\uff0c\u8fd8\u6709<code>io<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u6765\u5904\u7406\u4e2d\u6587\u8f93\u51fa\u3002\u4f7f\u7528<code>io.open()<\/code>\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\uff0c\u4ece\u800c\u7b80\u5316\u4e2d\u6587\u5904\u7406\u3002\u6b64\u5916\uff0c<code>chardet<\/code>\u5e93\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u786e\u4fdd\u8bfb\u53d6\u4e2d\u6587\u6587\u4ef6\u65f6\u4e0d\u4f1a\u51fa\u73b0\u7f16\u7801\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python2\u5982\u4f55\u8f93\u51fa\u4e2d\u6587\uff1a\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u3001\u6307\u5b9a\u8f93\u51fa\u7f16\u7801\u3001\u786e\u4fdd\u6e90\u6587\u4ef6\u7f16\u7801\u6b63\u786e\u3002 \u5728Python2\u4e2d\u8f93 [&hellip;]","protected":false},"author":3,"featured_media":1177890,"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\/1177883"}],"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=1177883"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177883\/revisions"}],"predecessor-version":[{"id":1177892,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177883\/revisions\/1177892"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1177890"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1177883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1177883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1177883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}