{"id":70105,"date":"2024-01-29T16:03:30","date_gmt":"2024-01-29T08:03:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=70105"},"modified":"2024-01-29T16:03:30","modified_gmt":"2024-01-29T08:03:30","slug":"python%e4%b8%ad%e7%9a%84encodingutf-8%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/70105.html","title":{"rendered":"Python\u4e2d\u7684encoding=utf-8\u662f\u4ec0\u4e48\u610f\u601d"},"content":{"rendered":"<p>\u6982\u62ec\u6765\u8bf4\uff0c<strong>encoding=&#8221;utf-8&#8243;<\/strong> \u5728Python\u4e2d\u8868\u793a\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u4e3aUTF-8\u3002UTF-8\u662f\u4e00\u79cd\u9488\u5bf9Unicode\u5b57\u7b26\u96c6\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u80fd\u591f\u5904\u7406\u4e16\u754c\u4e0a\u5927\u591a\u6570\u6587\u5b57\u7cfb\u7edf\u7684\u7b26\u53f7\u3002\u5728\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u65f6\u52a0\u5165\u8fd9\u4e00\u53c2\u6570\uff0c\u786e\u4fdd\u6587\u672c\u5904\u7406\u7684\u51c6\u786e\u6027\u4e0e\u517c\u5bb9\u6027\u3002\u4e0b\u9762\u5c06\u5bf9UTF-8\u7f16\u7801\u7684\u80cc\u666f\u53ca\u5176\u5728Python\u4e2d\u7684\u5e94\u7528\u8fdb\u884c\u63a2\u8ba8\u3002<\/p>\n<p><strong>\u4e00\u3001UTF-8\u7f16\u7801\u7b80\u8ff0<\/strong><\/p>\n<p>UTF-8\u662f\u4e00\u79cd\u53d8\u957f\u5b57\u7b26\u7f16\u7801\u65b9\u5f0f\uff0c\u666e\u904d\u7528\u4e8e\u7f51\u9875\u548c\u64cd\u4f5c\u7cfb\u7edf\u3002\u5b83\u7684\u7279\u70b9\u662f\u5bf9\u82f1\u6587\u5b57\u7b26\u4f7f\u75281\u5b57\u8282\u7f16\u7801\uff0c\u6b27\u6d32\u7b49\u5b57\u7b26\u4f7f\u75282\u5b57\u8282\uff0c\u4e9a\u6d32\u5b57\u7b26\u4f7f\u75283\u5b57\u8282\uff0c\u5176\u5b83\u7279\u6b8a\u5b57\u7b26\u4f7f\u75284\u5b57\u8282\u3002\u8fd9\u79cd\u65b9\u5f0f\u8db3\u591f\u8986\u76d6Unicode\u5b57\u7b26\u96c6\u4e2d\u6240\u6709\u5b57\u7b26\u7684\u8868\u73b0\uff0c\u540c\u65f6\u8fd8\u8003\u8651\u4e86\u5b58\u50a8\u6548\u7387\u3002<\/p>\n<p><strong>\u4e8c\u3001UTF-8\u5728Python\u4e2d\u7684\u5e94\u7528<\/strong><\/p>\n<p>\u5728Python\u8bed\u8a00\u4e2d\uff0c\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\u9700\u8981\u6307\u5b9a\u7f16\u7801\u3002\u7279\u522b\u662f\u5728\u5168\u7403\u5316\u5e94\u7528\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5904\u7406\u591a\u79cd\u8bed\u8a00\u7684\u5b57\u7b26\uff0cUTF-8\u56e0\u5176\u666e\u9002\u6027\u6210\u4e3a\u9996\u9009\u3002Python\u7684\u5185\u7f6e\u51fd\u6570\u5982`open`\u4f1a\u7528\u5230`encoding`\u53c2\u6570\uff0c\u5b9a\u4e49\u4e86\u8bfb\u5199\u6587\u4ef6\u65f6\u6240\u7528\u7684\u7f16\u7801\u3002<\/p>\n<p><strong>\u4e09\u3001\u4e3a\u4f55\u8981\u4f7f\u7528UTF-8\u7f16\u7801<\/strong><\/p>\n<p>\u5bf9\u4e8e\u4e0d\u540c\u8bed\u8a00\u5f00\u53d1\u7684\u517c\u5bb9\u6027\u548c\u6587\u672c\u6570\u636e\u7684\u65e0\u635f\u4f20\u8f93\u6765\u8bf4\uff0c\u4f7f\u7528UTF-8\u7f16\u7801\u975e\u5e38\u91cd\u8981\u3002\u5b83\u907f\u514d\u4e86\u7f16\u7801\u4e0d\u4e00\u81f4\u5f15\u8d77\u7684\u4e71\u7801\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u591a\u8bed\u8a00\u73af\u5883\u4e0b\u7684\u6570\u636e\u4ea4\u6362\u65f6\u3002<\/p>\n<p><strong>\u56db\u3001\u7f16\u7801\u4e0e\u89e3\u7801\u5728Python\u4e2d\u7684\u91cd\u8981\u6027<\/strong><\/p>\n<p>\u7f16\u7801\u6307\u7684\u662f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e32\u5b57\u8282\uff0c\u800c\u89e3\u7801\u662f\u5c06\u5b57\u8282\u5e8f\u5217\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\u3002\u7406\u89e3\u8fd9\u4e00\u8fc7\u7a0b\u5bf9\u4e8e\u5904\u7406\u6765\u81ea\u4e0d\u540c\u6765\u6e90\u7684\u6587\u672c\u6570\u636e\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\u4e94\u3001\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528UTF-8\u7f16\u7801<\/strong><\/p>\n<p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u6307\u5b9aUTF-8\u7f16\u7801\u3002\u65e0\u8bba\u662f\u6587\u4ef6\u64cd\u4f5c\u8fd8\u662f\u7f51\u7edc\u901a\u4fe1\uff0c\u751a\u81f3\u662f\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u9002\u5f53\u5730\u4f7f\u7528UTF-8\u7f16\u7801\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u6570\u636e\u7684\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u516d\u3001UTF-8\u4e0e\u5176\u5b83\u7f16\u7801\u683c\u5f0f\u7684\u6bd4\u8f83<\/strong><\/p>\n<p>\u9664\u4e86UTF-8\u4e4b\u5916\uff0c\u8fd8\u6709\u8bf8\u5982ISO 8859-1\u3001GBK\u7b49\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\u3002\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\uff0c\u4e86\u89e3\u8fd9\u4e9b\u7f16\u7801\u683c\u5f0f\u4e4b\u95f4\u7684\u533a\u522b\u548c\u8f6c\u6362\u5bf9\u4e8e\u5904\u7406\u7f16\u7801\u95ee\u9898\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0cencoding=&#8221;utf-8&#8243;\u5728Python\u4e2d\u786e\u4fdd\u4e86\u5728\u8bfb\u53d6\u3001\u7f16\u8f91\u548c\u5b58\u50a8\u6587\u4ef6\u65f6\u5b57\u7b26\u7684\u6b63\u786e\u8868\u793a\u3002\u8003\u8651\u5230\u5168\u7403\u5316\u5e94\u7528\u7684\u9700\u6c42\u548c\u8de8\u5e73\u53f0\u7684\u6570\u636e\u901a\u8baf\uff0c\u5b83\u5df2\u7ecf\u6210\u4e3a\u7f16\u7801\u6587\u672c\u6570\u636e\u7684\u884c\u4e1a\u6807\u51c6\u3002\u5728\u8de8\u56fd\u9879\u76ee\u548c\u5904\u7406\u591a\u79cd\u8bed\u8a00\u73af\u5883\u4e0b\u7684\u5f00\u53d1\u4e2d\uff0c\u6b63\u786e\u7684\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u626e\u6f14\u4e86\u4e0d\u53ef\u5ffd\u89c6\u7684\u89d2\u8272\u3002<\/p>\n<p> <strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><strong>\u4e3a\u4ec0\u4e48\u5728Python\u4e2d\u9700\u8981\u8bbe\u7f6eencoding=utf-8\uff1f<\/strong><\/p>\n<p>Python\u4e2d\u7684encoding=utf-8\u662f\u7528\u6765\u6307\u5b9a\u4ee5UTF-8\u683c\u5f0f\u5bf9\u6587\u4ef6\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u7684\u8bbe\u7f6e\u3002UTF-8\u662f\u4e00\u79cdUnicode\u5b57\u7b26\u7f16\u7801\u65b9\u6848\uff0c\u5b83\u652f\u6301\u5168\u7403\u8303\u56f4\u5185\u51e0\u4e4e\u6240\u6709\u7684\u8bed\u8a00\u548c\u6587\u5b57\u3002\u5728Python\u4e2d\uff0c\u901a\u8fc7\u5c06\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u8bbe\u7f6e\u4e3aUTF-8\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\u80fd\u591f\u6b63\u786e\u5730\u8bfb\u53d6\u548c\u5199\u5165\u975e\u82f1\u6587\u5b57\u7b26\uff0c\u540c\u65f6\u907f\u514d\u51fa\u73b0\u4e71\u7801\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528encoding=utf-8\uff1f<\/strong><\/p>\n<p>\u8981\u5728Python\u4e2d\u4f7f\u7528encoding=utf-8\uff0c\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u5f00\u5934\u6dfb\u52a0`# -*- coding: utf-8 -*-`\u8fd9\u6837\u7684\u58f0\u660e\uff0c\u544a\u8bc9Python\u89e3\u91ca\u5668\u5e94\u8be5\u4f7f\u7528UTF-8\u7f16\u7801\u6765\u89e3\u6790\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u3002\u6b64\u5916\uff0c\u5728\u8fdb\u884c\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u65f6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a`encoding=&#8217;utf-8&#8217;`\u53c2\u6570\u6765\u660e\u786e\u8868\u793a\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p><strong>encoding=utf-8\u548c\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><\/p>\n<p>\u4e0e\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\u76f8\u6bd4\uff0cUTF-8\u5177\u6709\u8f83\u597d\u7684\u517c\u5bb9\u6027\u548c\u6269\u5c55\u6027\uff0c\u80fd\u591f\u51c6\u786e\u5730\u8868\u793a\u591a\u79cd\u8bed\u8a00\u7684\u5b57\u7b26\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u4e00\u4e9b\u4f20\u7edf\u7684\u7f16\u7801\u683c\u5f0f\u53ef\u80fd\u65e0\u6cd5\u5b8c\u5168\u8986\u76d6\u548c\u5904\u7406\u5404\u79cd\u7279\u6b8a\u5b57\u7b26\uff0c\u5bb9\u6613\u9020\u6210\u6570\u636e\u635f\u574f\u6216\u4e71\u7801\u7684\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u9009\u62e9encoding=utf-8\u80fd\u591f\u66f4\u597d\u5730\u6ee1\u8db3\u5f53\u524d\u591a\u8bed\u8a00\u73af\u5883\u4e0b\u7684\u7f16\u7801\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6982\u62ec\u6765\u8bf4\uff0cencoding=&#8221;utf-8&#8243; \u5728Python\u4e2d\u8868\u793a\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u4e3aU [&hellip;]","protected":false},"author":1,"featured_media":70107,"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\/70105"}],"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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=70105"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/70105\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/70107"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=70105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=70105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=70105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}