{"id":175138,"date":"2024-05-08T18:45:34","date_gmt":"2024-05-08T10:45:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/175138.html"},"modified":"2024-05-08T18:45:43","modified_gmt":"2024-05-08T10:45:43","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96gb2312%e7%a0%81%ef%bc%8c%e5%b9%b6print%e5%87%ba%e6%b1%89%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/175138.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6gb2312\u7801\uff0c\u5e76print\u51fa\u6c49\u5b57"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27052936\/205e5794-5e79-45c2-93f7-a1296644f091.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6gb2312\u7801\uff0c\u5e76print\u51fa\u6c49\u5b57\" \/><\/p>\n<p><p>\u5728Python\u4e2d\u8bfb\u53d6GB2312\u7f16\u7801\u7684\u6587\u672c\u5e76\u6253\u5370\u51fa\u6c49\u5b57\u6d89\u53ca\u5230\u7f16\u7801\u8f6c\u6362\u7684\u77e5\u8bc6\uff0c\u4ee5\u53caPython\u5185\u7f6e\u7684\u7f16\u89e3\u7801\u5668\u3002<strong>\u80fd\u591f\u51c6\u786e\u8bfb\u53d6GB2312\u7f16\u7801\u7684\u6587\u4ef6\u5e76\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u6c49\u5b57\uff0c\u9700\u8981\u6ce8\u610f\u4e24\u4e2a\u5173\u952e\u6b65\u9aa4\uff1a\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4ee5\u53ca\u5728\u6253\u5370\u65f6\u786e\u4fdd\u63a7\u5236\u53f0\u652f\u6301\u8be5\u7f16\u7801\u3002<\/strong> \u4ee5\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u7684<code>encoding<\/code>\u53c2\u6570\u6765\u6307\u660e\u6587\u4ef6\u4f7f\u7528\u7684\u7f16\u7801\u3002Python\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u9ed8\u8ba4\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u901a\u5e38\u5728\u897f\u65b9\u56fd\u5bb6\u4e3a&#039;UTF-8&#039;\u3002\u5982\u679c\u5c1d\u8bd5\u8bfb\u53d6\u4e00\u4e2aGB2312\u7f16\u7801\u7684\u6587\u4ef6\u800c\u6ca1\u6709\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\uff0cPython\u4f1a\u629b\u51fa\u4e00\u4e2a<code>UnicodeDecodeError<\/code>\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u660e\u786e\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u5bf9\u4e8e\u6b63\u786e\u8bfb\u53d6\u548c\u663e\u793a\u6587\u4ef6\u5185\u5bb9\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001UNDERSTANDING ENCODING<\/h2>\n<\/p>\n<p><p>\u5728\u6df1\u5165\u5982\u4f55\u7528Python\u8bfb\u53d6GB2312\u7f16\u7801\u4e4b\u524d\uff0c\u7406\u89e3\u4ec0\u4e48\u662f\u7f16\u7801\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u5b58\u5728\u4e0d\u540c\u7684\u7f16\u7801\u6807\u51c6\u662f\u5fc5\u8981\u7684\u3002\u7f16\u7801\u662f\u5b57\u7b26\u5728\u8ba1\u7b97\u673a\u4e2d\u7684\u6570\u5b57\u8868\u793a\u3002\u56e0\u4e3a\u8ba1\u7b97\u673a\u53ea\u80fd\u7406\u89e3\u6570\u5b57\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u4e00\u79cd\u65b9\u5f0f\u6765\u5c06\u4eba\u7c7b\u7684\u8bed\u8a00\u8f6c\u6362\u4e3a\u8ba1\u7b97\u673a\u53ef\u4ee5\u7406\u89e3\u7684\u5f62\u5f0f\u3002GB2312\u662f\u9488\u5bf9\u7b80\u4f53\u4e2d\u6587\u8bbe\u8ba1\u7684\u4e00\u79cd\u5b57\u7b26\u7f16\u7801\u6807\u51c6\uff0c\u5b83\u5305\u542b\u4e86\u51e0\u4e4e\u6240\u6709\u5e38\u7528\u7684\u7b80\u4f53\u4e2d\u6587\u6c49\u5b57\u548c\u7b26\u53f7\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u7f16\u7801\u7684\u91cd\u8981\u6027<\/strong>\u4e0d\u4ec5\u5728\u4e8e\u4f7f\u8ba1\u7b97\u673a\u80fd\u591f\u5b58\u50a8\u548c\u5904\u7406\u6587\u5b57\u7b26\u53f7\uff0c\u800c\u4e14\u4e5f\u5173\u4e4e\u4fe1\u606f\u7684\u51c6\u786e\u4ea4\u6362\u3002\u4e0d\u540c\u7684\u7f16\u7801\u6807\u51c6\u5b9a\u4e49\u4e86\u4e0d\u540c\u7684\u6570\u5b57\u5230\u5b57\u7b26\u7684\u6620\u5c04\u89c4\u5219\uff0c\u56e0\u6b64\uff0c\u5728\u8bfb\u53d6\u548c\u5c55\u793a\u6765\u81ea\u4e0d\u540c\u7f16\u7801\u4f53\u7cfb\u7684\u6587\u672c\u65f6\uff0c\u4e86\u89e3\u8fd9\u4e9b\u6587\u672c\u7684\u7f16\u7801\u662f\u5fc5\u9700\u7684\u3002<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001READING GB2312 ENCODED FILES IN PYTHON<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728Python\u4e2d\u8bfb\u53d6GB2312\u7f16\u7801\u7684\u6587\u4ef6\uff0c\u5fc5\u987b\u5728\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a<code>encoding<\/code>\u53c2\u6570\u4e3a&#039;gb2312&#039;\u3002\u8fd9\u544a\u8bc9Python\u4ee5GB2312\u7f16\u7801\u7406\u89e3\u6587\u4ef6\u5185\u5bb9\uff0c\u4ece\u800c\u80fd\u591f\u6b63\u786e\u5730\u89e3\u6790\u6c49\u5b57\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6253\u5f00\u6587\u4ef6\u6307\u5b9a\u7f16\u7801<\/strong>\uff1a\u901a\u8fc7\u5728<code>open<\/code>\u51fd\u6570\u4e2d\u52a0\u5165<code>encoding=&#039;gb2312&#039;<\/code>\u53c2\u6570\uff0c\u7a0b\u5e8f\u5c31\u80fd\u6309\u7167GB2312\u7684\u7f16\u7801\u6807\u51c6\u8bfb\u53d6\u6587\u4ef6\u3002<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u5728\u64cd\u4f5c\u6587\u4ef6\u65f6\uff0c\u603b\u662f\u63a8\u8350\u4f7f\u7528<code>try<\/code>&#8230;<code>except<\/code>\u8bed\u53e5\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u4f8b\u5982\u7f16\u7801\u9519\u8bef\u6216\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#039;example.txt&#039;, &#039;r&#039;, encoding=&#039;gb2312&#039;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        print(content)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;The file was not found.&quot;)<\/p>\n<p>except UnicodeDecodeError:<\/p>\n<p>    print(&quot;Decoding error occurred.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001DEALING WITH ENCODING ERRORS<\/h2>\n<\/p>\n<p><p>\u5f53\u5904\u7406\u7f16\u7801\u4e0d\u4e00\u81f4\u7684\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f16\u7801\u9519\u8bef\u3002\u4e3a\u4e86\u907f\u514d\u56e0\u4e3a\u51e0\u4e2a\u65e0\u6cd5\u8bc6\u522b\u7684\u5b57\u7b26\u800c\u5bfc\u81f4\u6574\u4e2a\u7a0b\u5e8f\u5d29\u6e83\uff0cPython\u7684<code>open<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86<code>errors<\/code>\u53c2\u6570\u6765\u5904\u7406\u8fd9\u7c7b\u95ee\u9898\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u9519\u8bef\u5904\u7406\u7b56\u7565<\/strong>\uff1a<code>errors<\/code>\u53c2\u6570\u53ef\u4ee5\u8bbe\u7f6e\u4e3a&#039;ignore&#039;\u6765\u5ffd\u7565\u9519\u8bef\uff0c\u6216\u8005\u8bbe\u7f6e\u4e3a&#039;replace&#039;\u4ee5\u7279\u5b9a\u7684\u5b57\u7b26\u66ff\u6362\u65e0\u6cd5\u8bc6\u522b\u7684\u5b57\u7b26\uff0c\u8fd9\u6709\u52a9\u4e8e\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/li>\n<li><strong>\u4fdd\u8bc1\u8f93\u51fa\u7684\u901a\u7528\u6027<\/strong>\uff1a\u5373\u4fbf\u80fd\u591f\u6b63\u786e\u8bfb\u53d6GB2312\u7f16\u7801\u7684\u6587\u4ef6\uff0c\u4e5f\u9700\u8981\u786e\u4fdd\u63a7\u5236\u53f0\u6216\u5c55\u793a\u8f93\u51fa\u7684\u73af\u5883\u652f\u6301\u8be5\u7f16\u7801\uff0c\u5426\u5219\u6c49\u5b57\u53ef\u80fd\u663e\u793a\u4e3a\u4e71\u7801\u3002<\/li>\n<\/ul>\n<p><h2>\u56db\u3001PYTHON AND CHARACTER ENCODING BEST PRACTICES<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e0d\u540c\u7f16\u7801\u7684\u6587\u672c\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u907f\u514d\u5e38\u89c1\u95ee\u9898\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6807\u51c6\u5316\u7f16\u7801\u4f7f\u7528<\/strong>\uff1a\u5c3d\u53ef\u80fd\u5c06\u6587\u672c\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801\u5904\u7406\u3002UTF-8\u5177\u5907\u8f83\u597d\u7684\u517c\u5bb9\u6027\u548c\u7075\u6d3b\u6027\uff0c\u662f\u73b0\u4ee3\u7f16\u7a0b\u73af\u5883\u4e2d\u7684\u4e00\u79cd\u901a\u7528\u7f16\u7801\u6807\u51c6\u3002<\/li>\n<li><strong>\u7f16\u7801\u8f6c\u6362<\/strong>\uff1a\u5982\u679c\u9700\u8981\u9891\u7e41\u64cd\u4f5c\u4e0d\u540c\u7f16\u7801\u7684\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Python\u7684\u5185\u7f6e\u5e93\u5982<code>codecs<\/code>\u8fdb\u884c\u7f16\u7801\u95f4\u7684\u8f6c\u6362\u3002\u8fd9\u80fd\u591f\u786e\u4fdd\u6570\u636e\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u4e00\u81f4\u6027\u548c\u53ef\u8bbf\u95ee\u6027\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u6df1\u5165\u4e86\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u5173\u4e8e\u7f16\u7801\u7684\u77e5\u8bc6\u4e0e\u6280\u5de7\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u548c\u5c55\u793aGB2312\u7f16\u7801\u7684\u6587\u672c\u6570\u636e\uff0c\u907f\u514d\u5e38\u89c1\u7684\u7f16\u7801\u95ee\u9898\uff0c\u786e\u4fdd\u4fe1\u606f\u7684\u51c6\u786e\u4f20\u9012\u548c\u5c55\u793a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: \u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6gb2312\u7f16\u7801\u7684\u6587\u4ef6\uff1f<\/strong><\/p>\n<p>A1: \u9996\u5148\uff0c\u4f60\u9700\u8981\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00gb2312\u7f16\u7801\u7684\u6587\u4ef6\u3002\u5728<code>open()<\/code>\u51fd\u6570\u4e2d\uff0c\u4f60\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\u53ca\u6587\u4ef6\u7f16\u7801\u3002\u4f8b\u5982\uff1a<code>file = open(&#039;file.txt&#039;, encoding=&#039;gb2312&#039;)<\/code>\u3002<\/p>\n<p><strong>Q2: Python\u4e2d\u5982\u4f55\u5c06gb2312\u7f16\u7801\u8f6c\u6362\u4e3aUnicode\u7f16\u7801\u5e76\u6253\u5370\u6c49\u5b57\uff1f<\/strong><\/p>\n<p>A2: \u9996\u5148\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>codecs<\/code>\u6a21\u5757\u4e2d\u7684<code>open()<\/code>\u51fd\u6570\u6765\u6253\u5f00gb2312\u7f16\u7801\u7684\u6587\u4ef6\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aUnicode\u7f16\u7801\u3002\u4f8b\u5b50\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">import codecs\n\n# \u6253\u5f00gb2312\u7f16\u7801\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aUnicode\u7f16\u7801\nfile = codecs.open(&#039;file.txt&#039;, &#039;r&#039;, &#039;gb2312&#039;)\n\n# \u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u6253\u5370\u6c49\u5b57\nfor line in file:\n    print(line)\n<\/code><\/pre>\n<p><strong>Q3: \u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528chardet\u5e93\u81ea\u52a8\u68c0\u6d4bgb2312\u7f16\u7801\u5e76\u6253\u5370\u6c49\u5b57\uff1f<\/strong><\/p>\n<p>A3: \u9996\u5148\uff0c\u4f60\u9700\u8981\u4f7f\u7528<code>chardet<\/code>\u5e93\u6765\u68c0\u6d4b\u6587\u4ef6\u7684\u7f16\u7801\u3002\u6b64\u5e93\u53ef\u4ee5\u81ea\u52a8\u68c0\u6d4b\u6587\u4ef6\u7684\u7f16\u7801\u7c7b\u578b\u3002\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>codecs<\/code>\u6a21\u5757\u6765\u6253\u5f00\u6587\u4ef6\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aUnicode\u7f16\u7801\u3002\u4e0b\u9762\u662f\u4e00\u6bb5\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import chardet\nimport codecs\n\n# \u4f7f\u7528chardet\u5e93\u68c0\u6d4b\u6587\u4ef6\u7f16\u7801\nwith open(&#039;file.txt&#039;, &#039;rb&#039;) as f:\n    encoding = chardet.detect(f.read())[&#039;encoding&#039;]\n\n# \u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aUnicode\u7f16\u7801\nfile = codecs.open(&#039;file.txt&#039;, &#039;r&#039;, encoding)\n\n# \u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u6253\u5370\u6c49\u5b57\nfor line in file:\n    print(line)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u662f\u4e09\u79cd\u8bfb\u53d6gb2312\u7f16\u7801\u5e76\u6253\u5370\u6c49\u5b57\u7684\u65b9\u6cd5\uff0c\u5e76\u6839\u636e\u4f60\u7684\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u3002\u8bb0\u4f4f\uff0c\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u59cb\u7ec8\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u4ee5\u907f\u514d\u4e71\u7801\u548c\u5176\u4ed6\u7f16\u7801\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bfb\u53d6GB2312\u7f16\u7801\u7684\u6587\u672c\u5e76\u6253\u5370\u51fa\u6c49\u5b57\u6d89\u53ca\u5230\u7f16\u7801\u8f6c\u6362\u7684\u77e5\u8bc6\uff0c\u4ee5\u53caPython\u5185\u7f6e\u7684\u7f16\u89e3\u7801\u5668\u3002\u80fd [&hellip;]","protected":false},"author":3,"featured_media":175146,"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\/175138"}],"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=175138"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/175138\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/175146"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=175138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=175138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=175138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}