{"id":204504,"date":"2025-05-29T13:23:50","date_gmt":"2025-05-29T05:23:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204504\/"},"modified":"2025-05-29T13:23:50","modified_gmt":"2025-05-29T05:23:50","slug":"python-%e5%88%b6%e4%bd%9c%e7%bd%91%e9%a1%b5%e9%81%87%e5%88%b0-unicodedecodeerror-%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204504\/","title":{"rendered":"Python \u5236\u4f5c\u7f51\u9875\u9047\u5230 UnicodeDecodeError \u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5236\u4f5c\u7f51\u9875\u9047\u5230 UnicodeDecodeError \u5982\u4f55\u89e3\u51b3\uff1f<\/h1>\n<p>\u76ee\u524d\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPython \u5236\u4f5c\u7f51\u9875\u9047\u5230 UnicodeDecodeError \u5982\u4f55\u89e3\u51b3\uff1f\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241109\/1731125927672ee2a75f45a.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Python \u5236\u4f5c\u7f51\u9875\u9047\u5230\u7684 UnicodeDecodeError<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 Python \u5236\u4f5c\u7f51\u9875\u65f6\uff0c\u9047\u5230 &#8220;UnicodeDecodeError: &#8216;utf-8&#8217; codec can&#8217;t decode byte 0xbf in position 0: invalid start byte&#8221; \u9519\u8bef\u3002<\/p>\n<p>\u9519\u8bef\u4ee3\u7801\u8868\u660e\u6587\u4ef6\u7f16\u7801\u53ef\u80fd\u5b58\u5728\u95ee\u9898\u3002\u5efa\u8bae\u68c0\u67e5\u5e76\u786e\u4fdd\u4ee3\u7801\u548c\u5176\u4ed6\u76f8\u5173\u6587\u4ef6\uff08\u4f8b\u5982 HTML\uff09\u4f7f\u7528\u4e00\u81f4\u7684 UTF-8 \u7f16\u7801\u3002<\/p>\n<p>\u5982\u679c\u7f16\u7801\u683c\u5f0f\u6b63\u786e\uff0c\u5219\u53ef\u80fd\u662f Python \u7248\u672c\u7684\u95ee\u9898\u3002\u5c1d\u8bd5\u4f7f\u7528\u5176\u4ed6 Python \u7248\u672c\uff0c\u4f8b\u5982\u5c06 Python 3.8 \u7248\u672c\u66f4\u6362\u4e3a Python 3.5 \u7248\u672c\uff0c\u770b\u770b\u95ee\u9898\u662f\u5426\u5f97\u5230\u89e3\u51b3\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4ee3\u7801\u4e2d\u9700\u8981\u6ce8\u610f\u7684\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u786e\u4fdd HTML \u6587\u4ef6\u4ee5 UTF-8 \u7f16\u7801\u4fdd\u5b58\u3002<\/li>\n<li>\u5728 Python \u811a\u672c\u4e2d\u6dfb\u52a0 encoding=&#8221;utf-8&#8243; \u5230 open() \u51fd\u6570\u4e2d\u4ee5\u663e\u5f0f\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u3002<\/li>\n<li>\u68c0\u67e5\u6d89\u53ca\u6587\u4ef6\u8bfb\u53d6\u6216\u5199\u5165\u7684\u4efb\u4f55\u5176\u4ed6\u5e93\u6216\u7ec4\u4ef6\uff0c\u786e\u4fdd\u5b83\u4eec\u4e5f\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u8fd9\u4e9b\u5efa\u8bae\u90fd\u65e0\u6cd5\u89e3\u51b3\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u5220\u9664\u4e34\u65f6\u6587\u4ef6\u548c\u7f13\u5b58\u3002<\/li>\n<li>\u91cd\u65b0\u5b89\u88c5 Python \u548c\u76f8\u5173\u8f6f\u4ef6\u5305\u3002<\/li>\n<li>\u5728\u66f4\u7b80\u5355\u7684\u6d4b\u8bd5\u4ee3\u7801\u4e2d\u91cd\u73b0\u9519\u8bef\u4ee5\u8fdb\u4e00\u6b65\u9694\u79bb\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPython \u5236\u4f5c\u7f51\u9875\u9047\u5230 UnicodeDecodeError \u5982\u4f55\u89e3\u51b3\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5236\u4f5c\u7f51\u9875\u9047\u5230 Un&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204504","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204504"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204504\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}