{"id":1042,"date":"2017-11-08T23:06:11","date_gmt":"2017-11-08T15:06:11","guid":{"rendered":"http:\/\/192.168.1.20\/?p=1042"},"modified":"2017-11-08T23:06:11","modified_gmt":"2017-11-08T15:06:11","slug":"{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}e8{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}a7{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c0","status":"publish","type":"post","link":"http:\/\/alextop.top\/?p=1042","title":{"rendered":"\u89e3\u51b3Python2.7\u7684UnicodeEncodeError: \u2018ascii\u2019 codec can\u2019t encode\u5f02\u5e38\u9519\u8bef"},"content":{"rendered":"<p>\u4ee3\u7801\u4e0a\u9762\u5e94\u8be5\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\uff0cPython\u4f7f\u7528\u7684\u662f\u7248\u672c2.7\uff0c\u4f46\u662f\u5728\u8fd0\u884c\u7684\u65f6\u5019\u51fa\u73b0\u4e86\u5f02\u5e38\u9519\u8befUnicodeEncodeError\uff1a<\/p>\n<p>Traceback (most recent call last):<br \/>\n  File &#8220;makedb.py&#8221;, line 33, in<br \/>\n    main()<br \/>\n  File &#8220;makedb.py&#8221;, line 30, in main<br \/>\n    fp.write(row[1])<br \/>\nUnicodeEncodeError: &#8216;ascii&#8217; codec can&#8217;t encode characters in position 0-78: ordinal not in range(128)<br \/>\n\u672c\u6765\u4ee5\u4e3a\u6570\u636e\u8bfb\u53d6\u9519\u8bef\uff0c\u6211\u7279\u5c06fp.write\u6539\u6210print\uff0c\u7ed3\u679c\u6570\u636e\u5168\u90e8\u8bfb\u53d6\u5e76\u663e\u793a\u5728\u547d\u4ee4\u63a7\u5236\u53f0\u4e0a\u4e86\uff0c\u8bc1\u660e\u4ee3\u7801\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u4ed4\u7ec6\u770b\u4e86\u4e0b\u5f02\u5e38\u4fe1\u606f\uff0c\u8c8c\u4f3c\u662f\u56e0\u4e3a\u7f16\u7801\u95ee\u9898\uff1aUnicode\u7f16\u7801\u4e0eASCII\u7f16\u7801\u7684\u4e0d\u517c\u5bb9\uff0c\u5176\u5b9e\u8fd9\u4e2aPython\u811a\u672c\u6587\u4ef6\u662f\u7531utf-8\u7f16\u7801\u7684\uff0c\u540c\u65f6SQlite3\u6570\u636e\u5e93\u5b58\u53d6\u7684\u4e5f\u662fUTF-8\u683c\u5f0f\uff0cPython\u9ed8\u8ba4\u73af\u5883\u7f16\u7801\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\uff1a<\/p>\n<p>import sys<br \/>\nprint sys.getdefaultencoding()<br \/>\n# &#8216;ascii&#8217;<br \/>\n\u57fa\u672c\u4e0a\u662fascii\u7f16\u7801\u65b9\u5f0f\uff0c\u7531\u6b64Python\u81ea\u7136\u8c03\u7528ascii\u7f16\u7801\u89e3\u7801\u7a0b\u5e8f\u53bb\u5904\u7406\u5b57\u7b26\u6d41\uff0c\u5f53\u5b57\u7b26\u6d41\u4e0d\u5c5e\u4e8eascii\u8303\u56f4\u5185\uff0c\u5c31\u4f1a\u629b\u51fa\u5f02\u5e38\uff08ordinal not in range(128)\uff09\u3002<\/p>\n<p>\u89e3\u51b3\u7684\u65b9\u6848\u5f88\u7b80\u5355\uff0c\u4fee\u6539\u9ed8\u8ba4\u7684\u7f16\u7801\u6a21\u5f0f\uff0c\u5f88\u591a\u670b\u53cb\u4f1a\u60f3\u5230setdefaultencoding\uff0c\u662f\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7sys.setdefaultencoding(\u2018utf-8\u2019)\u6765\u5c06\u5f53\u524d\u7684\u5b57\u7b26\u5904\u7406\u6a21\u5f0f\u4fee\u6539\u4e3autf-8\u7f16\u7801\u6a21\u5f0f\uff0c\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5355\u7eaf\u8fd9\u4e48\u8c03\u7528\u7684\u8bdd\uff0cPython\u4f1a\u629b\u51fa\u4e00\u4e2aAttributeError\u5f02\u5e38\uff1a<\/p>\n<p>Traceback (most recent call last):<br \/>\n  File &#8220;<stdin>&#8220;, line 1, in <module><br \/>\nAttributeError: &#8216;module&#8217; object has no attribute &#8216;setdefaultencoding&#8217;<br \/>\n\u7adf\u7136\u8bf4sys\u6ca1\u6709setdefaultencoding\u7684\u65b9\u6cd5\uff0c\u5176\u5b9esys\u662f\u6709\u8fd9\u4e2a\u65b9\u6cd5\u7684\uff0c\u4f46\u662f\u8981\u8bf7\u51fa\u5979\u8001\u4eba\u5bb6\u9700\u8981\u8c03\u7528\u4e00\u6b21reload(sys)\uff0c\u5f88\u5947\u602a\uff0c\u662f\u4e48\uff1f\u5982\u679c\u6709\u8c01\u77e5\u9053\u539f\u56e0\u7684\u8bdd\uff0c\u8fd8\u671b\u4e0d\u541d\u8d50\u6559\u3002<\/p>\n<p>import sys<br \/>\nreload(sys)<br \/>\nsys.setdefaultencoding(&#8216;utf-8&#8217;)<br \/>\n\u597d\u4e86\uff0c\u901a\u8fc7\u4e0a\u9762\u77ed\u77ed\u7684\u4e09\u884c\uff0c\u6211\u4eec\u7b97\u662f\u5f88\u597d\u7684\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\u4e86\uff0c\u540c\u6837\u7684\u65b9\u5f0f\u4e5f\u53ef\u4ee5\u5e94\u7528\u5230UnicodeDecodeError\u4e0a\u3002\u5f53\u7136\u8fd9\u4e2a\u6280\u5de7\u6765\u81ea\u4e8e\u7f51\u7edc\uff0c\u6211\u8fd8\u627e\u5230\u5176\u4ed6\u7279\u522b\u7684\u529e\u6cd5\uff0c\u4f46\u662f\u611f\u89c9\u8fd8\u662f\u8fd9\u4e2a\u6bd4\u8f83\u9760\u8c31\uff0c\u6709\u7ae5\u978b\u8bf4\uff1a\u6211\u4eec\u5c06Python 2.x\u7cfb\u5217\u5347\u7ea7\u5230Python 3.x\u7cfb\u5217\u5c31\u53ef\u4ee5\u4e86\uff0c\u5c0f\u5c0f\u7684\u95ee\u9898\u72af\u4e0d\u7740\u5347\u7ea7\u5427\uff0c\u6bd5\u7adf2\u52303\u8fd8\u662f\u8981\u6709\u4e2a\u8fc7\u6e21\u7684\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u5c06\u6587\u7ae0\u4e00\u5f00\u59cb\u7684\u4ee3\u7801\u66f4\u6539\u5982\u4e0b\uff1a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee3\u7801\u4e0a\u9762\u5e94\u8be5\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\uff0cPy&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-1042","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/1042"}],"collection":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1042"}],"version-history":[{"count":1,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/1042\/revisions"}],"predecessor-version":[{"id":1043,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/1042\/revisions\/1043"}],"wp:attachment":[{"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1042"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}