{"id":35,"date":"2022-10-31T12:46:28","date_gmt":"2022-10-31T04:46:28","guid":{"rendered":"http:\/\/www.guoqw.cn\/?p=35"},"modified":"2022-10-31T12:46:28","modified_gmt":"2022-10-31T04:46:28","slug":"python3%e6%8a%a5%e9%94%99%e5%a4%84%e7%90%86%ef%bc%9aunicodeencodeerror-ascii-codec-cant-encode-characters-in-position-0-1","status":"publish","type":"post","link":"http:\/\/www.guoqw.cn\/?p=35","title":{"rendered":"PYTHON3\u62a5\u9519\u5904\u7406\uff1aUNICODEENCODEERROR: \u2018ASCII\u2018 CODEC CAN\u2018T ENCODE CHARACTERS IN POSITION 0-1"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u80cc\u666f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9700\u8981\u8bfb\u53d6pdf\u4e2d\u7684\u6570\u636e\uff0c\u4fdd\u5b58\u5230\u7cfb\u7edf\u4e2d\uff0c\u7cfb\u7edf\u91c7\u7528php\u5f00\u53d1\uff0c\u7531\u4e8ePHP\u6ca1\u6709\u5408\u9002\u7684\u8bfb\u53d6pdf\u6269\u5c55\u5e93\u3002 \u51b3\u5b9a\u91c7\u7528python\u8bfb\u53d6\u3002\u4f46\u5728\u63a5\u6536\u8fd4\u56de\u6570\u636e\u65f6\uff0c\u62a5\u7f16\u7801\u9519\u8bef<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u5f00\u59cb\u4e0d\u613f\u610f\u4f7f\u7528Python\uff0c\u4e00\u5927\u539f\u56e0\u662f\u56e0\u4e3aPython2\u9ed8\u8ba4\u4f7f\u7528ASCII\u7f16\u7801\u5904\u7406\u4e2d\u6587\u53ef\u4ee5\u8bf4\u662f\u4e00\u4ef6\u75db\u82e6\u7684\u4e8b\u60c5\u3002\u4ec5\u4ece\u66f4\u6362\u9ed8\u8ba4\u7f16\u7801\u4e00\u9879\u53d8\u6362\uff0c\u5c31\u53ef\u4ee5\u8bf4Python3\u548cPython2\u4e0d\u7b97\u540c\u4e00\u95e8\u8bed\u8a00\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Python3\u66f4\u6362\u4e3a\u9ed8\u8ba4\u4f7f\u7528Unicode\uff08utf-8\uff09\u7f16\u7801\uff0c\u4e00\u76f4\u4f7f\u7528\u4e0b\u6765\u518d\u6ca1\u6709\u9047\u5230\u7f16\u7801\u95ee\u9898\u5e26\u6765\u7684\u56f0\u6320\uff0c\u4f3c\u4e4e\u7f16\u7801\u95ee\u9898\u5728Python3\u65f6\u4ee3\u5c31\u8be5\u5b8c\u5168\u6d88\u5931\u7684\u3002\u4f46\u8fd9\u4e24\u5929\u9047\u5230\u4e86\u4e00\u4e2a\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8c03\u7528pdfplumber\u5e93\uff0c\u51fa\u73b0\u4e86\u4e00\u4e2a\u5f02\u5e38\u62a5\u9519\u7c7b\u4f3c\u5982UnicodeEncodeError: &#8216;ascii&#8217; codec can&#8217;t encode characters in position 0-1: ordinal not in range(128)\uff0c\u51e0\u7ecf\u6392\u67e5\u4e4b\u4e0b\u53d1\u73b0\u53ea\u8981\u8be5\u5e93\u8fd4\u56de\u7ed3\u679c\u5305\u542b\u4e2d\u6587\uff0c\u6211\u8fd9\u8fb9\u4f7f\u7528print()\u6253\u5370\u8be5\u7ed3\u679c\u65f6\u5c31\u4f1a\u51fa\u73b0\u8be5\u5f02\u5e38\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001\u539f\u56e0\u5206\u6790<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1 \u6570\u636e\u8981\u7ecf\u8fc7\u7f16\u7801\u624d\u80fd\u4f20\u8f93<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u77e5\u9053\u6570\u636e\u5728\u7f51\u7edc\u4e0a\u4f20\u8f93\u65f6\uff0c\u9700\u8981\u5148\u7f16\u7801\uff1b\u5e73\u65f6\u6211\u4eec\u53ef\u80fd\u5e76\u4e0d\u6ce8\u610f\uff0c\u4f46\u73b0\u5728\u8981\u660e\u786e\uff0c\u7f16\u7801\u7684\u539f\u56e0\u4e0d\u5728\u4e8e\u7f51\u7edc\u800c\u5728\u4e8e\u4f20\u8f93\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">print()\u76f8\u5f53\u4e8e\u628a\u5b57\u7b26\u4e32\u4ece\u5185\u5b58\u4f20\u8f93\u5230\u4e86tty\u4e0a\uff0c\u6240\u4ee5print()\u662f\u9700\u8981encode()\u52a8\u4f5c\u7684\uff1b\u5e73\u65f6\u6211\u4eecprint()\u65f6\u4e00\u822c\u90fd\u4e0d\u9700\u8981encode()\uff0c\u53ea\u662f\u56e0\u4e3a\u5f53print()\u68c0\u6d4b\u5230\u4f20\u6765\u7684\u53c2\u6570\u662f\u4e0d\u662fbyte\u7c7b\u578b\u65f6\u81ea\u52a8\u8fdb\u884c\u4e86\u7f16\u7801\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2 print()\u4f7f\u7528\u4f55\u79cd\u7f16\u7801<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Python3\u9ed8\u8ba4\u4f7f\u7528\u7684\u662futf-8\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7sys.getdefaultencoding()\u8fdb\u884c\u786e\u8ba4\u3002\u4f46\u8fd9\u53ea\u662f\u9ed8\u8ba4\uff0c\u5f53\u7cfb\u7edf\u914d\u7f6e\u4e86LC_ALL\u3001LC_CTYPE\u3001LANG\u7b49\u73af\u5883\u53d8\u91cf\u65f6\uff08\u4e09\u8005\u4f18\u5148\u7ea7\u4ece\u9ad8\u5230\u4f4e\uff09\uff0cPython3\u91c7\u7528\u8fd9\u4e9b\u53d8\u91cf\u914d\u7f6e\u7684\u7f16\u7801\uff1b\u5982\u679c\u8fd9\u4e9b\u53d8\u91cf\u914d\u7f6e\u7684\u662futf-8\u90a3Python3\u7528\u7684\u5c31\u8fd8\u662futf-8\uff0c\u4f46\u5982\u679c\u4e0d\u662futf-8\u90a3Python3\u6240\u7528\u7684\u4e5f\u5c31\u4e0d\u662futf-8\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u524d\u4f7f\u7528\u7684\u7f16\u7801\u53ef\u901a\u8fc7sys.getfilesystemencoding()\u83b7\u53d6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001\u573a\u666f\u590d\u73b0<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u8fd9\u91cc\u76f4\u63a5\u4ee5\u6253\u5370\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26\u4e32\u4f5c\u4e3a\u6f14\u793a\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff08\u6211\u8fd9\u91cc\u4fdd\u5b58\u6210test_encode.py\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\n \nclass TestEncode():\n    def __int__(self):\n        pass\n \n    def main_logic(self):\n        # \u6253\u5370\u8bed\u8a00\u9ed8\u8ba4\u7f16\u7801\n        print(f\"defaultencoding--{sys.getdefaultencoding()}\")\n        # \u6253\u5370\u7cfb\u7edf\u914d\u7f6e\u7684\u7f16\u7801\n        print(f\"filesystemencoding--{sys.getfilesystemencoding()}\")\n \n        # \u6700\u540e\u5c1d\u8bd5\u6253\u5370\u4e2d\u6587\n        print(\"\u4e2d\u6587\")\n \nif __name__ == \"__main__\":\n    obj = TestEncode()\n    obj.main_logic()<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-left wp-block-paragraph\">shell\u4f9d\u6b21\u6267\u884c\u5982\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u67e5\u770b\u5f53\u524d\u7f16\u7801\u60c5\u51b5\nlocale\n# \u786e\u8ba4\u5728utf-8\u60c5\u51b5\u4e0b\u6253\u5370\u4e2d\u6587\u65e0\u8bef\npython3 test_encode.py\n \n# \u8bbe\u7f6eLC_TYPE\uff0cC\u4ee3\u8868ASCII\nexport LC_CTYPE=\"C\"\n# \u67e5\u770b\u5f53\u524d\u7f16\u7801\u60c5\u51b5\nlocale\n# \u518d\u6b21\u8fd0\u884c\uff0c\u786e\u8ba4\u7cfb\u7edf\u7f16\u7801\u5df2\u6539\u53d8\uff0c\u5e76\u51fa\u73b0\u7f16\u7801\u9519\u8bef\npython3 test_encode.py<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u7ec8\u7ed3\u679c\u5982\u4e0b\uff0c\u5728\u7cfb\u7edf\u7f16\u7801\u914d\u7f6e\u4e3autf-8\u65f6\u6253\u5370\u6b63\u5e38\uff0c\u5728\u7cfb\u7edf\u7f16\u7801\u6539\u4e3aC\uff08\u5373ASCII\uff09\u540e\u6253\u5370\u62a5\u7f16\u7801\u5f02\u5e38\uff08\u4e0d\u8fc7\u6211\u5728root\u7528\u6237\u73af\u5883\u4fee\u6539\u7f16\u7801\u4e00\u76f4\u4e0d\u6210\u529f\uff0c\u4e0d\u61c2\u6211\u7535\u8111\u6709\u70b9\u95ee\u9898\u8fd8\u662f\u4ec0\u4e48\u539f\u56e0\uff09\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"985\" height=\"701\" src=\"http:\/\/www.guoqw.cn\/wp-content\/uploads\/2022\/10\/image.png\" alt=\"\" class=\"wp-image-36\" srcset=\"http:\/\/www.guoqw.cn\/wp-content\/uploads\/2022\/10\/image.png 985w, http:\/\/www.guoqw.cn\/wp-content\/uploads\/2022\/10\/image-300x214.png 300w, http:\/\/www.guoqw.cn\/wp-content\/uploads\/2022\/10\/image-768x547.png 768w\" sizes=\"auto, (max-width: 985px) 100vw, 985px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u56db\u3001\u89e3\u51b3\u529e\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u65b9\u6cd5\u4e00\uff1a\u8bbe\u7f6e\u7cfb\u7edf\u6240\u7528\u7f16\u7801\u4e3autf-8<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e2\u7136\u95ee\u9898\u662f\u7cfb\u7edf\u8bed\u8a00\u7f16\u7801\u6240\u81f4\uff0c\u90a3\u6211\u4eec\u5728\u8fd0\u884c\u524d\u628aLC_ALL\u7b49\u73af\u5883\u53d8\u91cf\u6539\u4e3autf-8\u5373\u53ef\uff1b\u5b9e\u73b0\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u6700\u7b80\u5355\u7684\u5c31\u662f\u5728\u8fd0\u884c\u524dexport\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export LC_ALL=\"en_US.utf8\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528PYTHONIOENCODING<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PYTHONIOENCODING=ascii python test_encode.py<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u65b9\u6cd5\u4e09\uff1a\u5148\u7f16\u597d\u7801\u518d\u4ea4\u7ed9print()\uff08\u4e0d\u5b9e\u7528\uff1f\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u5b9e\u56de\u987e\u6211\u4eec\u7b2c\u4e8c\u5927\u8282\u7684\u5206\u6790\uff0c\u4e4b\u540e\u4ee5\u9700\u8981\u627e\u7cfb\u7edf\u7684\u7f16\u7801\u662f\u56e0\u4e3aprint()\u6536\u5230\u672a\u7ecf\u7f16\u7801\u7684\u5185\u5bb9\uff0c\u6240\u4ee5\u66f4\u76f4\u63a5\u7684\u5904\u7406\u529e\u6cd5\u662f\u6211\u4eec\u5148\u7f16\u597d\u7801\u518d\u4ea4\u7ed9print()\u5c31\u597d\u4e86\u3002\uff08\u4e0d\u8fc7\u4ea4\u7ed9print\u65f6\u662fbyte\u7c7b\u578b\uff0c\u6700\u540eprint\u8f93\u51fa\u7684\u4e5f\u662fbyte\u7c7b\u578b\uff1f\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"\u4e2d\u6587\".encode(encoding=\"utf-8\"))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u65b9\u6cd5\u56db\uff1a\u76f4\u63a5\u5728Python\u4e2d\u91cd\u65b0\u8bbe\u7f6e\u6807\u51c6\u8f93\u51fa\u7f16\u7801<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u6cd5\u76f4\u63a5\u5728Python\u4ee3\u7801\u4e2d\u6539\uff0c\u662f\u4e0ePython\u6700\u5951\u5408\u7684\u505a\u6cd5\uff0c\u811a\u672c\u6587\u4ef6\u5f62\u5f0f\u65f6\u7279\u522b\u63a8\u8350<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sysimport codecssys.stdout = codecs.getwriter(\"utf-8\")(sys.stdout.detach())print(\"\u4e2d\u6587\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u9700\u8981\u8bfb\u53d6pdf\u4e2d\u7684\u6570\u636e\uff0c\u4fdd\u5b58\u5230\u7cfb\u7edf\u4e2d\uff0c\u7cfb\u7edf\u91c7&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.guoqw.cn\/index.php?rest_route=\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.guoqw.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.guoqw.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.guoqw.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.guoqw.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=35"}],"version-history":[{"count":1,"href":"http:\/\/www.guoqw.cn\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":37,"href":"http:\/\/www.guoqw.cn\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions\/37"}],"wp:attachment":[{"href":"http:\/\/www.guoqw.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.guoqw.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.guoqw.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}