{"id":176481,"date":"2024-05-08T19:11:40","date_gmt":"2024-05-08T11:11:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176481.html"},"modified":"2024-05-08T19:11:44","modified_gmt":"2024-05-08T11:11:44","slug":"python2-7%e9%87%8c%e6%80%8e%e4%b9%88%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%88%90%e5%ad%97%e8%8a%82","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/176481.html","title":{"rendered":"Python2.7\u91cc\u600e\u4e48\u5c06\u5b57\u7b26\u4e32\u8f6c\u6210\u5b57\u8282"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060229\/7117efbb-e20c-440e-b86c-d9a87c5d611b.webp\" alt=\"Python2.7\u91cc\u600e\u4e48\u5c06\u5b57\u7b26\u4e32\u8f6c\u6210\u5b57\u8282\" \/><\/p>\n<p><p>\u5728Python 2.7\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6210\u5b57\u8282\u4e3b\u8981\u4f9d\u8d56\u4e8e\u7f16\u7801\u65b9\u6cd5\u548c\u5b57\u7b26\u4e32\u672c\u8eab\u7684\u7279\u6027\u3002<strong>\u5b57\u7b26\u4e32\u5728Python 2.7\u4e2d\u5206\u4e3aUnicode\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u5b57\u7b26\u4e32<\/strong>\uff0c\u5176\u4e2d\uff0c<strong>Unicode\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u7f16\u7801(encode)\u8f6c\u6362\u6210\u5b57\u8282\u5b57\u7b26\u4e32<\/strong>\uff0c\u800c\u5bf9\u4e8e\u5df2\u7ecf\u662f\u5b57\u8282\u7684\u5b57\u7b26\u4e32\uff0c\u5219\u65e0\u9700\u8f6c\u6362\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\uff0c\u5c06Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5b57\u7b26\u4e32\uff0c\u901a\u5e38\u6307\u5b9a\u7f16\u7801\u4e3a<code>&#039;utf-8&#039;<\/code>\u3002\u8fd9\u4e00\u8f6c\u6362\u5bf9\u4e8e\u5904\u7406\u6587\u672c\u6570\u636e\u5c24\u5176\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u80fd\u786e\u4fdd\u6587\u672c\u53ef\u5728\u4e0d\u540c\u7684\u7cfb\u7edf\u548c\u5e73\u53f0\u95f4\u6b63\u786e\u4f20\u8f93\u548c\u5b58\u50a8\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3\u5b57\u7b26\u4e32\u4e0e\u5b57\u8282\u7684\u533a\u522b<\/h3>\n<\/p>\n<p><p>\u5728\u6df1\u5165\u4e86\u89e3\u8f6c\u6362\u65b9\u6cd5\u4e4b\u524d\uff0c\u9996\u5148\u8981\u660e\u786e\u5728Python 2.7\u91cc\u5b57\u7b26\u4e32\uff08str\uff09\u548cUnicode\u5b57\u7b26\u4e32\uff08unicode\uff09\u7684\u533a\u522b\u3002\u5b57\u7b26\u4e32\u76f4\u63a5\u4ee5\u5b57\u8282\u5e8f\u5217\u5b58\u50a8\uff0c\u800cUnicode\u5b57\u7b26\u4e32\u5219\u662f\u4ee5Unicode\u7801\u70b9\u5b58\u50a8\u7684\u6587\u672c\u3002\u5728\u8fdb\u884c\u7f51\u7edc\u4f20\u8f93\u6216\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5b57\u7b26\u4e32\uff0c\u56e0\u4e3a\u5b57\u8282\u5b57\u7b26\u4e32\u80fd\u5728\u6240\u6709\u73af\u5883\u4e2d\u4f20\u8f93\u548c\u5b58\u50a8\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9Unicode\u5b57\u7b26\u4e32\u8fdb\u884c\u8f6c\u6362\u7684\u6807\u51c6\u65b9\u5f0f\u662f\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u6839\u636e\u6307\u5b9a\u7684\u7f16\u7801\u5c06Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u7b49\u4ef7\u7684\u5b57\u8282\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u5f3a\u5927\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u591a\u79cd\u7f16\u7801\u683c\u5f0f\uff0c\u5176\u4e2d<code>&#039;utf-8&#039;<\/code>\u662f\u6700\u5e7f\u6cdb\u4f7f\u7528\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Unicode\u5b57\u7b26\u4e32<\/p>\n<p>uni_str = u&quot;\u4f60\u597d\u4e16\u754c&quot;<\/p>\n<h2><strong>\u4f7f\u7528utf-8\u7f16\u7801\u5c06Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u5b57\u8282\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>byte_str = uni_str.encode(&#039;utf-8&#039;)<\/p>\n<p>print(byte_str)  # \u8f93\u51fa\u8f6c\u6362\u540e\u7684\u5b57\u8282\u5b57\u7b26\u4e32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u662f\u5904\u7406Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u7684\u901a\u7528\u65b9\u6cd5\uff0c\u7279\u522b\u9002\u5408\u591a\u8bed\u8a00\u7684\u6587\u672c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u5b57\u8282\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728Python 2.7\u4e2d\uff0c\u5982\u679c\u8981\u5904\u7406\u7684\u5b57\u7b26\u4e32\u5df2\u7ecf\u662f\u5b57\u8282\u7c7b\u578b\uff08\u5373str\u7c7b\u578b\uff09\uff0c\u5219\u4e0d\u9700\u8981\u8fdb\u884c\u8f6c\u6362\u3002\u5b57\u8282\u5b57\u7b26\u4e32\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u6587\u4ef6\u64cd\u4f5c\u3001\u7f51\u7edc\u901a\u4fe1\u7b49\u9700\u8981\u5b57\u8282\u6570\u636e\u7684\u573a\u666f\u3002\u7136\u800c\uff0c\u5f53\u5b57\u7b26\u4e32\u542b\u6709\u975eASCII\u5b57\u7b26\u65f6\uff0c\u4ecd\u7136\u9700\u8981\u8003\u8651\u7f16\u7801\uff0c\u4ee5\u4fdd\u8bc1\u5b57\u7b26\u7684\u6b63\u786e\u8868\u793a\u548c\u4f20\u8f93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b57\u8282\u5b57\u7b26\u4e32\uff0cPython 2.7\u9ed8\u8ba4\u5b57\u7b26\u4e32\u7c7b\u578b<\/p>\n<p>byte_str = &quot;Hello world&quot;<\/p>\n<h2><strong>\u5df2\u7ecf\u662f\u5b57\u8282\u7c7b\u578b\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528<\/strong><\/h2>\n<p>print(byte_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5177\u6709\u56fd\u9645\u5316\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u660e\u786e\u7f16\u7801\u4ecd\u7136\u662f\u5173\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6ce8\u610f\u7f16\u7801\u517c\u5bb9\u6027\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u9ad8\u6548\u5730\u8f6c\u6362Unicode\u5b57\u7b26\u4e32\u81f3\u5b57\u8282\u5b57\u7b26\u4e32\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u7f16\u7801\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u786e\u4fdd\u7f16\u7801\u540e\u7684\u5b57\u8282\u5b57\u7b26\u4e32\u80fd\u591f\u88ab\u76ee\u6807\u7cfb\u7edf\u6216\u5e94\u7528\u6b63\u786e\u89e3\u6790\u548c\u663e\u793a\u3002\u5bf9\u4e8e\u9700\u8981\u8de8\u8bed\u8a00\u6216\u5e73\u53f0\u4ea4\u4e92\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c<code>&#039;utf-8&#039;<\/code>\u7f16\u7801\u63d0\u4f9b\u4e86\u4e00\u79cd\u901a\u7528\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">unicode_str = u&quot;\u6587\u672c\u4fe1\u606f&quot;<\/p>\n<p>try:<\/p>\n<p>    byte_str = unicode_str.encode(&#039;utf-8&#039;)<\/p>\n<p>    print(byte_str)<\/p>\n<p>except UnicodeEncodeError:<\/p>\n<p>    print(&quot;\u7f16\u7801\u5931\u8d25&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u9002\u5f53\u7684\u5f02\u5e38\u5904\u7406\uff0c\u53ef\u4ee5\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u66f4\u52a0\u5065\u58ee\u548c\u53ef\u9760\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python 2.7\u4e2d\u6709\u6548\u5730\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u5b57\u8282\uff0c\u65e0\u8bba\u662f\u5904\u7406\u672c\u5730\u5316\u6587\u672c\u8fd8\u662f\u8fdb\u884c\u6570\u636e\u4f20\u8f93\uff0c\u638c\u63e1\u5b57\u7b26\u4e32\u4e0e\u5b57\u8282\u4e4b\u95f4\u7684\u8f6c\u6362\u5bf9\u7f16\u5199\u9ad8\u6548\u3001\u517c\u5bb9\u7684Python\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u600e\u6837\u5728Python2.7\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\uff1f<\/strong><\/p>\n<p>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u662f\u5728Python 2.7\u4e2d\u975e\u5e38\u5e38\u89c1\u7684\u4e00\u4e2a\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528<code>encode<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u6307\u5b9a\u7684\u5b57\u7b26\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528UTF-8\u7f16\u7801\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>string_value = &quot;Hello, world!&quot;\nbyte_value = string_value.encode(&quot;utf-8&quot;)\n<\/code><\/pre>\n<p><strong>2. Python2.7\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u7684\u5176\u4ed6\u5e38\u7528\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528<code>encode<\/code>\u65b9\u6cd5\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>str<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\uff1a<\/p>\n<pre><code>string_value = &quot;Hello, world!&quot;\nbyte_value = str(string_value)\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>bytearray<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">string_value = &quot;Hello, world!&quot;\nbyte_array = bytearray(string_value)\n<\/code><\/pre>\n<p><strong>3. Python2.7\u4e2d\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u7f16\u7801\u683c\u5f0f\u7684\u5b57\u8282\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u7f16\u7801\u683c\u5f0f\u7684\u5b57\u8282\uff0c\u53ef\u4ee5\u5728\u8c03\u7528<code>encode<\/code>\u65b9\u6cd5\u65f6\u4f20\u5165\u6240\u9700\u7684\u5b57\u7b26\u7f16\u7801\u683c\u5f0f\u4f5c\u4e3a\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aGBK\u7f16\u7801\u7684\u5b57\u8282\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u65b9\u5f0f\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-python\">string_value = &quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01&quot;\nbyte_value = string_value.encode(&quot;gbk&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u7684\u5b57\u8282\u3002\u8bf7\u786e\u4fdd\u4f7f\u7528\u4e0e\u7f51\u7edc\u4f20\u8f93\u6216\u5b58\u50a8\u6570\u636e\u6240\u9700\u7684\u7f16\u7801\u683c\u5f0f\u76f8\u5339\u914d\u7684\u5b57\u7b26\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python 2.7\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6210\u5b57\u8282\u4e3b\u8981\u4f9d\u8d56\u4e8e\u7f16\u7801\u65b9\u6cd5\u548c\u5b57\u7b26\u4e32\u672c\u8eab\u7684\u7279\u6027\u3002\u5b57\u7b26\u4e32\u5728Python 2.7\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":176485,"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\/176481"}],"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=176481"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/176481\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/176485"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=176481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=176481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=176481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}