{"id":754,"date":"2022-05-14T16:44:33","date_gmt":"2022-05-14T08:44:33","guid":{"rendered":"http:\/\/blog.xtaa.cn\/?p=754"},"modified":"2024-12-20T15:38:47","modified_gmt":"2024-12-20T07:38:47","slug":"%e5%ad%97%e7%ac%a6%e7%bc%96%e7%a0%81","status":"publish","type":"post","link":"http:\/\/blog.xtaa.cn\/index.php\/2022\/05\/14\/%e5%ad%97%e7%ac%a6%e7%bc%96%e7%a0%81\/","title":{"rendered":"\u5b57\u7b26\u7f16\u7801"},"content":{"rendered":"\n<p>\u6458\u81ea<a href=\"https:\/\/liaoxuefeng.com\/books\/python\/basic\/string-encoding\/index.html\">https:\/\/liaoxuefeng.com\/books\/python\/basic\/string-encoding\/index.html<\/a><\/p>\n\n\n\n<p>\u5b57\u7b26\u4e32\u4e5f\u662f\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u662f\uff0c\u5b57\u7b26\u4e32\u6bd4\u8f83\u7279\u6b8a\u7684\u662f\u8fd8\u6709\u4e00\u4e2a\u7f16\u7801\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3a\u8ba1\u7b97\u673a\u53ea\u80fd\u5904\u7406\u6570\u5b57\uff0c\u5982\u679c\u8981\u5904\u7406\u6587\u672c\uff0c\u5c31\u5fc5\u987b\u5148\u628a\u6587\u672c\u8f6c\u6362\u4e3a\u6570\u5b57\u624d\u80fd\u5904\u7406\u3002\u6700\u65e9\u7684\u8ba1\u7b97\u673a\u5728\u8bbe\u8ba1\u65f6\u91c7\u75288\u4e2a\u6bd4\u7279\uff08bit\uff09\u4f5c\u4e3a\u4e00\u4e2a\u5b57\u8282\uff08byte\uff09\uff0c\u6240\u4ee5\uff0c\u4e00\u4e2a\u5b57\u8282\u80fd\u8868\u793a\u7684\u6700\u5927\u7684\u6574\u6570\u5c31\u662f255\uff08\u4e8c\u8fdb\u523611111111=\u5341\u8fdb\u5236255\uff09\uff0c\u5982\u679c\u8981\u8868\u793a\u66f4\u5927\u7684\u6574\u6570\uff0c\u5c31\u5fc5\u987b\u7528\u66f4\u591a\u7684\u5b57\u8282\u3002\u6bd4\u5982\u4e24\u4e2a\u5b57\u8282\u53ef\u4ee5\u8868\u793a\u7684\u6700\u5927\u6574\u6570\u662f<code>65535<\/code>\uff0c4\u4e2a\u5b57\u8282\u53ef\u4ee5\u8868\u793a\u7684\u6700\u5927\u6574\u6570\u662f<code>4294967295<\/code>\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u8ba1\u7b97\u673a\u662f\u7f8e\u56fd\u4eba\u53d1\u660e\u7684\uff0c\u56e0\u6b64\uff0c\u6700\u65e9\u53ea\u6709127\u4e2a\u5b57\u7b26\u88ab\u7f16\u7801\u5230\u8ba1\u7b97\u673a\u91cc\uff0c\u4e5f\u5c31\u662f\u5927\u5c0f\u5199\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e00\u4e9b\u7b26\u53f7\uff0c\u8fd9\u4e2a\u7f16\u7801\u8868\u88ab\u79f0\u4e3a<code>ASCII<\/code>\u7f16\u7801\uff0c\u6bd4\u5982\u5927\u5199\u5b57\u6bcd<code>A<\/code>\u7684\u7f16\u7801\u662f<code>65<\/code>\uff0c\u5c0f\u5199\u5b57\u6bcd<code>z<\/code>\u7684\u7f16\u7801\u662f<code>122<\/code>\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u8981\u5904\u7406\u4e2d\u6587\u663e\u7136\u4e00\u4e2a\u5b57\u8282\u662f\u4e0d\u591f\u7684\uff0c\u81f3\u5c11\u9700\u8981\u4e24\u4e2a\u5b57\u8282\uff0c\u800c\u4e14\u8fd8\u4e0d\u80fd\u548cASCII\u7f16\u7801\u51b2\u7a81\uff0c\u6240\u4ee5\uff0c\u4e2d\u56fd\u5236\u5b9a\u4e86<code>GB2312<\/code>\u7f16\u7801\uff0c\u7528\u6765\u628a\u4e2d\u6587\u7f16\u8fdb\u53bb\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u60f3\u5f97\u5230\u7684\u662f\uff0c\u5168\u4e16\u754c\u6709\u4e0a\u767e\u79cd\u8bed\u8a00\uff0c\u65e5\u672c\u628a\u65e5\u6587\u7f16\u5230<code>Shift_JIS<\/code>\u91cc\uff0c\u97e9\u56fd\u628a\u97e9\u6587\u7f16\u5230<code>Euc-kr<\/code>\u91cc\uff0c\u5404\u56fd\u6709\u5404\u56fd\u7684\u6807\u51c6\uff0c\u5c31\u4f1a\u4e0d\u53ef\u907f\u514d\u5730\u51fa\u73b0\u51b2\u7a81\uff0c\u7ed3\u679c\u5c31\u662f\uff0c\u5728\u591a\u8bed\u8a00\u6df7\u5408\u7684\u6587\u672c\u4e2d\uff0c\u663e\u793a\u51fa\u6765\u4f1a\u6709\u4e71\u7801\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.liaoxuefeng.com\/files\/attachments\/923930471927008\/0\" alt=\"char-encoding-problem\"\/><\/figure>\n\n\n\n<p>\u56e0\u6b64\uff0cUnicode\u5b57\u7b26\u96c6\u5e94\u8fd0\u800c\u751f\u3002Unicode\u628a\u6240\u6709\u8bed\u8a00\u90fd\u7edf\u4e00\u5230\u4e00\u5957\u7f16\u7801\u91cc\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u518d\u6709\u4e71\u7801\u95ee\u9898\u4e86\u3002<\/p>\n\n\n\n<p>Unicode\u6807\u51c6\u4e5f\u5728\u4e0d\u65ad\u53d1\u5c55\uff0c\u4f46\u6700\u5e38\u7528\u7684\u662fUCS-16\u7f16\u7801\uff0c\u7528\u4e24\u4e2a\u5b57\u8282\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff08\u5982\u679c\u8981\u7528\u5230\u975e\u5e38\u504f\u50fb\u7684\u5b57\u7b26\uff0c\u5c31\u9700\u89814\u4e2a\u5b57\u8282\uff09\u3002\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u548c\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u90fd\u76f4\u63a5\u652f\u6301Unicode\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\uff0c\u634b\u4e00\u634bASCII\u7f16\u7801\u548cUnicode\u7f16\u7801\u7684\u533a\u522b\uff1aASCII\u7f16\u7801\u662f1\u4e2a\u5b57\u8282\uff0c\u800cUnicode\u7f16\u7801\u901a\u5e38\u662f2\u4e2a\u5b57\u8282\u3002<\/p>\n\n\n\n<p>\u5b57\u6bcd<code>A<\/code>\u7528ASCII\u7f16\u7801\u662f\u5341\u8fdb\u5236\u7684<code>65<\/code>\uff0c\u4e8c\u8fdb\u5236\u7684<code>01000001<\/code>\uff1b<\/p>\n\n\n\n<p>\u5b57\u7b26<code>0<\/code>\u7528ASCII\u7f16\u7801\u662f\u5341\u8fdb\u5236\u7684<code>48<\/code>\uff0c\u4e8c\u8fdb\u5236\u7684<code>00110000<\/code>\uff0c\u6ce8\u610f\u5b57\u7b26<code>'0'<\/code>\u548c\u6574\u6570<code>0<\/code>\u662f\u4e0d\u540c\u7684\uff1b<\/p>\n\n\n\n<p>\u6c49\u5b57<code>\u4e2d<\/code>\u5df2\u7ecf\u8d85\u51fa\u4e86ASCII\u7f16\u7801\u7684\u8303\u56f4\uff0c\u7528Unicode\u7f16\u7801\u662f\u5341\u8fdb\u5236\u7684<code>20013<\/code>\uff0c\u4e8c\u8fdb\u5236\u7684<code>01001110 00101101<\/code>\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u731c\u6d4b\uff0c\u5982\u679c\u628aASCII\u7f16\u7801\u7684<code>A<\/code>\u7528Unicode\u7f16\u7801\uff0c\u53ea\u9700\u8981\u5728\u524d\u9762\u88650\u5c31\u53ef\u4ee5\uff0c\u56e0\u6b64\uff0c<code>A<\/code>\u7684Unicode\u7f16\u7801\u662f<code>00000000 01000001<\/code>\u3002<\/p>\n\n\n\n<p>\u65b0\u7684\u95ee\u9898\u53c8\u51fa\u73b0\u4e86\uff1a\u5982\u679c\u7edf\u4e00\u6210Unicode\u7f16\u7801\uff0c\u4e71\u7801\u95ee\u9898\u4ece\u6b64\u6d88\u5931\u4e86\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u5199\u7684\u6587\u672c\u57fa\u672c\u4e0a\u5168\u90e8\u662f\u82f1\u6587\u7684\u8bdd\uff0c\u7528Unicode\u7f16\u7801\u6bd4ASCII\u7f16\u7801\u9700\u8981\u591a\u4e00\u500d\u7684\u5b58\u50a8\u7a7a\u95f4\uff0c\u5728\u5b58\u50a8\u548c\u4f20\u8f93\u4e0a\u5c31\u5341\u5206\u4e0d\u5212\u7b97\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u672c\u7740\u8282\u7ea6\u7684\u7cbe\u795e\uff0c\u53c8\u51fa\u73b0\u4e86\u628aUnicode\u7f16\u7801\u8f6c\u5316\u4e3a\u201c\u53ef\u53d8\u957f\u7f16\u7801\u201d\u7684<code>UTF-8<\/code>\u7f16\u7801\u3002UTF-8\u7f16\u7801\u628a\u4e00\u4e2aUnicode\u5b57\u7b26\u6839\u636e\u4e0d\u540c\u7684\u6570\u5b57\u5927\u5c0f\u7f16\u7801\u62101-6\u4e2a\u5b57\u8282\uff0c\u5e38\u7528\u7684\u82f1\u6587\u5b57\u6bcd\u88ab\u7f16\u7801\u62101\u4e2a\u5b57\u8282\uff0c\u6c49\u5b57\u901a\u5e38\u662f3\u4e2a\u5b57\u8282\uff0c\u53ea\u6709\u5f88\u751f\u50fb\u7684\u5b57\u7b26\u624d\u4f1a\u88ab\u7f16\u7801\u62104-6\u4e2a\u5b57\u8282\u3002\u5982\u679c\u4f60\u8981\u4f20\u8f93\u7684\u6587\u672c\u5305\u542b\u5927\u91cf\u82f1\u6587\u5b57\u7b26\uff0c\u7528UTF-8\u7f16\u7801\u5c31\u80fd\u8282\u7701\u7a7a\u95f4\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>\u5b57\u7b26<\/th><th>ASCII<\/th><th>Unicode<\/th><th>UTF-8<\/th><\/tr><tr><td>A<\/td><td>01000001<\/td><td>00000000 01000001<\/td><td>01000001<\/td><\/tr><tr><td>\u4e2d<\/td><td>x<\/td><td>01001110 00101101<\/td><td>11100100 10111000 10101101<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4ece\u4e0a\u9762\u7684\u8868\u683c\u8fd8\u53ef\u4ee5\u53d1\u73b0\uff0cUTF-8\u7f16\u7801\u6709\u4e00\u4e2a\u989d\u5916\u7684\u597d\u5904\uff0c\u5c31\u662fASCII\u7f16\u7801\u5b9e\u9645\u4e0a\u53ef\u4ee5\u88ab\u770b\u6210\u662fUTF-8\u7f16\u7801\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\uff0c\u5927\u91cf\u53ea\u652f\u6301ASCII\u7f16\u7801\u7684\u5386\u53f2\u9057\u7559\u8f6f\u4ef6\u53ef\u4ee5\u5728UTF-8\u7f16\u7801\u4e0b\u7ee7\u7eed\u5de5\u4f5c\u3002<\/p>\n\n\n\n<p>\u641e\u6e05\u695a\u4e86ASCII\u3001Unicode\u548cUTF-8\u7684\u5173\u7cfb\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u603b\u7ed3\u4e00\u4e0b\u73b0\u5728\u8ba1\u7b97\u673a\u7cfb\u7edf\u901a\u7528\u7684\u5b57\u7b26\u7f16\u7801\u5de5\u4f5c\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<p><strong>\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\uff0c\u7edf\u4e00\u4f7f\u7528Unicode\u7f16\u7801\uff0c\u5f53\u9700\u8981\u4fdd\u5b58\u5230\u786c\u76d8\u6216\u8005\u9700\u8981\u4f20\u8f93\u7684\u65f6\u5019\uff0c\u5c31\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801<\/strong>\u3002<\/p>\n\n\n\n<p>\u7528\u8bb0\u4e8b\u672c\u7f16\u8f91\u7684\u65f6\u5019\uff0c\u4ece\u6587\u4ef6\u8bfb\u53d6\u7684UTF-8\u5b57\u7b26\u88ab<strong>\u8f6c\u6362\u4e3aUnicode\u5b57\u7b26\u5230\u5185\u5b58\u91cc<\/strong>\uff0c\u7f16\u8f91\u5b8c\u6210\u540e\uff0c\u4fdd\u5b58\u7684\u65f6\u5019\u518d\u628aUnicode\u8f6c\u6362\u4e3aUTF-8\u4fdd\u5b58\u5230\u6587\u4ef6\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.liaoxuefeng.com\/files\/attachments\/923923787018816\/0\" alt=\"rw-file-utf-8\"\/><\/figure>\n\n\n\n<p>\u6d4f\u89c8\u7f51\u9875\u7684\u65f6\u5019\uff0c\u670d\u52a1\u5668\u4f1a\u628a\u52a8\u6001\u751f\u6210\u7684Unicode\u5185\u5bb9\u8f6c\u6362\u4e3aUTF-8\u518d\u4f20\u8f93\u5230\u6d4f\u89c8\u5668\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.liaoxuefeng.com\/files\/attachments\/923923759189600\/0\" alt=\"web-utf-8\"\/><\/figure>\n\n\n\n<p>\u6240\u4ee5\u4f60\u770b\u5230\u5f88\u591a\u7f51\u9875\u7684\u6e90\u7801\u4e0a\u4f1a\u6709\u7c7b\u4f3c<code>&lt;meta charset=\"UTF-8\" \/&gt;<\/code>\u7684\u4fe1\u606f\uff0c\u8868\u793a\u8be5\u7f51\u9875\u6b63\u662f\u7528\u7684UTF-8\u7f16\u7801\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Python\u7684\u5b57\u7b26\u4e32<\/h3>\n\n\n\n<p>\u641e\u6e05\u695a\u4e86\u4ee4\u4eba\u5934\u75bc\u7684\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u540e\uff0c\u6211\u4eec\u518d\u6765\u7814\u7a76Python\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>\u5728\u6700\u65b0\u7684Python 3\u7248\u672c\u4e2d\uff0c\u5b57\u7b26\u4e32\u662f\u4ee5Unicode\u7f16\u7801\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0cPython\u7684\u5b57\u7b26\u4e32\u652f\u6301\u591a\u8bed\u8a00\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; print('\u5305\u542b\u4e2d\u6587\u7684str')\n\u5305\u542b\u4e2d\u6587\u7684str\n<\/code><\/pre>\n\n\n\n<p>\u5bf9\u4e8e\u5355\u4e2a\u5b57\u7b26\u7684\u7f16\u7801\uff0cPython\u63d0\u4f9b\u4e86<code>ord()<\/code>\u51fd\u6570\u83b7\u53d6\u5b57\u7b26\u7684\u6574\u6570\u8868\u793a\uff0c<code>chr()<\/code>\u51fd\u6570\u628a\u7f16\u7801\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5b57\u7b26\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; ord('A')\n65\n&gt;&gt;&gt; ord('\u4e2d')\n20013\n&gt;&gt;&gt; chr(66)\n'B'\n&gt;&gt;&gt; chr(25991)\n'\u6587'\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u77e5\u9053\u5b57\u7b26\u7684\u6574\u6570\u7f16\u7801\uff0c\u8fd8\u53ef\u4ee5\u7528\u5341\u516d\u8fdb\u5236\u8fd9\u4e48\u5199<code>str<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; '\\u4e2d\\u6587'\n'\u4e2d\u6587'\n<\/code><\/pre>\n\n\n\n<p>\u4e24\u79cd\u5199\u6cd5\u5b8c\u5168\u662f\u7b49\u4ef7\u7684\u3002<\/p>\n\n\n\n<p>\u7531\u4e8ePython\u7684<strong>\u5b57\u7b26\u4e32\u7c7b\u578b\u662f<code>str<\/code>\uff0c\u5728\u5185\u5b58\u4e2d\u4ee5Unicode\u8868\u793a\uff0c\u4e00\u4e2a\u5b57\u7b26\u5bf9\u5e94\u82e5\u5e72\u4e2a\u5b57\u8282\u3002\u5982\u679c\u8981\u5728\u7f51\u7edc\u4e0a\u4f20\u8f93\uff0c\u6216\u8005\u4fdd\u5b58\u5230\u78c1\u76d8\u4e0a\uff0c\u5c31\u9700\u8981\u628a<code>str<\/code>\u53d8\u4e3a\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u7684<code>bytes<\/code><\/strong>\u3002<\/p>\n\n\n\n<p>Python\u5bf9<code>bytes<\/code>\u7c7b\u578b\u7684\u6570\u636e\u7528\u5e26<code>b<\/code>\u524d\u7f00\u7684\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u8868\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = b'ABC'\n<\/code><\/pre>\n\n\n\n<p>\u8981\u6ce8\u610f\u533a\u5206<code>'ABC'<\/code>\u548c<code>b'ABC'<\/code>\uff0c\u524d\u8005\u662f<code>str<\/code>\uff0c\u540e\u8005\u867d\u7136\u5185\u5bb9\u663e\u793a\u5f97\u548c\u524d\u8005\u4e00\u6837\uff0c\u4f46<code>bytes<\/code>\u7684\u6bcf\u4e2a\u5b57\u7b26\u90fd\u53ea\u5360\u7528\u4e00\u4e2a\u5b57\u8282\u3002<\/p>\n\n\n\n<p>\u4ee5Unicode\u8868\u793a\u7684<code>str<\/code>\u901a\u8fc7<code>encode()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7f16\u7801\u4e3a\u6307\u5b9a\u7684<code>bytes<\/code>\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; 'ABC'.encode('ascii')\nb'ABC'\n&gt;&gt;&gt; '\u4e2d\u6587'.encode('utf-8')\nb'\\xe4\\xb8\\xad\\xe6\\x96\\x87'\n&gt;&gt;&gt; '\u4e2d\u6587'.encode('ascii')\nTraceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;\nUnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)\n<\/code><\/pre>\n\n\n\n<p>\u7eaf\u82f1\u6587\u7684<code>str<\/code>\u53ef\u4ee5\u7528<code>ASCII<\/code>\u7f16\u7801\u4e3a<code>bytes<\/code>\uff0c\u5185\u5bb9\u662f\u4e00\u6837\u7684\uff0c\u542b\u6709\u4e2d\u6587\u7684<code>str<\/code>\u53ef\u4ee5\u7528<code>UTF-8<\/code>\u7f16\u7801\u4e3a<code>bytes<\/code>\u3002\u542b\u6709\u4e2d\u6587\u7684<code>str<\/code>\u65e0\u6cd5\u7528<code>ASCII<\/code>\u7f16\u7801\uff0c\u56e0\u4e3a\u4e2d\u6587\u7f16\u7801\u7684\u8303\u56f4\u8d85\u8fc7\u4e86<code>ASCII<\/code>\u7f16\u7801\u7684\u8303\u56f4\uff0cPython\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<p>\u5728<code>bytes<\/code>\u4e2d\uff0c\u65e0\u6cd5\u663e\u793a\u4e3aASCII\u5b57\u7b26\u7684\u5b57\u8282\uff0c\u7528<code>\\x##<\/code>\u663e\u793a\u3002<\/p>\n\n\n\n<p>\u53cd\u8fc7\u6765\uff0c\u5982\u679c\u6211\u4eec\u4ece\u7f51\u7edc\u6216\u78c1\u76d8\u4e0a\u8bfb\u53d6\u4e86\u5b57\u8282\u6d41\uff0c\u90a3\u4e48\u8bfb\u5230\u7684\u6570\u636e\u5c31\u662f<code>bytes<\/code>\u3002\u8981\u628a<code>bytes<\/code>\u53d8\u4e3a<code>str<\/code>\uff0c\u5c31\u9700\u8981\u7528<code>decode()<\/code>\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; b'ABC'.decode('ascii')\n'ABC'\n&gt;&gt;&gt; b'\\xe4\\xb8\\xad\\xe6\\x96\\x87'.decode('utf-8')\n'\u4e2d\u6587'\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c<code>bytes<\/code>\u4e2d\u5305\u542b\u65e0\u6cd5\u89e3\u7801\u7684\u5b57\u8282\uff0c<code>decode()<\/code>\u65b9\u6cd5\u4f1a\u62a5\u9519\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; b'\\xe4\\xb8\\xad\\xff'.decode('utf-8')\nTraceback (most recent <strong>call<\/strong> <strong>last<\/strong>):\n  ...\nUnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 3: invalid start byte\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c<code>bytes<\/code>\u4e2d\u53ea\u6709\u4e00\u5c0f\u90e8\u5206\u65e0\u6548\u7684\u5b57\u8282\uff0c\u53ef\u4ee5\u4f20\u5165<code>errors='ignore'<\/code>\u5ffd\u7565\u9519\u8bef\u7684\u5b57\u8282\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; b'\\xe4\\xb8\\xad\\xff'.decode('utf-8', errors='ignore')\n'\u4e2d'\n<\/code><\/pre>\n\n\n\n<p>\u8981\u8ba1\u7b97<code>str<\/code>\u5305\u542b\u591a\u5c11\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u7528<code>len()<\/code>\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; len('ABC')\n3\n&gt;&gt;&gt; len('\u4e2d\u6587')\n2\n<\/code><\/pre>\n\n\n\n<p><code>len()<\/code>\u51fd\u6570\u8ba1\u7b97\u7684\u662f<code>str<\/code>\u7684\u5b57\u7b26\u6570\uff0c\u5982\u679c\u6362\u6210<code>bytes<\/code>\uff0c<code>len()<\/code>\u51fd\u6570\u5c31\u8ba1\u7b97\u5b57\u8282\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; len(b'ABC')\n3\n&gt;&gt;&gt; len(b'\\xe4\\xb8\\xad\\xe6\\x96\\x87')\n6\n&gt;&gt;&gt; len('\u4e2d\u6587'.encode('utf-8'))\n6\n<\/code><\/pre>\n\n\n\n<p>\u53ef\u89c1\uff0c1\u4e2a\u4e2d\u6587\u5b57\u7b26\u7ecf\u8fc7UTF-8\u7f16\u7801\u540e\u901a\u5e38\u4f1a\u5360\u75283\u4e2a\u5b57\u8282\uff0c\u800c1\u4e2a\u82f1\u6587\u5b57\u7b26\u53ea\u5360\u75281\u4e2a\u5b57\u8282\u3002<\/p>\n\n\n\n<p>\u5728\u64cd\u4f5c\u5b57\u7b26\u4e32\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u9047\u5230<code>str<\/code>\u548c<code>bytes<\/code>\u7684\u4e92\u76f8\u8f6c\u6362\u3002\u4e3a\u4e86\u907f\u514d\u4e71\u7801\u95ee\u9898\uff0c\u5e94\u5f53\u59cb\u7ec8\u575a\u6301\u4f7f\u7528UTF-8\u7f16\u7801\u5bf9<code>str<\/code>\u548c<code>bytes<\/code>\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n\n\n\n<p>\u7531\u4e8ePython\u6e90\u4ee3\u7801\u4e5f\u662f\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u6240\u4ee5\uff0c\u5f53\u4f60\u7684\u6e90\u4ee3\u7801\u4e2d\u5305\u542b\u4e2d\u6587\u7684\u65f6\u5019\uff0c\u5728\u4fdd\u5b58\u6e90\u4ee3\u7801\u65f6\uff0c\u5c31\u9700\u8981\u52a1\u5fc5\u6307\u5b9a\u4fdd\u5b58\u4e3aUTF-8\u7f16\u7801\u3002\u5f53Python\u89e3\u91ca\u5668\u8bfb\u53d6\u6e90\u4ee3\u7801\u65f6\uff0c\u4e3a\u4e86\u8ba9\u5b83\u6309UTF-8\u7f16\u7801\u8bfb\u53d6\uff0c\u6211\u4eec\u901a\u5e38\u5728\u6587\u4ef6\u5f00\u5934\u5199\u4e0a\u8fd9\u4e24\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>#!\/usr\/bin\/env python3<\/em>\n<em># -*- coding: utf-8 -*-<\/em>\n<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e00\u884c\u6ce8\u91ca\u662f\u4e3a\u4e86\u544a\u8bc9Linux\/OS X\u7cfb\u7edf\uff0c\u8fd9\u662f\u4e00\u4e2aPython\u53ef\u6267\u884c\u7a0b\u5e8f\uff0cWindows\u7cfb\u7edf\u4f1a\u5ffd\u7565\u8fd9\u4e2a\u6ce8\u91ca\uff1b<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u884c\u6ce8\u91ca\u662f\u4e3a\u4e86\u544a\u8bc9Python\u89e3\u91ca\u5668\uff0c\u6309\u7167UTF-8\u7f16\u7801\u8bfb\u53d6\u6e90\u4ee3\u7801\uff0c\u5426\u5219\uff0c\u4f60\u5728\u6e90\u4ee3\u7801\u4e2d\u5199\u7684\u4e2d\u6587\u8f93\u51fa\u53ef\u80fd\u4f1a\u6709\u4e71\u7801\u3002<\/p>\n\n\n\n<p>\u7533\u660e\u4e86UTF-8\u7f16\u7801\u5e76\u4e0d\u610f\u5473\u7740\u4f60\u7684<code>.py<\/code>\u6587\u4ef6\u5c31\u662fUTF-8\u7f16\u7801\u7684\uff0c\u5fc5\u987b\u5e76\u4e14\u8981\u786e\u4fdd\u6587\u672c\u7f16\u8f91\u5668\u6b63\u5728\u4f7f\u7528UTF-8 without BOM\u7f16\u7801\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.liaoxuefeng.com\/files\/attachments\/1008802356788736\" alt=\"set-encoding-in-notepad++\"\/><\/figure>\n\n\n\n<p>\u5982\u679c<code>.py<\/code>\u6587\u4ef6\u672c\u8eab\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u5e76\u4e14\u4e5f\u7533\u660e\u4e86<code># -*- coding: utf-8 -*-<\/code>\uff0c\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u6d4b\u8bd5\u5c31\u53ef\u4ee5\u6b63\u5e38\u663e\u793a\u4e2d\u6587\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.liaoxuefeng.com\/files\/attachments\/1008802515054144\" alt=\"py-chinese-test-in-cmd\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u683c\u5f0f\u5316<\/h3>\n\n\n\n<p>\u6700\u540e\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u662f\u5982\u4f55\u8f93\u51fa\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u7ecf\u5e38\u4f1a\u8f93\u51fa\u7c7b\u4f3c<code>'\u4eb2\u7231\u7684xxx\u4f60\u597d\uff01\u4f60xx\u6708\u7684\u8bdd\u8d39\u662fxx\uff0c\u4f59\u989d\u662fxx'<\/code>\u4e4b\u7c7b\u7684\u5b57\u7b26\u4e32\uff0c\u800cxxx\u7684\u5185\u5bb9\u90fd\u662f\u6839\u636e\u53d8\u91cf\u53d8\u5316\u7684\uff0c\u6240\u4ee5\uff0c\u9700\u8981\u4e00\u79cd\u7b80\u4fbf\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.liaoxuefeng.com\/files\/attachments\/928817906446432\/0\" alt=\"py-str-format\"\/><\/figure>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u91c7\u7528\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\u548cC\u8bed\u8a00\u662f\u4e00\u81f4\u7684\uff0c\u7528<code>%<\/code>\u5b9e\u73b0\uff0c\u4e3e\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; 'Hello, %s' % 'world'\n'Hello, world'\n&gt;&gt;&gt; 'Hi, %s, you have $%d.' % ('Michael', 1000000)\n'Hi, Michael, you have $1000000.'\n<\/code><\/pre>\n\n\n\n<p>\u4f60\u53ef\u80fd\u731c\u5230\u4e86\uff0c<code>%<\/code>\u8fd0\u7b97\u7b26\u5c31\u662f\u7528\u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u3002\u5728\u5b57\u7b26\u4e32\u5185\u90e8\uff0c<code>%s<\/code>\u8868\u793a\u7528\u5b57\u7b26\u4e32\u66ff\u6362\uff0c<code>%d<\/code>\u8868\u793a\u7528\u6574\u6570\u66ff\u6362\uff0c\u6709\u51e0\u4e2a<code>%?<\/code>\u5360\u4f4d\u7b26\uff0c\u540e\u9762\u5c31\u8ddf\u51e0\u4e2a\u53d8\u91cf\u6216\u8005\u503c\uff0c\u987a\u5e8f\u8981\u5bf9\u5e94\u597d\u3002\u5982\u679c\u53ea\u6709\u4e00\u4e2a<code>%?<\/code>\uff0c\u62ec\u53f7\u53ef\u4ee5\u7701\u7565\u3002<\/p>\n\n\n\n<p>\u5e38\u89c1\u7684\u5360\u4f4d\u7b26\u6709\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u5360\u4f4d\u7b26<\/th><th>\u66ff\u6362\u5185\u5bb9<\/th><\/tr><\/thead><tbody><tr><td>%d<\/td><td>\u6574\u6570<\/td><\/tr><tr><td>%f<\/td><td>\u6d6e\u70b9\u6570<\/td><\/tr><tr><td>%s<\/td><td>\u5b57\u7b26\u4e32<\/td><\/tr><tr><td>%x<\/td><td>\u5341\u516d\u8fdb\u5236\u6574\u6570<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5176\u4e2d\uff0c\u683c\u5f0f\u5316\u6574\u6570\u548c\u6d6e\u70b9\u6570\u8fd8\u53ef\u4ee5\u6307\u5b9a\u662f\u5426\u88650\u548c\u6574\u6570\u4e0e\u5c0f\u6570\u7684\u4f4d\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print('%2d-%02d' % (3, 1))\nprint('%.2f' % 3.1415926)\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u4e0d\u592a\u786e\u5b9a\u5e94\u8be5\u7528\u4ec0\u4e48\uff0c<code>%s<\/code>\u6c38\u8fdc\u8d77\u4f5c\u7528\uff0c\u5b83\u4f1a\u628a\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; 'Age: %s. Gender: %s' % (25, True)\n'Age: 25. Gender: True'\n<\/code><\/pre>\n\n\n\n<p>\u6709\u4e9b\u65f6\u5019\uff0c\u5b57\u7b26\u4e32\u91cc\u9762\u7684<code>%<\/code>\u662f\u4e00\u4e2a\u666e\u901a\u5b57\u7b26\u600e\u4e48\u529e\uff1f\u8fd9\u4e2a\u65f6\u5019\u5c31\u9700\u8981\u8f6c\u4e49\uff0c\u7528<code>%%<\/code>\u6765\u8868\u793a\u4e00\u4e2a<code>%<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; 'growth rate: %d %%' % 7\n'growth rate: 7 %'\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">format()<\/h3>\n\n\n\n<p>\u53e6\u4e00\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>format()<\/code>\u65b9\u6cd5\uff0c\u5b83\u4f1a\u7528\u4f20\u5165\u7684\u53c2\u6570\u4f9d\u6b21\u66ff\u6362\u5b57\u7b26\u4e32\u5185\u7684\u5360\u4f4d\u7b26<code>{0}<\/code>\u3001<code>{1}<\/code>\u2026\u2026\uff0c\u4e0d\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u5199\u8d77\u6765\u6bd4%\u8981\u9ebb\u70e6\u5f97\u591a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; 'Hello, {0}, \u6210\u7ee9\u63d0\u5347\u4e86 {1:.1f}%'.format('\u5c0f\u660e', 17.125)\n'Hello, \u5c0f\u660e, \u6210\u7ee9\u63d0\u5347\u4e86 17.1%'\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">f-string<\/h3>\n\n\n\n<p>\u6700\u540e\u4e00\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4ee5<code>f<\/code>\u5f00\u5934\u7684\u5b57\u7b26\u4e32\uff0c\u79f0\u4e4b\u4e3a<code>f-string<\/code>\uff0c\u5b83\u548c\u666e\u901a\u5b57\u7b26\u4e32\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff0c\u5b57\u7b26\u4e32\u5982\u679c\u5305\u542b<code>{xxx}<\/code>\uff0c\u5c31\u4f1a\u4ee5\u5bf9\u5e94\u7684\u53d8\u91cf\u66ff\u6362\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; r = 2.5\n&gt;&gt;&gt; s = 3.14 * r ** 2\n&gt;&gt;&gt; print(f'The area of a circle with radius {r} is {s:.2f}')\nThe area of a circle with radius 2.5 is 19.62\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>{r}<\/code>\u88ab\u53d8\u91cf<code>r<\/code>\u7684\u503c\u66ff\u6362\uff0c<code>{s:.2f}<\/code>\u88ab\u53d8\u91cf<code>s<\/code>\u7684\u503c\u66ff\u6362\uff0c\u5e76\u4e14<code>:<\/code>\u540e\u9762\u7684<code>.2f<\/code>\u6307\u5b9a\u4e86\u683c\u5f0f\u5316\u53c2\u6570\uff08\u5373\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\uff09\uff0c\u56e0\u6b64\uff0c<code>{s:.2f}<\/code>\u7684\u66ff\u6362\u7ed3\u679c\u662f<code>19.62<\/code><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u81eahttps:\/\/liaoxuefeng.com\/books\/python\/basic\/string-enc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-754","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=754"}],"version-history":[{"count":3,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/754\/revisions"}],"predecessor-version":[{"id":2144,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/754\/revisions\/2144"}],"wp:attachment":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=754"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}