{"id":1098117,"date":"2025-01-08T15:18:16","date_gmt":"2025-01-08T07:18:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098117.html"},"modified":"2025-01-08T15:18:19","modified_gmt":"2025-01-08T07:18:19","slug":"%e5%9c%a8python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e4%b8%ad%e6%96%87%e4%b9%b1%e7%a0%81%e9%97%ae%e9%a2%98-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098117.html","title":{"rendered":"\u5728python\u4e2d\u5982\u4f55\u8f93\u51fa\u4e2d\u6587\u4e71\u7801\u95ee\u9898"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212601\/3cfddf18-eb46-4f4a-bc3b-1716a14e390f.webp\" alt=\"\u5728python\u4e2d\u5982\u4f55\u8f93\u51fa\u4e2d\u6587\u4e71\u7801\u95ee\u9898\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u89e3\u51b3\u8f93\u51fa\u4e2d\u6587\u4e71\u7801\u95ee\u9898\u7684\u65b9\u6cd5\u6709\uff1a\u8bbe\u7f6e\u7f16\u7801\u3001\u4f7f\u7528\u76f8\u5e94\u7684\u7f16\u7801\u51fd\u6570\u3001\u914d\u7f6eIDE\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u3001\u5b89\u88c5\u76f8\u5173\u5e93\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u4e14\u6709\u6548\u7684\u65b9\u6cd5\u662f<strong>\u8bbe\u7f6e\u7f16\u7801<\/strong>\u3002\u901a\u8fc7\u5728\u811a\u672c\u5f00\u5934\u8bbe\u7f6e\u7f16\u7801\uff0c\u53ef\u4ee5\u786e\u4fddPython\u89e3\u91ca\u5668\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u8bbe\u7f6e\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u7684\u5f00\u5934\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\u662f\u89e3\u51b3\u4e2d\u6587\u4e71\u7801\u95ee\u9898\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\u6216\u7b2c\u4e8c\u884c\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\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\u884c\u4ee3\u7801\u544a\u8bc9Python\u89e3\u91ca\u5668\u4f7f\u7528UTF-8\u7f16\u7801\u6765\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u3002UTF-8\u662f\u4e00\u79cd\u5e38\u7528\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u80fd\u591f\u6b63\u786e\u8868\u793a\u4e2d\u6587\u5b57\u7b26\uff0c\u4ece\u800c\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u76f8\u5e94\u7684\u7f16\u7801\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4ec5\u4ec5\u8bbe\u7f6e\u7f16\u7801\u58f0\u660e\u53ef\u80fd\u5e76\u4e0d\u8db3\u591f\u3002\u7279\u522b\u662f\u5f53\u4f60\u9700\u8981\u5904\u7406\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u6216\u7f51\u7edc\u8bf7\u6c42\u65f6\uff0c\u4f7f\u7528\u76f8\u5e94\u7684\u7f16\u7801\u51fd\u6570\u4e5f\u662f\u5f88\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5904\u7406\u6587\u4ef6\u8f93\u5165\u8f93\u51fa<\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u65f6\uff0c\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u662f\u907f\u514d\u4e2d\u6587\u4e71\u7801\u7684\u5173\u952e\u3002\u53ef\u4ee5\u5728<code>open<\/code>\u51fd\u6570\u4e2d\u4f7f\u7528<code>encoding<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\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>\u7c7b\u4f3c\u5730\uff0c\u5728\u5199\u5165\u6587\u4ef6\u65f6\u4e5f\u9700\u8981\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\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 file:<\/p>\n<p>    file.write(&#39;\u8fd9\u662f\u4e00\u4e9b\u4e2d\u6587\u5185\u5bb9&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5904\u7406\u7f51\u7edc\u8bf7\u6c42<\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\u65f6\uff0c\u7279\u522b\u662f\u5f53\u4ece\u7f51\u9875\u4e0a\u83b7\u53d6\u6570\u636e\u65f6\uff0c\u901a\u5e38\u9700\u8981\u6307\u5b9a\u54cd\u5e94\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>requests<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u54cd\u5e94\u7684\u7f16\u7801\u6765\u907f\u514d\u4e71\u7801\u95ee\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p>response.encoding = &#39;utf-8&#39;<\/p>\n<p>content = response.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u914d\u7f6eIDE<\/p>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5bf9\u7f16\u7801\u7684\u5904\u7406\u65b9\u5f0f\u53ef\u80fd\u6709\u6240\u4e0d\u540c\uff0c\u786e\u4fddIDE\u7684\u7f16\u7801\u8bbe\u7f6e\u4e0ePython\u811a\u672c\u7684\u7f16\u7801\u8bbe\u7f6e\u4e00\u81f4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<ol>\n<li>PyCharm<\/li>\n<\/ol>\n<p><p>\u5728PyCharm\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u6587\u4ef6\u7f16\u7801\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00PyCharm\uff0c\u70b9\u51fb<code>File<\/code>\u83dc\u5355\uff0c\u9009\u62e9<code>Settings<\/code>\u3002<\/li>\n<li>\u5728\u8bbe\u7f6e\u7a97\u53e3\u4e2d\uff0c\u5bfc\u822a\u5230<code>Editor<\/code> -&gt; <code>File Encodings<\/code>\u3002<\/li>\n<li>\u5728<code>Project Encoding<\/code>\u548c<code>Default encoding for properties files<\/code>\u4e2d\u9009\u62e9<code>UTF-8<\/code>\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>VS Code<\/li>\n<\/ol>\n<p><p>\u5728VS Code\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u6587\u4ef6\u7f16\u7801\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00VS Code\uff0c\u70b9\u51fb\u53f3\u4e0b\u89d2\u7684\u7f16\u7801\u683c\u5f0f\uff08\u4f8b\u5982<code>UTF-8<\/code>\uff09\u3002<\/li>\n<li>\u5728\u5f39\u51fa\u7684\u83dc\u5355\u4e2d\u9009\u62e9<code>Reopen with Encoding<\/code>\uff0c\u7136\u540e\u9009\u62e9<code>UTF-8<\/code>\u3002<\/li>\n<\/ul>\n<p><p>\u56db\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u4e5f\u662f\u89e3\u51b3\u4e2d\u6587\u4e71\u7801\u95ee\u9898\u7684\u6709\u6548\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>PYTHONIOENCODING<\/code>\u73af\u5883\u53d8\u91cf\u6765\u6307\u5b9aPython\u4f7f\u7528\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u5728Linux\u6216MacOS\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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>\u5728Windows\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cmd\">set PYTHONIOENCODING=utf-8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5b89\u88c5\u76f8\u5173\u5e93<\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u5b89\u88c5\u548c\u4f7f\u7528\u76f8\u5173\u7684\u5e93\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u89e3\u51b3\u4e2d\u6587\u4e71\u7801\u95ee\u9898\u3002\u4f8b\u5982\uff0c<code>chardet<\/code>\u5e93\u53ef\u4ee5\u81ea\u52a8\u68c0\u6d4b\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4ece\u800c\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5<code>chardet<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install chardet<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>chardet<\/code>\u5e93\u68c0\u6d4b\u6587\u4ef6\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import chardet<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    raw_data = file.read()<\/p>\n<p>    result = chardet.detect(raw_data)<\/p>\n<p>    encoding = result[&#39;encoding&#39;]<\/p>\n<p>    content = raw_data.decode(encoding)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u89e3\u51b3Python\u4e2d\u8f93\u51fa\u4e2d\u6587\u4e71\u7801\u7684\u95ee\u9898\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u4e2a\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u8bbe\u7f6e\u7f16\u7801<\/p>\n<\/p>\n<ol>\n<li>\u5728Python\u811a\u672c\u5f00\u5934\u8bbe\u7f6e\u7f16\u7801\u58f0\u660e<\/li>\n<\/ol>\n<p><p>\u5728Python\u811a\u672c\u7684\u7b2c\u4e00\u884c\u6216\u7b2c\u4e8c\u884c\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\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\u884c\u4ee3\u7801\u544a\u8bc9Python\u89e3\u91ca\u5668\u4f7f\u7528UTF-8\u7f16\u7801\u6765\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u3002UTF-8\u662f\u4e00\u79cd\u5e38\u7528\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u80fd\u591f\u6b63\u786e\u8868\u793a\u4e2d\u6587\u5b57\u7b26\uff0c\u4ece\u800c\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528BOM\uff08Byte Order Mark\uff09<\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7279\u522b\u662f\u5f53\u6587\u4ef6\u9700\u8981\u5728\u4e0d\u540c\u5e73\u53f0\u4e4b\u95f4\u4f20\u8f93\u65f6\uff0c\u4f7f\u7528BOM\uff08Byte Order Mark\uff09\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u88ab\u6b63\u786e\u8bc6\u522b\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0BOM\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8-sig&#39;) as file:<\/p>\n<p>    file.write(&#39;\u8fd9\u662f\u4e00\u4e9b\u4e2d\u6587\u5185\u5bb9&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>utf-8-sig<\/code>\u7f16\u7801\u683c\u5f0f\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0BOM\uff0c\u4ece\u800c\u786e\u4fdd\u6587\u4ef6\u5728\u4e0d\u540c\u5e73\u53f0\u4e4b\u95f4\u4f20\u8f93\u65f6\u7f16\u7801\u683c\u5f0f\u88ab\u6b63\u786e\u8bc6\u522b\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u76f8\u5e94\u7684\u7f16\u7801\u51fd\u6570<\/p>\n<\/p>\n<ol>\n<li>\u5904\u7406\u6587\u4ef6\u8f93\u5165\u8f93\u51fa<\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u65f6\uff0c\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u662f\u907f\u514d\u4e2d\u6587\u4e71\u7801\u7684\u5173\u952e\u3002\u53ef\u4ee5\u5728<code>open<\/code>\u51fd\u6570\u4e2d\u4f7f\u7528<code>encoding<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\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>\u7c7b\u4f3c\u5730\uff0c\u5728\u5199\u5165\u6587\u4ef6\u65f6\u4e5f\u9700\u8981\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\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 file:<\/p>\n<p>    file.write(&#39;\u8fd9\u662f\u4e00\u4e9b\u4e2d\u6587\u5185\u5bb9&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5904\u7406\u7f51\u7edc\u8bf7\u6c42<\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\u65f6\uff0c\u7279\u522b\u662f\u5f53\u4ece\u7f51\u9875\u4e0a\u83b7\u53d6\u6570\u636e\u65f6\uff0c\u901a\u5e38\u9700\u8981\u6307\u5b9a\u54cd\u5e94\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>requests<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u54cd\u5e94\u7684\u7f16\u7801\u6765\u907f\u514d\u4e71\u7801\u95ee\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p>response.encoding = &#39;utf-8&#39;<\/p>\n<p>content = response.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u914d\u7f6eIDE<\/p>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5bf9\u7f16\u7801\u7684\u5904\u7406\u65b9\u5f0f\u53ef\u80fd\u6709\u6240\u4e0d\u540c\uff0c\u786e\u4fddIDE\u7684\u7f16\u7801\u8bbe\u7f6e\u4e0ePython\u811a\u672c\u7684\u7f16\u7801\u8bbe\u7f6e\u4e00\u81f4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<ol>\n<li>PyCharm<\/li>\n<\/ol>\n<p><p>\u5728PyCharm\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u6587\u4ef6\u7f16\u7801\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00PyCharm\uff0c\u70b9\u51fb<code>File<\/code>\u83dc\u5355\uff0c\u9009\u62e9<code>Settings<\/code>\u3002<\/li>\n<li>\u5728\u8bbe\u7f6e\u7a97\u53e3\u4e2d\uff0c\u5bfc\u822a\u5230<code>Editor<\/code> -&gt; <code>File Encodings<\/code>\u3002<\/li>\n<li>\u5728<code>Project Encoding<\/code>\u548c<code>Default encoding for properties files<\/code>\u4e2d\u9009\u62e9<code>UTF-8<\/code>\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>VS Code<\/li>\n<\/ol>\n<p><p>\u5728VS Code\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u6587\u4ef6\u7f16\u7801\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00VS Code\uff0c\u70b9\u51fb\u53f3\u4e0b\u89d2\u7684\u7f16\u7801\u683c\u5f0f\uff08\u4f8b\u5982<code>UTF-8<\/code>\uff09\u3002<\/li>\n<li>\u5728\u5f39\u51fa\u7684\u83dc\u5355\u4e2d\u9009\u62e9<code>Reopen with Encoding<\/code>\uff0c\u7136\u540e\u9009\u62e9<code>UTF-8<\/code>\u3002<\/li>\n<\/ul>\n<p><p>\u56db\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u4e5f\u662f\u89e3\u51b3\u4e2d\u6587\u4e71\u7801\u95ee\u9898\u7684\u6709\u6548\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>PYTHONIOENCODING<\/code>\u73af\u5883\u53d8\u91cf\u6765\u6307\u5b9aPython\u4f7f\u7528\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u5728Linux\u6216MacOS\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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>\u5728Windows\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cmd\">set PYTHONIOENCODING=utf-8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5b89\u88c5\u76f8\u5173\u5e93<\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u5b89\u88c5\u548c\u4f7f\u7528\u76f8\u5173\u7684\u5e93\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u89e3\u51b3\u4e2d\u6587\u4e71\u7801\u95ee\u9898\u3002\u4f8b\u5982\uff0c<code>chardet<\/code>\u5e93\u53ef\u4ee5\u81ea\u52a8\u68c0\u6d4b\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4ece\u800c\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5<code>chardet<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install chardet<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>chardet<\/code>\u5e93\u68c0\u6d4b\u6587\u4ef6\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import chardet<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    raw_data = file.read()<\/p>\n<p>    result = chardet.detect(raw_data)<\/p>\n<p>    encoding = result[&#39;encoding&#39;]<\/p>\n<p>    content = raw_data.decode(encoding)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u89e3\u51b3Python\u4e2d\u8f93\u51fa\u4e2d\u6587\u4e71\u7801\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7279\u522b\u662f\u5f53\u4f60\u65e0\u6cd5\u63a7\u5236\u8f93\u5165\u8f93\u51fa\u7684\u7f16\u7801\u683c\u5f0f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u6765\u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ecePython 3.0\u5f00\u59cb\uff0c<code>sys.setdefaultencoding()<\/code>\u51fd\u6570\u88ab\u79fb\u9664\u4e86\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528<code>reload<\/code>\u51fd\u6570\u6765\u91cd\u65b0\u52a0\u8f7d<code>sys<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import importlib<\/p>\n<p>importlib.reload(sys)<\/p>\n<p>sys.setdefaultencoding(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u5904\u7406\u6570\u636e\u5e93\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u786e\u4fdd\u6570\u636e\u5e93\u7684\u7f16\u7801\u8bbe\u7f6e\u4e0ePython\u811a\u672c\u7684\u7f16\u7801\u8bbe\u7f6e\u4e00\u81f4\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5MySQL\u6570\u636e\u5e93\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u6570\u636e\u5e93\u7f16\u7801\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u6570\u636e\u5e93\u65f6\u6307\u5b9a\u7f16\u7801\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-sql\">CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8fde\u63a5\u6570\u636e\u5e93\u65f6\u6307\u5b9a\u7f16\u7801\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;user&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    database=&#39;mydatabase&#39;,<\/p>\n<p>    charset=&#39;utf8mb4&#39;<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5e93\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u4e0d\u4f1a\u51fa\u73b0\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u5904\u7406\u7ec8\u7aef\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7279\u522b\u662f\u5f53\u5728\u7ec8\u7aef\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u786e\u4fdd\u7ec8\u7aef\u7684\u7f16\u7801\u8bbe\u7f6e\u4e0ePython\u811a\u672c\u7684\u7f16\u7801\u8bbe\u7f6e\u4e00\u81f4\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5728Windows\u7ec8\u7aef\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8bbe\u7f6e\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cmd\">chcp 65001<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>chcp 65001<\/code>\u547d\u4ee4\u8bbe\u7f6e\u7ec8\u7aef\u7684\u7f16\u7801\u4e3aUTF-8\uff0c\u4ece\u800c\u786e\u4fdd\u7ec8\u7aef\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\u65f6\u4e0d\u4f1a\u51fa\u73b0\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u5904\u7406\u65e5\u5fd7\u6587\u4ef6\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u5728\u8bb0\u5f55\u65e5\u5fd7\u65f6\uff0c\u786e\u4fdd\u65e5\u5fd7\u6587\u4ef6\u7684\u7f16\u7801\u8bbe\u7f6e\u4e0ePython\u811a\u672c\u7684\u7f16\u7801\u8bbe\u7f6e\u4e00\u81f4\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u5728\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(<\/p>\n<p>    filename=&#39;example.log&#39;,<\/p>\n<p>    level=logging.DEBUG,<\/p>\n<p>    format=&#39;%(asctime)s %(message)s&#39;,<\/p>\n<p>    encoding=&#39;utf-8&#39;<\/p>\n<p>)<\/p>\n<p>logging.debug(&#39;\u8fd9\u662f\u4e00\u4e9b\u4e2d\u6587\u5185\u5bb9&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u89e3\u51b3Python\u4e2d\u8f93\u51fa\u4e2d\u6587\u4e71\u7801\u7684\u95ee\u9898\u3002\u65e0\u8bba\u662f\u8bbe\u7f6e\u7f16\u7801\u58f0\u660e\u3001\u4f7f\u7528\u76f8\u5e94\u7684\u7f16\u7801\u51fd\u6570\u3001\u914d\u7f6eIDE\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u3001\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u5904\u7406\u6570\u636e\u5e93\u7f16\u7801\u3001\u5904\u7406\u7ec8\u7aef\u8f93\u51fa\u8fd8\u662f\u5904\u7406\u65e5\u5fd7\u6587\u4ef6\u7f16\u7801\uff0c\u786e\u4fdd\u7f16\u7801\u8bbe\u7f6e\u7684\u4e00\u81f4\u6027\u662f\u907f\u514d\u4e71\u7801\u95ee\u9898\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u89e3\u51b3\u4e2d\u6587\u4e71\u7801\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u4e2d\u6587\u4e71\u7801\u901a\u5e38\u662f\u7531\u4e8e\u7f16\u7801\u4e0d\u5339\u914d\u5f15\u8d77\u7684\u3002\u786e\u4fdd\u5728\u5904\u7406\u6587\u4ef6\u65f6\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u63a8\u8350\u4f7f\u7528UTF-8\u3002\u53ef\u4ee5\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\uff0c\u4f8b\u5982\uff1a<code>open(&#39;file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;)<\/code>\u3002\u6b64\u5916\uff0c\u5728\u6253\u5370\u8f93\u51fa\u65f6\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u7ec8\u7aef\u7684\u7f16\u7801\uff0c\u4ee5\u786e\u4fdd\u4e2d\u6587\u80fd\u591f\u6b63\u786e\u663e\u793a\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5Python\u4ee3\u7801\u4e2d\u7684\u7f16\u7801\u8bbe\u7f6e\uff1f<\/strong><br \/>\u5728Python\u6587\u4ef6\u7684\u5f00\u5934\uff0c\u53ef\u4ee5\u6dfb\u52a0<code># -*- coding: utf-8 -*-<\/code>\u6765\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u89e3\u91ca\u5668\u6309\u7167UTF-8\u7f16\u7801\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u907f\u514d\u56e0\u7f16\u7801\u4e0d\u4e00\u81f4\u5bfc\u81f4\u7684\u4e71\u7801\u95ee\u9898\u3002\u540c\u65f6\uff0c\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3aUTF-8\uff0c\u4f8b\u5982\uff1a<code>print(your_string.encode(&#39;utf-8&#39;).decode(&#39;utf-8&#39;))<\/code>\uff0c\u4ee5\u9a8c\u8bc1\u8f93\u51fa\u662f\u5426\u6b63\u5e38\u3002<\/p>\n<p><strong>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u5982\u4f55\u5904\u7406\u4e2d\u6587\u4e71\u7801\uff1f<\/strong><br \/>\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u5b57\u7b26\u7f16\u7801\u7684\u5904\u7406\u65b9\u5f0f\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u547d\u4ee4\u884c\u7684\u9ed8\u8ba4\u7f16\u7801\u53ef\u80fd\u4e0d\u662fUTF-8\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4e2d\u6587\u663e\u793a\u4e0d\u6b63\u5e38\u3002\u53ef\u4ee5\u901a\u8fc7\u6539\u53d8\u547d\u4ee4\u884c\u7684\u4ee3\u7801\u9875\u6765\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u4f7f\u7528\u547d\u4ee4<code>chcp 65001<\/code>\u5207\u6362\u5230UTF-8\u7f16\u7801\u3002\u5728Linux\u548cMac\u7cfb\u7edf\u4e2d\uff0c\u901a\u5e38\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u786e\u4fdd\u6e90\u6587\u4ef6\u53ca\u8f93\u51fa\u73af\u5883\u4e5f\u4f7f\u7528\u76f8\u540c\u7684\u7f16\u7801\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u89e3\u51b3\u8f93\u51fa\u4e2d\u6587\u4e71\u7801\u95ee\u9898\u7684\u65b9\u6cd5\u6709\uff1a\u8bbe\u7f6e\u7f16\u7801\u3001\u4f7f\u7528\u76f8\u5e94\u7684\u7f16\u7801\u51fd\u6570\u3001\u914d\u7f6eIDE\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u3001\u5b89\u88c5\u76f8\u5173 [&hellip;]","protected":false},"author":3,"featured_media":1098127,"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\/1098117"}],"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=1098117"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098117\/revisions"}],"predecessor-version":[{"id":1098130,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098117\/revisions\/1098130"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098127"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}