{"id":179155,"date":"2024-05-08T20:07:04","date_gmt":"2024-05-08T12:07:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/179155.html"},"modified":"2024-05-08T20:07:10","modified_gmt":"2024-05-08T12:07:10","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-python-2-%e5%92%8c-python-3-%e9%80%a0%e5%b0%b1%e4%ba%86%e7%a4%be%e5%8c%ba%e5%88%86%e8%a3%82","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/179155.html","title":{"rendered":"\u4e3a\u4ec0\u4e48 Python 2 \u548c Python 3 \u9020\u5c31\u4e86\u793e\u533a\u5206\u88c2"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27063749\/45a9b957-5c94-4230-b159-88bf36a1c885.webp\" alt=\"\u4e3a\u4ec0\u4e48 Python 2 \u548c Python 3 \u9020\u5c31\u4e86\u793e\u533a\u5206\u88c2\" \/><\/p>\n<p><p>Python 2\u548cPython 3\u4e4b\u95f4\u7684\u4e3b\u8981\u5206\u6b67\u5728\u4e8e\u4e0d\u517c\u5bb9\u7684\u8bed\u6cd5\u3001\u6807\u51c6\u5e93\u5dee\u5f02\u3001\u5b57\u7b26\u4e32\u4ee5\u53ca\u5b57\u8282\u7684\u5904\u7406\u65b9\u5f0f\u3001\u6539\u8fdb\u7684\u6574\u6570\u9664\u6cd5\u7b49\u65b9\u9762\u3002<strong>\u8fd9\u79cd\u4e0d\u517c\u5bb9\u6027\u9020\u6210\u4e86\u663e\u8457\u7684\u793e\u533a\u5206\u88c2<\/strong>\uff0c\u4e3b\u8981\u4f53\u73b0\u5728<strong>\u73b0\u6709\u4ee3\u7801\u5e93\u7684\u7ef4\u62a4\u6210\u672c\u589e\u52a0\u3001\u793e\u533a\u8d44\u6e90\u7684\u5206\u6563\u4ee5\u53ca\u65b0\u9879\u76ee\u91c7\u7528\u8bed\u8a00\u7248\u672c\u7684\u51b3\u7b56\u96be\u5ea6\u63d0\u5347<\/strong>\u3002\u7279\u522b\u662f\uff0c\u5728\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u4e32\u7684\u5904\u7406\u4e0a\uff0cPython 3\u5f15\u5165\u4e86\u660e\u786e\u7684\u533a\u5206\uff0c\u7528<code>unicode<\/code>\u8868\u793a\u6587\u672c\u5b57\u7b26\u4e32\uff0c<code>bytes<\/code>\u8868\u793a\u5b57\u8282\u4e32\uff0c\u8fd9\u5728\u672c\u8d28\u4e0a\u662f\u4e3a\u4e86\u66f4\u597d\u5730\u652f\u6301\u56fd\u9645\u5316\uff0c\u4f46\u4e5f\u4f7f\u5f97\u4ecePython 2\u8fc1\u79fb\u5230Python 3\u53d8\u5f97\u590d\u6742\uff0c\u56e0\u4e3a\u9700\u8981\u5f00\u53d1\u4eba\u5458\u8c03\u6574\u7f16\u7801\u4e60\u60ef\uff0c\u786e\u4fdd\u6570\u636e\u7c7b\u578b\u7684\u6b63\u786e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8bed\u6cd5\u4e0d\u517c\u5bb9<\/h3>\n<\/p>\n<p><p>Python 3\u5f15\u5165\u4e86\u65b0\u7684\u8bed\u6cd5\u7279\u6027\uff0c\u540c\u65f6\u653e\u5f03\u4e86Python 2\u4e2d\u7684\u4e00\u4e9b\u8001\u65e7\u8bed\u6cd5\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u4f7fPython\u8bed\u8a00\u66f4\u52a0\u6e05\u6670\u548c\u4e00\u81f4\u3002\u6bd4\u5982\uff0c<code>print<\/code>\u5728Python 2\u4e2d\u662f\u4e00\u4e2a\u8bed\u53e5\uff0c\u800c\u5728Python 3\u4e2d\u53d8\u6210\u4e86\u4e00\u4e2a\u51fd\u6570\uff0c\u9700\u8981\u4f7f\u7528\u62ec\u53f7\u5305\u56f4\u8981\u6253\u5370\u7684\u5185\u5bb9\u3002\u8fd9\u79cd\u770b\u4f3c\u7b80\u5355\u7684\u53d8\u5316\uff0c\u5b9e\u9645\u4e0a\u8981\u6c42\u5f00\u53d1\u8005\u4fee\u6539\u5927\u91cf\u7684\u4ee3\u7801\uff0c\u5c24\u5176\u662f\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u8fd9\u65e0\u7591\u662f\u4e00\u9879\u5de8\u5927\u7684\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u53e6\u5916\uff0cPython 3\u5bf9\u4e8e\u5f02\u5e38\u7684\u629b\u51fa\u548c\u6355\u83b7\u4e5f\u505a\u51fa\u4e86\u6539\u53d8\uff0c\u4f7f\u7528\u65b0\u7684<code>as<\/code>\u5173\u952e\u8bcd\u53d6\u4ee3\u4e86\u65e7\u7684\u9017\u53f7\u8bed\u6cd5\uff0c\u8fd9\u8981\u6c42\u5f00\u53d1\u8005\u91cd\u65b0\u7f16\u5199\u5f02\u5e38\u5904\u7406\u7684\u4ee3\u7801\u3002\u8fd9\u4e9b\u4e0d\u517c\u5bb9\u7684\u53d8\u66f4\u867d\u7136\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u4e00\u81f4\u6027\uff0c\u4f46\u4e5f\u589e\u52a0\u4e86\u79fb\u690d\u65e7\u4ee3\u7801\u7684\u96be\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6807\u51c6\u5e93\u7684\u53d8\u5316<\/h3>\n<\/p>\n<p><p>Python 3\u5bf9\u6807\u51c6\u5e93\u8fdb\u884c\u4e86\u91cd\u7ec4\u548c\u4f18\u5316\uff0c\u4e00\u4e9b\u5e93\u88ab\u91cd\u547d\u540d\u6216\u91cd\u65b0\u7ec4\u7ec7\uff0c\u610f\u5473\u7740\u5f00\u53d1\u8005\u5728\u8fc1\u79fb\u5230Python 3\u65f6\uff0c\u9700\u8981\u5bf9\u5bfc\u5165\u7684\u5e93\u8fdb\u884c\u4fee\u6539\u3002\u6bd4\u5982\uff0c<code>urlib<\/code>\u3001<code>urlib2<\/code>\u3001<code>urlparse<\/code>\u7b49\u5728Python 2\u4e2d\u7528\u4e8e\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\u7684\u5e93\uff0c\u5728Python 3\u4e2d\u88ab\u6574\u5408\u4e3a<code>urllib.request<\/code>\u3001<code>urllib.parse<\/code>\u7b49\uff0c\u8fd9\u79cd\u53d8\u5316\u867d\u5bf9\u4e8e\u65b0\u9879\u76ee\u6709\u76ca\uff0c\u4f46\u5bf9\u4e8e\u7ef4\u62a4\u65e2\u6709\u4ee3\u7801\u5219\u589e\u52a0\u4e86\u6210\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b57\u7b26\u4e32\u4e0e\u5b57\u8282\u7684\u5904\u7406<\/h3>\n<\/p>\n<p><p>Python 3\u5728\u5b57\u7b26\u4e32\uff08\u9ed8\u8ba4\u4e3aUnicode\uff09\u548c\u5b57\u8282\u4e32\u4e4b\u95f4\u505a\u4e86\u660e\u786e\u7684\u533a\u5206\uff0c\u8fd9\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u89e3\u51b3\u4e86Python 2\u4e2d\u6587\u672c\u548c\u4e8c\u8fdb\u5236\u6570\u636e\u5904\u7406\u7684\u6df7\u4e71\u95ee\u9898\uff0c\u4f46\u540c\u65f6\u4e5f\u7ed9\u65e7\u4ee3\u7801\u7684\u8fc1\u79fb\u5e26\u6765\u4e86\u6311\u6218\u3002\u5728Python 2\u4e2d\uff0c\u5b57\u7b26\u4e32\u65e2\u53ef\u4ee5\u8868\u793a\u6587\u672c\uff0c\u4e5f\u53ef\u4ee5\u8868\u793a\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u8fd9\u5bfc\u81f4\u4e86\u7f16\u7801\u76f8\u5173\u7684\u9519\u8bef\u3002Python 3\u901a\u8fc7\u5f15\u5165<code>str<\/code>\u548c<code>bytes<\/code>\u7c7b\u578b\u6765\u5206\u522b\u8868\u793a\u6587\u672c\u548c\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u63d0\u9ad8\u4e86\u5904\u7406\u6570\u636e\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6574\u6570\u9664\u6cd5\u7684\u53d8\u5316<\/h3>\n<\/p>\n<p><p>Python 3\u8fd8\u6539\u8fdb\u4e86\u6574\u6570\u9664\u6cd5\u7684\u884c\u4e3a\uff0c\u4f7f\u5176\u66f4\u52a0\u76f4\u89c2\u3002\u5728Python 2\u4e2d\uff0c\u4e24\u4e2a\u6574\u6570\u76f8\u9664\u4f1a\u81ea\u52a8\u53d6\u6574\uff0c\u800c\u5728Python 3\u4e2d\uff0c\u76f8\u540c\u7684\u64cd\u4f5c\u4f1a\u4ea7\u751f\u6d6e\u70b9\u6570\u7ed3\u679c\u3002\u8fd9\u79cd\u6539\u53d8\u867d\u7136\u4f7f\u5f97\u6570\u5b66\u8fd0\u7b97\u7684\u7ed3\u679c\u66f4\u52a0\u51c6\u786e\uff0c\u4f46\u4e5f\u8981\u6c42\u5f00\u53d1\u8005\u5728\u8fc1\u79fb\u4ee3\u7801\u65f6\u6ce8\u610f\u5904\u7406\u9664\u6cd5\u7684\u53d8\u5316\uff0c\u4ee5\u907f\u514d\u903b\u8f91\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7f16\u7801\u58f0\u660e\u7684\u53d8\u5316<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u652f\u6301\u56fd\u9645\u5316\uff0cPython 3\u5c06\u9ed8\u8ba4\u7f16\u7801\u8bbe\u7f6e\u4e3aUTF-8\uff0c\u8fd9\u4f7f\u5f97\u5904\u7406\u591a\u8bed\u8a00\u6570\u636e\u53d8\u5f97\u66f4\u52a0\u7b80\u4fbf\u3002\u7136\u800c\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u4ecePython 2\u8fc1\u79fb\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u989d\u5916\u6ce8\u610f\u7f16\u7801\u95ee\u9898\uff0c\u786e\u4fdd\u5b57\u7b26\u4e32\u7684\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>Python 3\u5f15\u5165\u7684\u5404\u9879\u6539\u52a8\u76ee\u7684\u5728\u4e8e\u4f7fPython\u8bed\u8a00\u66f4\u52a0\u5148\u8fdb\u548c\u4e00\u81f4\uff0c\u4f46\u8fd9\u4e9b\u6539\u52a8\u5728\u77ed\u671f\u5185\u589e\u52a0\u4e86\u5f00\u53d1\u8005\u7684\u8fc1\u79fb\u6210\u672c\uff0c\u5bfc\u81f4\u4e86\u793e\u533a\u7684\u5206\u88c2\u3002\u957f\u8fdc\u6765\u770b\uff0c\u968f\u7740\u66f4\u591a\u7684\u9879\u76ee\u548c\u5f00\u53d1\u8005\u8f6c\u5411Python 3\uff0c\u793e\u533a\u5c06\u9010\u6b65\u7edf\u4e00\uff0c\u5e26\u6765Python\u751f\u6001\u7684\u8fdb\u4e00\u6b65\u7e41\u8363\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Python\u793e\u533a\u4e3a\u4ec0\u4e48\u51fa\u73b0\u5206\u88c2\u73b0\u8c61\uff1f<\/strong><\/p>\n<p>\u5206\u88c2\u73b0\u8c61\u4e3b\u8981\u662f\u56e0\u4e3aPython\u7684\u5347\u7ea7\u7248\u672c\u4ece2\u52303\u5b58\u5728\u4e00\u4e9b\u4e0d\u517c\u5bb9\u7684\u6539\u53d8\uff0c\u5bfc\u81f4\u4e86\u793e\u533a\u5728\u9009\u62e9\u4f7f\u7528Python\u7684\u7248\u672c\u4e0a\u51fa\u73b0\u4e86\u5206\u6b67\u3002<\/p>\n<p><strong>Python 2\u548cPython 3\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><\/p>\n<p>Python 2\u548cPython 3\u4e4b\u95f4\u5b58\u5728\u4e00\u4e9b\u8bed\u6cd5\u548c\u529f\u80fd\u4e0a\u7684\u5dee\u5f02\u3002Python 3\u662fPython\u8bed\u8a00\u7684\u4e0b\u4e00\u4e2a\u4e3b\u8981\u7248\u672c\uff0c\u5f15\u5165\u4e86\u4e00\u4e9b\u65b0\u7684\u529f\u80fd\u548c\u6539\u8fdb\uff0c\u540c\u65f6\u4fee\u590d\u4e86\u4e00\u4e9b\u8fc7\u53bb\u5728Python 2\u4e2d\u5b58\u5728\u7684\u95ee\u9898\u3002\u7136\u800c\uff0c\u7531\u4e8e\u67d0\u4e9b\u8bed\u6cd5\u548c\u51fd\u6570\u7684\u6539\u53d8\uff0c\u5bfc\u81f4Python 2\u7684\u4e00\u4e9b\u73b0\u6709\u4ee3\u7801\u65e0\u6cd5\u5728Python 3\u4e2d\u8fd0\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python 2\u548cPython 3\u4e4b\u95f4\u7684\u517c\u5bb9\u6027\u95ee\u9898\uff1f<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3Python 2\u548cPython 3\u4e4b\u95f4\u7684\u517c\u5bb9\u6027\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5de5\u5177\u548c\u6280\u672f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u75282to3\u5de5\u5177\u5c06Python 2\u4ee3\u7801\u8f6c\u6362\u4e3aPython 3\u4ee3\u7801\uff0c\u6216\u8005\u4f7f\u7528future\u6a21\u5757\u5728Python 2\u4e2d\u4f7f\u7528\u4e00\u4e9bPython 3\u7684\u7279\u6027\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u7f16\u5199\u9002\u914d\u5668\u6216\u517c\u5bb9\u6027\u5c42\uff0c\u4ee5\u4fbf\u5728\u4e24\u4e2a\u7248\u672c\u4e4b\u95f4\u5171\u4eab\u4ee3\u7801\u3002\u6700\u91cd\u8981\u7684\u662f\uff0c\u793e\u533a\u6210\u5458\u4e4b\u95f4\u7684\u4ea4\u6d41\u548c\u5408\u4f5c\u4e5f\u662f\u89e3\u51b3\u517c\u5bb9\u6027\u95ee\u9898\u7684\u5173\u952e\uff0c\u901a\u8fc7\u76f8\u4e92\u5b66\u4e60\u548c\u5171\u4eab\u7ecf\u9a8c\uff0c\u53ef\u4ee5\u627e\u5230\u66f4\u597d\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python 2\u548cPython 3\u4e4b\u95f4\u7684\u4e3b\u8981\u5206\u6b67\u5728\u4e8e\u4e0d\u517c\u5bb9\u7684\u8bed\u6cd5\u3001\u6807\u51c6\u5e93\u5dee\u5f02\u3001\u5b57\u7b26\u4e32\u4ee5\u53ca\u5b57\u8282\u7684\u5904\u7406\u65b9\u5f0f\u3001\u6539\u8fdb\u7684 [&hellip;]","protected":false},"author":3,"featured_media":179162,"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\/179155"}],"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=179155"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/179155\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/179162"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=179155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=179155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=179155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}