{"id":1000513,"date":"2024-12-27T09:50:44","date_gmt":"2024-12-27T01:50:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1000513.html"},"modified":"2024-12-27T09:50:52","modified_gmt":"2024-12-27T01:50:52","slug":"python%e5%a6%82%e4%bd%95%e5%bc%95%e5%85%a5%e4%b8%ad%e6%96%87%e4%b9%b1%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1000513.html","title":{"rendered":"python\u5982\u4f55\u5f15\u5165\u4e2d\u6587\u4e71\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075059\/52bf9b7a-1225-46be-abe8-bea56d0e9ddb.webp\" alt=\"python\u5982\u4f55\u5f15\u5165\u4e2d\u6587\u4e71\u7801\" \/><\/p>\n<p><p> <strong>\u8981\u5f15\u5165\u4e2d\u6587\u4e71\u7801\u95ee\u9898\uff0c\u901a\u5e38\u662f\u56e0\u4e3a\u7f16\u7801\u683c\u5f0f\u4e0d\u5339\u914d\u3001\u4f7f\u7528\u4e0d\u5f53\u7684\u6587\u4ef6\u8bfb\u5199\u6a21\u5f0f\u3001\u672a\u6b63\u786e\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3001Python\u7248\u672c\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/strong>\u5728Python\u4e2d\u5904\u7406\u4e2d\u6587\u4e71\u7801\u95ee\u9898\uff0c\u5efa\u8bae\u4f18\u5148\u68c0\u67e5\u7f16\u7801\u683c\u5f0f\u3002Python 3\u4e2d\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4f46\u5728\u6587\u4ef6\u8bfb\u5199\u65f6\u4ecd\u9700\u660e\u786e\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u7f16\u7801\u683c\u5f0f\u4e0d\u5339\u914d<\/p>\n<\/p>\n<p><p>\u5728Python\u7a0b\u5e8f\u4e2d\uff0c\u4e2d\u6587\u4e71\u7801\u901a\u5e38\u6e90\u4e8e\u7f16\u7801\u683c\u5f0f\u4e0d\u5339\u914d\u3002\u7f16\u7801\u683c\u5f0f\u662f\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5b57\u8282\u7684\u65b9\u5f0f\uff0c\u800c\u89e3\u7801\u683c\u5f0f\u5219\u662f\u5c06\u5b57\u8282\u8f6c\u6362\u4e3a\u5b57\u7b26\u7684\u65b9\u5f0f\u3002\u5e38\u89c1\u7684\u7f16\u7801\u683c\u5f0f\u6709UTF-8\u3001GBK\u7b49\u3002\u82e5\u6587\u4ef6\u4fdd\u5b58\u65f6\u4f7f\u7528GBK\u7f16\u7801\uff0c\u800c\u8bfb\u53d6\u65f6\u4f7f\u7528UTF-8\u89e3\u7801\uff0c\u5c31\u4f1a\u4ea7\u751f\u4e71\u7801\u3002\u56e0\u6b64\uff0c<strong>\u786e\u4fdd\u7f16\u7801\u548c\u89e3\u7801\u683c\u5f0f\u5339\u914d\u81f3\u5173\u91cd\u8981<\/strong>\u3002<\/p>\n<\/p>\n<p><p>UTF-8\u662f\u76ee\u524d\u6700\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u652f\u6301\u6240\u6709\u8bed\u8a00\u7684\u5b57\u7b26\u96c6\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u5728\u7f16\u5199Python\u7a0b\u5e8f\u65f6\uff0c\u7edf\u4e00\u4f7f\u7528UTF-8\u7f16\u7801\u3002\u4e3a\u4e86\u907f\u514d\u4e71\u7801\u95ee\u9898\uff0c\u6253\u5f00\u6587\u4ef6\u65f6\uff0c\u660e\u786e\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:<\/p>\n<p>    content = f.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fddPython\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u4f7f\u7528UTF-8\u89e3\u7801\uff0c\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u6587\u4ef6\u8bfb\u5199\u6a21\u5f0f\u4e0d\u5f53<\/p>\n<\/p>\n<p><p>\u6587\u4ef6\u8bfb\u5199\u6a21\u5f0f\u7684\u9009\u62e9\u4e5f\u53ef\u80fd\u5bfc\u81f4\u4e2d\u6587\u4e71\u7801\u3002\u5728Python\u4e2d\uff0c\u6587\u4ef6\u64cd\u4f5c\u6709\u591a\u79cd\u6a21\u5f0f\uff0c\u5982\u6587\u672c\u6a21\u5f0f\uff08&#39;r&#39;, &#39;w&#39;\uff09\u548c\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff08&#39;rb&#39;, &#39;wb&#39;\uff09\u3002<strong>\u9009\u62e9\u4e0d\u5f53\u7684\u6a21\u5f0f\u53ef\u80fd\u5bfc\u81f4\u4e71\u7801<\/strong>\u3002\u6587\u672c\u6a21\u5f0f\u4e0b\uff0cPython\u4f1a\u5c06\u5b57\u8282\u81ea\u52a8\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u800c\u4e8c\u8fdb\u5236\u6a21\u5f0f\u5219\u4e0d\u4f1a\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u6587\u672c\u65f6\uff0c\u901a\u5e38\u5e94\u4f7f\u7528\u6587\u672c\u6a21\u5f0f\uff0c\u5e76\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as f:<\/p>\n<p>    f.write(&#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0cPython\u4f1a\u6309\u7167\u6307\u5b9a\u7684\u7f16\u7801\u683c\u5f0f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\uff0c\u5199\u5165\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u672a\u6b63\u786e\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f<\/p>\n<\/p>\n<p><p>\u5373\u4f7f\u5728Python 3\u4e2d\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4f46\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c<strong>\u660e\u786e\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u4ecd\u7136\u5f88\u91cd\u8981<\/strong>\u3002\u5c24\u5176\u662f\u5f53\u6587\u4ef6\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u95f4\u4f20\u8f93\u65f6\uff0c\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u9ed8\u8ba4\u4f7f\u7528\u4e0d\u540c\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u5982Windows\u901a\u5e38\u4f7f\u7528GBK\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u786e\u4fdd\u8de8\u5e73\u53f0\u7684\u517c\u5bb9\u6027\uff0c\u5efa\u8bae\u59cb\u7ec8\u663e\u5f0f\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:<\/p>\n<p>    content = f.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u65e0\u8bba\u5728\u54ea\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\uff0c\u7a0b\u5e8f\u90fd\u80fd\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001Python\u7248\u672c\u517c\u5bb9\u6027\u95ee\u9898<\/p>\n<\/p>\n<p><p>Python 2\u548cPython 3\u5728\u5904\u7406\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u65f6\u6709\u663e\u8457\u5dee\u5f02\u3002Python 2\u9ed8\u8ba4\u4f7f\u7528ASCII\u7f16\u7801\uff0c\u9700\u663e\u5f0f\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u6765\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u4e71\u7801\u3002Python 3\u6539\u8fdb\u4e86\u8fd9\u4e00\u70b9\uff0c\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4f46\u4ecd\u9700\u6ce8\u610f\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u4ee3\u7801\u9700\u8981\u5728Python 2\u548cPython 3\u4e4b\u95f4\u517c\u5bb9\uff0c\u5efa\u8bae\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>from __future__ import unicode_literals<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fddPython 2\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u5e76\u542f\u7528Python 3\u7684\u5b57\u7b26\u4e32\u5904\u7406\u7279\u6027\uff0c\u51cf\u5c11\u4e2d\u6587\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7ec8\u7aef\u663e\u793a\u95ee\u9898<\/strong>\uff1a\u6709\u65f6\uff0c\u4e2d\u6587\u4e71\u7801\u5e76\u4e0d\u662f\u4ee3\u7801\u95ee\u9898\uff0c\u800c\u662f\u7ec8\u7aef\u65e0\u6cd5\u6b63\u786e\u663e\u793a\u4e2d\u6587\u5b57\u7b26\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u68c0\u67e5\u7ec8\u7aef\u7684\u7f16\u7801\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u5e93\u7f16\u7801\u95ee\u9898<\/strong>\uff1a\u5982\u679c\u4ece\u6570\u636e\u5e93\u8bfb\u53d6\u4e2d\u6587\u6570\u636e\uff0c\u4e5f\u9700\u786e\u4fdd\u6570\u636e\u5e93\u8fde\u63a5\u4f7f\u7528\u4e86\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f51\u7edc\u4f20\u8f93\u7f16\u7801\u95ee\u9898<\/strong>\uff1a\u5728\u7f51\u7edc\u4f20\u8f93\u4e2d\u6587\u6570\u636e\u65f6\uff0c\u4f7f\u7528\u4e0d\u5f53\u7684\u7f16\u7801\u683c\u5f0f\u4e5f\u53ef\u80fd\u5bfc\u81f4\u4e71\u7801\u3002\u5728\u4f20\u8f93\u524d\uff0c\u786e\u4fdd\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b2c\u4e09\u65b9\u5e93\u95ee\u9898<\/strong>\uff1a\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u80fd\u4e0d\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u6216\u4f7f\u7528\u4e86\u4e0d\u5f53\u7684\u7f16\u7801\u683c\u5f0f\u3002\u5728\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u65f6\uff0c\u68c0\u67e5\u5176\u6587\u6863\u548c\u7f16\u7801\u652f\u6301\u60c5\u51b5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u6216\u89e3\u51b3Python\u4e2d\u5904\u7406\u4e2d\u6587\u65f6\u7684\u4e71\u7801\u95ee\u9898\uff0c\u786e\u4fdd\u7a0b\u5e8f\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\u89e3\u51b3Python\u4e2d\u51fa\u73b0\u7684\u4e2d\u6587\u4e71\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5904\u7406\u4e2d\u6587\u65f6\uff0c\u4e71\u7801\u901a\u5e38\u662f\u7531\u4e8e\u7f16\u7801\u4e0d\u5339\u914d\u5bfc\u81f4\u7684\u3002\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u786e\u4fdd\u5728\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u65f6\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u3002\u901a\u5e38\u5efa\u8bae\u4f7f\u7528UTF-8\u7f16\u7801\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\u786e\u4fdd\u4f60\u7684Python\u6e90\u6587\u4ef6\u672c\u8eab\u4e5f\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u4e0d\u540c\u7f16\u7801\u7684\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5904\u7406\u4e0d\u540c\u7f16\u7801\u683c\u5f0f\u7684\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u6700\u597d\u5148\u4e86\u89e3\u6587\u4ef6\u7684\u539f\u59cb\u7f16\u7801\u683c\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528<code>chardet<\/code>\u5e93\u6765\u68c0\u6d4b\u6587\u4ef6\u7f16\u7801\uff0c\u7136\u540e\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u4f7f\u7528\u76f8\u5e94\u7684\u7f16\u7801\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import chardet\n\nwith open(&#39;file.txt&#39;, &#39;rb&#39;) as f:\n    result = chardet.detect(f.read())\n    encoding = result[&#39;encoding&#39;]\n\nwith open(&#39;file.txt&#39;, &#39;r&#39;, encoding=encoding) as f:\n    content = f.read()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u4e0d\u4e00\u81f4\u800c\u5bfc\u81f4\u7684\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5370\u4e2d\u6587\u5b57\u7b26\u800c\u4e0d\u51fa\u73b0\u4e71\u7801\uff1f<\/strong><br \/>\u5728\u7ec8\u7aef\u6216\u63a7\u5236\u53f0\u4e2d\u6253\u5370\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u786e\u4fdd\u4f60\u7684\u7ec8\u7aef\u652f\u6301UTF-8\u7f16\u7801\u3002\u5982\u679c\u6253\u5370\u65f6\u51fa\u73b0\u4e71\u7801\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u5f00\u5934\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a  <\/p>\n<pre><code class=\"language-python\">import sys\nimport io\n\nsys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding=&#39;utf-8&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u786e\u4fdd\u5728\u6253\u5370\u8f93\u51fa\u65f6\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4ece\u800c\u6b63\u786e\u663e\u793a\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5f15\u5165\u4e2d\u6587\u4e71\u7801\u95ee\u9898\uff0c\u901a\u5e38\u662f\u56e0\u4e3a\u7f16\u7801\u683c\u5f0f\u4e0d\u5339\u914d\u3001\u4f7f\u7528\u4e0d\u5f53\u7684\u6587\u4ef6\u8bfb\u5199\u6a21\u5f0f\u3001\u672a\u6b63\u786e\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3001Python\u7248 [&hellip;]","protected":false},"author":3,"featured_media":1000532,"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\/1000513"}],"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=1000513"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000513\/revisions"}],"predecessor-version":[{"id":1000538,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000513\/revisions\/1000538"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1000532"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1000513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1000513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1000513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}