{"id":172332,"date":"2022-04-16T01:01:13","date_gmt":"2022-04-15T17:01:13","guid":{"rendered":"http:\/\/www.idc.net\/help\/172332\/"},"modified":"2022-04-16T01:01:13","modified_gmt":"2022-04-15T17:01:13","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython3%e5%a6%82%e4%bd%95%e6%94%b9%e5%8f%98%e9%bb%98%e8%ae%a4%e7%9a%84ascii%e7%bc%96%e7%a0%81","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/172332\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1apython3\u5982\u4f55\u6539\u53d8\u9ed8\u8ba4\u7684ascii\u7f16\u7801?"},"content":{"rendered":"<p style=\"text-align:center\"><span style=\"font-size: 14px\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/813\/725\/506\/1606449519715504.jpg\" class=\"aligncenter\"><\/span><\/p>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 16px\">\u6211\u4eec\u90fd\u77e5\u9053\u5f88\u591a\u8f6f\u4ef6\u91cc\u4f1a\u81ea\u5e26\u4e00\u4e9b\u5de5\u5177\uff0c\u5927\u90e8\u5206\u4f7f\u7528\u8d77\u6765\u8fd8\u662f\u6bd4\u8f83\u987a\u624b\u7684\uff0c\u4f46\u662f\u603b\u4f1a\u9047\u5230\u4e00\u4e9b\u4e0d\u597d\u7528\u7684\u5de5\u5177\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u5c31\u9700\u8981\u627e\u4e00\u4e9b\u5176\u4ed6\u7684\u65b9\u6cd5\u66ff\u6362\u4e00\u4e0b\u3002\u63d0\u5230<span style=\"font-size: 16px\">utf-8\u5c0f\u4f19\u4f34\u4eec\u80af\u5b9a\u4e0d\u964c\u751f\uff0c\u6211\u4eec\u5728\u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\u6709\u53cd\u590d\u63d0\u53ca\u3002<\/span>python\u7684\u5b57\u7b26\u4e32\u9ed8\u8ba4\u7684\u7684\u662f\u53e6\u4e00\u79cd\u7f16\u7801\uff0c\u5f88\u591a\u4eba\u8fd8\u662f\u559c\u6b22\u7528<span style=\"font-size: 16px\">utf-8\uff0c\u6240\u4ee5\u4eca\u5929\u5c0f\u7f16\u6559\u5927\u5bb6\u6539\u53d8\u9ed8\u8ba4\u7684ascii\u7f16\u7801\u65b9\u6cd5\u3002<\/span><\/span><\/p>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 14px\"><br \/><\/span><\/p>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 14px\">Python\u9ed8\u8ba4\u5b57\u7b26\u4e32\u91c7\u7528\u7684\u662fascii\u7f16\u7801\u65b9\u5f0f\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/span><\/p>\n<pre>python&nbsp;-c&nbsp;\"import&nbsp;sys;&nbsp;print&nbsp;sys.getdefaultencoding()\"\nAscii<\/pre>\n<p style=\"line-height: 1.75em\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1\"><br \/><\/span><\/p>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 14px\">\u53ef\u4ee5\u901a\u8fc7#coding:utf-8 \u6307\u5b9a\u9875\u9762\u9ed8\u8ba4\u7f16\u7801\u4e3autf-8(ps:\u4f46\u7cfb\u7edf\u9ed8\u8ba4\u8fd8\u662fascii)<br \/><\/span><\/p>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 14px\"><span style=\"font-size: 14px;font-family: \u5fae\u8f6f\u96c5\u9ed1\">\u5b57\u7b26\u4e32\u7684\u7f16\u89e3\u7801\u90fd\u662f\u4ee5<\/span>unicode\u4e3a\u4e2d\u95f4\u7f16\u7801,\u65e0\u6cd5\u76f4\u63a5\u5b8c\u6210\u8f6c\u6362,python\u4f1a\u81ea\u52a8\u6309\u5176\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\u65b9\u5f0f\u89e3\u7801\u4e3aunicode\uff0c\u518d\u7f16\u7801\u6210\u53e6\u4e00\u4e2d\u7f16\u7801\u683c\u5f0f<\/span><\/p>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 14px\"><span style=\"font-size: 14px;font-family: \u5fae\u8f6f\u96c5\u9ed1\">\u6bd4\u5982<\/span>:<\/span><\/p>\n<pre>#coding:utf-8\ns&nbsp;=&nbsp;'\u4e2d\u6587'\nprint&nbsp;s.decode('gbk')<\/pre>\n<p style=\"line-height: 1.75em\"><\/p>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 14px\">\u62a5\u9519\u89e3\u51b3\uff1a<\/span><\/p>\n<pre>#!\/usr\/bin\/python\n#&nbsp;-*-&nbsp;coding:&nbsp;utf-8&nbsp;-*-\nimport&nbsp;sys\n#&nbsp;reload(sys)\n#&nbsp;sys.setdefaultencoding('utf-8')\ns&nbsp;=&nbsp;'\u4e2d\u6587'\n&nbsp;\nprint&nbsp;[s]\nprint&nbsp;s.encode('gbk')<\/pre>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 14px\">\u6b64\u65f6\u62a5\u9519\u4e3aUnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128) \u731c\u6d4b\u867d\u7136\u6307\u5b9a\u4e86\u5f53\u524d\u9875\u9762\u4e3autf-8,\u4f46\u56e0\u4e3a\u76f4\u63a5encode()\u8f6c\u6362\u7a0b\u5e8f\u4f1a\u81ea\u52a8\u5148\u6309\u7167\u7cfb\u7edf\u9ed8\u8ba4\u7684\u7f16\u7801\uff08\u6b64\u65f6\u8fd8\u662fascii) decode\u4e00\u6b21\u6210unicode,\u518d\u4eceunicode\u7f16\u7801\u4e3agbk, \u56e0\u4e3as\u7f16\u7801\u4e3autf-8,\u660e\u663e\u89e3\u7801\u51fa\u9519<\/span><\/p>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 14px\"><\/span><\/p>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 14px\">&nbsp;<\/span><\/p>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 14px\">\u6709\u4e24\u79cd\u89e3\u51b3\u529e\u6cd5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 14px\">1 \u624b\u52a8\u89e3\u7801 print s.decode(\u2018utf-8\u2019).encode(\u2018gbk\u2019)<\/span><\/p>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 14px\">2 \u6539\u53d8\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801,\u5373\u52a0\u5165\u8fd9\u4e24\u53e5<\/span><\/p>\n<pre>reload(sys)\nsys.setdefaultencoding('utf-8')<\/pre>\n<p style=\"line-height: 1.75em\"><span><\/span><\/p>\n<p style=\"line-height: 1.75em\"><span style=\"font-size: 16px\">\u76f8\u4fe1\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6211\u4eec\u5df2\u7ecf\u5b66\u4f1a\u628a<span style=\"font-size: 16px\">\u9ed8\u8ba4\u5b57\u7b26\u4e32\u91c7\u7528\u7684ascii\u7f16\u7801\u6362\u6210<span style=\"font-size: 16px\">utf-8\u4e86\uff0c\u76f8\u4fe1\u540e\u8005\u5927\u5bb6\u4f7f\u7528\u8d77\u6765\u66f4\u4e3a\u987a\u624b\u3002<\/span><\/span><span style=\"font-size: 16px\">\u66f4\u591aPython\u5b66\u4e60\u6307\u8def:<\/span><\/span><span style=\"font-size: 16px\">PyThon\u5b66\u4e60\u7f51\u6559\u5b66\u4e2d\u5fc3<\/span><span style=\"font-size: 16px\">\u3002<\/span><\/p>\n<p style=\"line-height: 1.75em\"><span><span><\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u90fd\u77e5\u9053\u5f88\u591a\u8f6f\u4ef6\u91cc\u4f1a\u81ea\u5e26\u4e00\u4e9b\u5de5\u5177\uff0c\u5927\u90e8\u5206\u4f7f\u7528\u8d77\u6765\u8fd8\u662f\u6bd4\u8f83\u987a\u624b\u7684\uff0c\u4f46\u662f\u603b\u4f1a\u9047\u5230\u4e00\u4e9b\u4e0d\u597d\u7528\u7684\u5de5\u5177\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u5c31\u9700 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":172333,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-172332","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/172332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=172332"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/172332\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/172333"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=172332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=172332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=172332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}