{"id":1112287,"date":"2025-01-08T17:37:01","date_gmt":"2025-01-08T09:37:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1112287.html"},"modified":"2025-01-08T17:37:03","modified_gmt":"2025-01-08T09:37:03","slug":"python3-%e5%bc%80%e5%a4%b4%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%aeutf-8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1112287.html","title":{"rendered":"python3 \u5f00\u5934\u5982\u4f55\u8bbe\u7f6eutf-8"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074402\/babf1225-b7f3-4568-8516-4aafed2e702f.webp\" alt=\"python3 \u5f00\u5934\u5982\u4f55\u8bbe\u7f6eutf-8\" \/><\/p>\n<p><p> <strong>\u5728Python3\u4e2d\u8bbe\u7f6eUTF-8\u7f16\u7801\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\u3001\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u65f6\u6307\u5b9a\u7f16\u7801\u3001\u8bbe\u7f6e\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\u7b49\u3002<\/strong> \u5176\u4e2d\u6700\u5e38\u7528\u548c\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728Python3\u4e2d\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6e90\u4ee3\u7801\u6587\u4ef6\u7684\u7f16\u7801\u662fUTF-8\u3002\u56e0\u6b64\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u4e0d\u9700\u8981\u7279\u522b\u58f0\u660e\u6587\u4ef6\u7684\u7f16\u7801\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u60f3\u660e\u786e\u5730\u6307\u5b9a\u7f16\u7801\uff0c\u6216\u8005\u517c\u5bb9\u4e00\u4e9b\u7279\u6b8a\u73af\u5883\uff0c\u53ef\u4ee5\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6dfb\u52a0\u7f16\u7801\u58f0\u660e<\/h3>\n<\/p>\n<p><p>\u5728Python\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\u6216\u8005\u7b2c\u4e8c\u884c\u6dfb\u52a0\u5982\u4e0b\u7f16\u7801\u58f0\u660e\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\u79cd\u65b9\u5f0f\u660e\u786e\u6307\u5b9a\u4e86\u6587\u4ef6\u7684\u7f16\u7801\u4e3aUTF-8\u3002\u867d\u7136Python3\u9ed8\u8ba4\u4f7f\u7528UTF-8\uff0c\u4f46\u6dfb\u52a0\u8fd9\u4e00\u884c\u53ef\u4ee5\u786e\u4fdd\u5728\u4efb\u4f55\u73af\u5883\u4e0b\u90fd\u6b63\u786e\u89e3\u91ca\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>open<\/code> \u51fd\u6570\u65f6\u6307\u5b9a\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>open<\/code> \u51fd\u6570\u7684 <code>encoding<\/code> \u53c2\u6570\u6765\u6307\u5b9a\u7f16\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u907f\u514d\u4e86\u7f16\u7801\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbe\u7f6e\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4e0d\u63a8\u8350\uff0c\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\u6765\u786e\u4fdd\u6240\u6709\u64cd\u4f5c\u90fd\u4f7f\u7528UTF-8\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u5e26\u6765\u4e0d\u53ef\u9884\u89c1\u7684\u95ee\u9898\uff0c\u56e0\u6b64\u5e94\u8c28\u614e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.setdefaultencoding(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u5728Python2\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u4f46\u5728Python3\u4e2d\u5df2\u88ab\u5f03\u7528\uff0c\u56e0\u6b64\u4e0d\u63a8\u8350\u5728Python3\u4e2d\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8be6\u7ec6\u8bf4\u660e\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\u7684\u597d\u5904<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\u6709\u4ee5\u4e0b\u51e0\u4e2a\u597d\u5904\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u517c\u5bb9\u6027<\/strong>\uff1a\u867d\u7136Python3\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4f46\u5728\u4e0d\u540c\u7684\u7f16\u8f91\u5668\u6216\u7cfb\u7edf\u73af\u5883\u4e0b\uff0c\u660e\u786e\u58f0\u660e\u7f16\u7801\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u7f16\u7801\u95ee\u9898\u3002<\/li>\n<li><strong>\u53ef\u8bfb\u6027<\/strong>\uff1a\u5bf9\u4e8e\u5176\u4ed6\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u660e\u786e\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u6709\u52a9\u4e8e\u7406\u89e3\u4ee3\u7801\uff0c\u7279\u522b\u662f\u5728\u5305\u542b\u975eASCII\u5b57\u7b26\u65f6\u3002<\/li>\n<li><strong>\u89c4\u8303\u6027<\/strong>\uff1a\u5728\u56e2\u961f\u5f00\u53d1\u4e2d\uff0c\u9075\u5faa\u7f16\u7801\u58f0\u660e\u7684\u89c4\u8303\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u4eba\u4f7f\u7528\u7edf\u4e00\u7684\u7f16\u7801\u6807\u51c6\uff0c\u51cf\u5c11\u7f16\u7801\u51b2\u7a81\u7684\u98ce\u9669\u3002<\/li>\n<\/ol>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684Python\u6587\u4ef6\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\u7f16\u7801\uff0c\u53ef\u80fd\u4f1a\u5728\u67d0\u4e9b\u73af\u5883\u4e0b\u51fa\u73b0\u4e71\u7801\u6216\u8005\u7f16\u7801\u9519\u8bef\u3002\u901a\u8fc7\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\uff0c\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4efb\u4f55\u73af\u5883\u4e0b\u90fd\u80fd\u6b63\u786e\u89e3\u91ca\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>print(&quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u65f6\uff0c\u5728\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u65f6\u4e5f\u53ef\u4ee5\u6307\u5b9a\u7f16\u7801\uff0c\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u7684\u7edf\u4e00\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<h2><strong>\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(&quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fddPython\u6587\u4ef6\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u7f16\u7801\u4e00\u81f4\u6027\uff0c\u907f\u514d\u7f16\u7801\u95ee\u9898\u7684\u53d1\u751f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7f16\u7801\u95ee\u9898\u7684\u5e38\u89c1\u9519\u8bef\u53ca\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5e38\u89c1\u7684\u7f16\u7801\u9519\u8bef\u5305\u62ec\uff1aUnicodeDecodeError\u3001UnicodeEncodeError\u7b49\u3002\u4e86\u89e3\u8fd9\u4e9b\u9519\u8bef\u7684\u4ea7\u751f\u539f\u56e0\u53ca\u89e3\u51b3\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u7f16\u7801\u5904\u7406\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><p><strong>UnicodeDecodeError<\/strong>\uff1a\u901a\u5e38\u53d1\u751f\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u6587\u4ef6\u5185\u5bb9\u7684\u5b9e\u9645\u7f16\u7801\u4e0e\u6307\u5b9a\u7f16\u7801\u4e0d\u4e00\u81f4\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u786e\u4fdd\u6587\u4ef6\u7684\u7f16\u7801\u4e0e<code>open<\/code>\u51fd\u6570\u7684<code>encoding<\/code>\u53c2\u6570\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><p><strong>UnicodeEncodeError<\/strong>\uff1a\u901a\u5e38\u53d1\u751f\u5728\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u4e0e\u76ee\u6807\u6587\u4ef6\u7684\u7f16\u7801\u4e0d\u4e00\u81f4\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u786e\u4fdd\u5b57\u7b26\u4e32\u7f16\u7801\u4e0e\u76ee\u6807\u6587\u4ef6\u7684\u7f16\u7801\u4e00\u81f4\uff0c\u6216\u8005\u5728\u5199\u5165\u524d\u8fdb\u884c\u7f16\u7801\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u5e94\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python3\u4e2d\u6709\u6548\u5730\u5904\u7406UTF-8\u7f16\u7801\u95ee\u9898\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python 3\u4e2d\u8bbe\u7f6e\u6587\u4ef6\u7684\u7f16\u7801\u4e3aUTF-8\uff1f<\/strong><br \/>\u5728Python 3\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u6765\u8bbe\u7f6e\u6587\u4ef6\u7684\u7f16\u7801\u4e3aUTF-8\u3002\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4f20\u9012<code>encoding=&#39;utf-8&#39;<\/code>\u53c2\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;\u6587\u4ef6\u540d.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n    content = file.read()\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u8bfb\u53d6\u6587\u4ef6\u65f6\u4ee5UTF-8\u683c\u5f0f\u5904\u7406\u5185\u5bb9\uff0c\u907f\u514d\u7f16\u7801\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728Python 3\u4e2d\uff0c\u5982\u4f55\u786e\u4fdd\u5b57\u7b26\u4e32\u4f7f\u7528UTF-8\u7f16\u7801\uff1f<\/strong><br \/>\u5728Python 3\u4e2d\uff0c\u5b57\u7b26\u4e32\u9ed8\u8ba4\u4f7f\u7528Unicode\u7f16\u7801\u3002\u5982\u679c\u60a8\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUTF-8\u5b57\u8282\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">utf8_bytes = your_string.encode(&#39;utf-8&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5c06Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUTF-8\u683c\u5f0f\u7684\u5b57\u8282\u4e32\uff0c\u4fbf\u4e8e\u5b58\u50a8\u6216\u4f20\u8f93\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5Python 3\u7a0b\u5e8f\u4e2d\u662f\u5426\u4f7f\u7528UTF-8\u7f16\u7801\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u6587\u4ef6\u7684\u5f00\u5934\u90e8\u5206\uff0c\u5c24\u5176\u662f\u6e90\u4ee3\u7801\u6587\u4ef6\u7684\u7f16\u7801\u58f0\u660e\uff0c\u6765\u68c0\u67e5\u7f16\u7801\u8bbe\u7f6e\u3002\u867d\u7136Python 3\u9ed8\u8ba4\u4f7f\u7528UTF-8\uff0c\u4f46\u5982\u679c\u60a8\u60f3\u660e\u786e\u6307\u5b9a\uff0c\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\u6dfb\u52a0\u5982\u4e0b\u6ce8\u91ca\uff1a  <\/p>\n<pre><code class=\"language-python\"># -*- coding: utf-8 -*-\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u4f7f\u7528<code>sys.getdefaultencoding()<\/code>\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u9ed8\u8ba4\u7f16\u7801\uff0c\u786e\u8ba4\u662f\u5426\u4e3aUTF-8\u3002  <\/p>\n<pre><code class=\"language-python\">import sys\nprint(sys.getdefaultencoding())\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u5728Python 3\u4e2d\u6b63\u786e\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python3\u4e2d\u8bbe\u7f6eUTF-8\u7f16\u7801\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\u3001\u4f7f\u7528open\u51fd\u6570\u65f6\u6307\u5b9a\u7f16\u7801\u3001\u8bbe\u7f6e\u7cfb\u7edf\u9ed8 [&hellip;]","protected":false},"author":3,"featured_media":1112293,"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\/1112287"}],"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=1112287"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112287\/revisions"}],"predecessor-version":[{"id":1112296,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112287\/revisions\/1112296"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1112293"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1112287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1112287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1112287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}