{"id":949464,"date":"2024-12-27T00:13:12","date_gmt":"2024-12-26T16:13:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/949464.html"},"modified":"2024-12-27T00:13:14","modified_gmt":"2024-12-26T16:13:14","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%aeencoding","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/949464.html","title":{"rendered":"python\u5982\u4f55\u8bbe\u7f6eencoding"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084433\/5445f579-3f9e-40ee-84ac-39dcc774aefe.webp\" alt=\"python\u5982\u4f55\u8bbe\u7f6eencoding\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8bbe\u7f6e\u7f16\u7801\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\uff1a\u5728\u6587\u4ef6\u9876\u90e8\u58f0\u660e\u7f16\u7801\u3001\u4f7f\u7528\u7f16\u7801\u53c2\u6570\u6253\u5f00\u6587\u4ef6\u3001\u6307\u5b9a\u9ed8\u8ba4\u7f16\u7801\u3001\u5904\u7406\u5b57\u7b26\u4e32\u7f16\u7801\u7b49\u3002\u5176\u4e2d\uff0c\u5728\u6587\u4ef6\u9876\u90e8\u58f0\u660e\u7f16\u7801\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/strong>\u5728Python\u811a\u672c\u7684\u7b2c\u4e00\u884c\u6216\u7b2c\u4e8c\u884c\u6dfb\u52a0\u4e00\u4e2a\u7279\u6b8a\u7684\u6ce8\u91ca\uff0c\u6307\u793a\u89e3\u91ca\u5668\u4f7f\u7528\u7279\u5b9a\u7684\u7f16\u7801\u6765\u8bfb\u53d6\u6e90\u4ee3\u7801\u6587\u4ef6\u3002\u8fd9\u901a\u5e38\u7528\u4e8e\u5904\u7406\u975eASCII\u5b57\u7b26\u3002\u4ee5\u4e0b\u662f\u5173\u4e8e\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u548c\u5904\u7406\u7f16\u7801\u7684\u8be6\u7ec6\u6307\u5357\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5728\u6587\u4ef6\u9876\u90e8\u58f0\u660e\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u5728Python\u6587\u4ef6\u7684\u9876\u90e8\u58f0\u660e\u6587\u4ef6\u7f16\u7801\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u505a\u6cd5\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u975eASCII\u5b57\u7b26\u65f6\u3002Python\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u7136\u800c\uff0c\u5f53\u9700\u8981\u660e\u786e\u6307\u5b9a\u5176\u4ed6\u7f16\u7801\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u683c\u5f0f\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\u4e2a\u58f0\u660e\u901a\u5e38\u653e\u5728\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\u6216\u7b2c\u4e8c\u884c\u3002\u5982\u679c\u811a\u672c\u4e2d\u5305\u542b\u975eASCII\u5b57\u7b26\uff0c\u660e\u786e\u6307\u5b9a\u7f16\u7801\u53ef\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u7f16\u7801\u53c2\u6570\u6253\u5f00\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6I\/O\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u5728<code>open()<\/code>\u51fd\u6570\u4e2d\u6307\u5b9a\u7f16\u7801\u53c2\u6570\u3002\u8fd9\u6837\u505a\u786e\u4fdd\u4e86\u6587\u4ef6\u7684\u6b63\u786e\u7f16\u7801\u548c\u89e3\u7801\u3002<\/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 f:<\/p>\n<p>    content = f.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6307\u5b9a<code>encoding=&#39;utf-8&#39;<\/code>\u786e\u4fdd\u4e86\u6587\u4ef6\u88ab\u4ee5UTF-8\u7f16\u7801\u8bfb\u53d6\u3002\u82e5\u4e0d\u6307\u5b9a\u7f16\u7801\uff0cPython\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u7684\u7cfb\u7edf\u7f16\u7801\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u8de8\u5e73\u53f0\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6307\u5b9a\u9ed8\u8ba4\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801\u6765\u5f71\u54cd\u6574\u4e2aPython\u7a0b\u5e8f\u7684\u7f16\u7801\u884c\u4e3a\u3002\u8fd9\u901a\u5e38\u6d89\u53ca\u5230\u4fee\u6539Python\u7684\u5168\u5c40\u7f16\u7801\u8bbe\u7f6e\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5e76\u4e0d\u63a8\u8350\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4\u4e0d\u53ef\u9884\u6d4b\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u67e5\u770b\u548c\u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(sys.getdefaultencoding())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff0c\u4f46\u8fd9\u901a\u5e38\u9700\u8981\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>importlib<\/code>\uff0c\u5e76\u4e14\u4ec5\u5728\u7279\u5b9a\u73af\u5883\u4e2d\u6709\u6548\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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>\u5e94\u8c28\u614e\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u4f1a\u5f71\u54cd\u6240\u6709\u6a21\u5757\u7684\u7f16\u7801\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u5b57\u7b26\u4e32\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u7406\u89e3\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u4ece\u4e00\u79cd\u7f16\u7801\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7f16\u7801\u662f\u5f88\u91cd\u8981\u7684\u3002\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u6709\u4e24\u79cd\u4e3b\u8981\u7c7b\u578b\uff1a<code>str<\/code>\uff08\u6587\u672c\u5b57\u7b26\u4e32\uff09\u548c<code>bytes<\/code>\uff08\u5b57\u8282\u5b57\u7b26\u4e32\uff09\u3002\u5728\u4e24\u8005\u4e4b\u95f4\u8f6c\u6362\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u8f6c\u6362\u6587\u672c\u5b57\u7b26\u4e32\u4e3a\u5b57\u8282\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, \u4e16\u754c&quot;<\/p>\n<p>bytes_string = text.encode(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f6c\u6362\u5b57\u8282\u5b57\u7b26\u4e32\u4e3a\u6587\u672c\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">decoded_text = bytes_string.decode(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7406\u89e3\u548c\u6b63\u786e\u4f7f\u7528\u7f16\u7801\u5728\u5904\u7406\u56fd\u9645\u5316\u548c\u591a\u8bed\u8a00\u652f\u6301\u7684\u5e94\u7528\u7a0b\u5e8f\u65f6\u5c24\u5176\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u5e38\u89c1\u7f16\u7801\u9519\u8bef<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f16\u7801\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u5982<code>UnicodeDecodeError<\/code>\u548c<code>UnicodeEncodeError<\/code>\u3002\u8fd9\u4e9b\u9519\u8bef\u901a\u5e38\u662f\u7531\u4e8e\u5c1d\u8bd5\u5728\u4e0d\u6b63\u786e\u7684\u7f16\u7801\u4e0b\u8bfb\u53d6\u6216\u5199\u5165\u5b57\u7b26\u4e32\u5f15\u8d77\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u786e\u4fdd\u7f16\u7801\u4e00\u81f4<\/strong>\uff1a\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u76f8\u540c\u7684\u7f16\u7801\u3002<\/li>\n<li><strong>\u4f7f\u7528\u9519\u8bef\u5904\u7406\u7b56\u7565<\/strong>\uff1a\u5728\u7f16\u7801\u6216\u89e3\u7801\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u9519\u8bef\u5904\u7406\u7b56\u7565\uff0c\u5982<code>errors=&#39;ignore&#39;<\/code>\u6216<code>errors=&#39;replace&#39;<\/code>\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">text = &quot;Hello, \u4e16\u754c&quot;<\/p>\n<p>try:<\/p>\n<p>    bytes_string = text.encode(&#39;ascii&#39;, errors=&#39;ignore&#39;)<\/p>\n<p>except UnicodeEncodeError as e:<\/p>\n<p>    print(f&quot;Encoding error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u9519\u8bef\u5904\u7406\u7b56\u7565\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u7f16\u7801\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u5de5\u5177\u548c\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u590d\u6742\u7f16\u7801\u4efb\u52a1\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9bPython\u5e93\u6765\u7b80\u5316\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c<code>chardet<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u6587\u4ef6\u7684\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 f:<\/p>\n<p>    raw_data = f.read()<\/p>\n<p>result = chardet.detect(raw_data)<\/p>\n<p>encoding = result[&#39;encoding&#39;]<\/p>\n<p>print(f&quot;The detected encoding is: {encoding}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u81ea\u52a8\u8bc6\u522b\u6587\u4ef6\u7684\u7f16\u7801\uff0c\u4ece\u800c\u9009\u62e9\u6b63\u786e\u7684\u89e3\u7801\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<ol>\n<li><strong>\u59cb\u7ec8\u663e\u5f0f\u6307\u5b9a\u7f16\u7801<\/strong>\uff1a\u5728\u6587\u4ef6I\/O\u4e2d\u59cb\u7ec8\u663e\u5f0f\u6307\u5b9a\u7f16\u7801\uff0c\u907f\u514d\u4f7f\u7528\u9ed8\u8ba4\u7f16\u7801\u3002<\/li>\n<li><strong>\u4f7f\u7528UTF-8<\/strong>\uff1a\u5c3d\u53ef\u80fd\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u56e0\u4e3a\u5b83\u662f\u517c\u5bb9\u6027\u6700\u597d\u7684\u7f16\u7801\u65b9\u5f0f\u3002<\/li>\n<li><strong>\u5904\u7406\u5f02\u5e38<\/strong>\uff1a\u5728\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u4e2d\u5904\u7406\u6f5c\u5728\u7684\u5f02\u5e38\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u7f16\u7801\u5904\u7406\u6280\u672f\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u7f16\u7801\u9519\u8bef\uff0c\u5e76\u786e\u4fddPython\u7a0b\u5e8f\u5728\u5404\u79cd\u8bed\u8a00\u73af\u5883\u4e2d\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u901a\u8fc7<code>open()<\/code>\u51fd\u6570\u7684<code>encoding<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5e0c\u671b\u4ee5UTF-8\u7f16\u7801\u6253\u5f00\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n    content = file.read()\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u786e\u4fdd\u60a8\u8bfb\u53d6\u6587\u4ef6\u65f6\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\uff0c\u907f\u514d\u51fa\u73b0\u7f16\u7801\u9519\u8bef\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9b\u5e38\u89c1\u7684\u7f16\u7801\u683c\u5f0f\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u7f16\u7801\u683c\u5f0f\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8eUTF-8\u3001UTF-16\u3001ISO-8859-1\u3001GBK\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u683c\u5f0f\u975e\u5e38\u91cd\u8981\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c\u65f6\u3002\u4f8b\u5982\uff0cUTF-8\u662f\u5904\u7406\u56fd\u9645\u5316\u6587\u672c\u7684\u63a8\u8350\u7f16\u7801\u683c\u5f0f\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u6240\u6709Unicode\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u7f16\u7801\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f16\u7801\u9519\u8bef\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7<code>errors<\/code>\u53c2\u6570\u6765\u63a7\u5236\u9519\u8bef\u5904\u7406\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>errors=&#39;ignore&#39;<\/code>\u53ef\u4ee5\u5ffd\u7565\u65e0\u6cd5\u89e3\u7801\u7684\u5b57\u7b26\uff0c\u800c\u4f7f\u7528<code>errors=&#39;replace&#39;<\/code>\u5219\u4f1a\u7528\u4e00\u4e2a\u66ff\u4ee3\u5b57\u7b26\uff08\u5982\u95ee\u53f7\uff09\u66ff\u6362\u6389\u8fd9\u4e9b\u5b57\u7b26\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;, errors=&#39;replace&#39;) as file:\n    content = file.read()\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u5355\u4e2a\u9519\u8bef\u800c\u5d29\u6e83\uff0c\u540c\u65f6\u4fdd\u7559\u5c3d\u53ef\u80fd\u591a\u7684\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u7f6e\u7f16\u7801\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\uff1a\u5728\u6587\u4ef6\u9876\u90e8\u58f0\u660e\u7f16\u7801\u3001\u4f7f\u7528\u7f16\u7801\u53c2\u6570\u6253\u5f00\u6587\u4ef6\u3001\u6307\u5b9a\u9ed8\u8ba4\u7f16\u7801\u3001\u5904\u7406\u5b57\u7b26\u4e32 [&hellip;]","protected":false},"author":3,"featured_media":949469,"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\/949464"}],"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=949464"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949464\/revisions"}],"predecessor-version":[{"id":949470,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949464\/revisions\/949470"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/949469"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=949464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=949464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=949464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}