{"id":204010,"date":"2025-05-29T13:43:49","date_gmt":"2025-05-29T05:43:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204010\/"},"modified":"2025-05-29T13:43:49","modified_gmt":"2025-05-29T05:43:49","slug":"%e4%bd%bf%e7%94%a8-python-os-%e6%a8%a1%e5%9d%97%e5%92%8c-unicode-%e8%bf%9b%e8%a1%8c%e5%ad%97%e7%ac%a6%e7%bc%96%e7%a0%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204010\/","title":{"rendered":"\u4f7f\u7528 Python os \u6a21\u5757\u548c Unicode \u8fdb\u884c\u5b57\u7b26\u7f16\u7801"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528 Python os \u6a21\u5757\u548c Unicode \u8fdb\u884c\u5b57\u7b26\u7f16\u7801<\/h1>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528 Python os \u6a21\u5757\u548c Unicode \u8fdb\u884c\u5b57\u7b26\u7f16\u7801\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241017\/17291628466710ee5e86911.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4eca\u5929\u6211\u610f\u8bc6\u5230\u6211\u6ca1\u6709\u53d1\u8868\u4e00\u4e9b\u5173\u4e8e\u5982\u4f55\u505a\u7684\u7b14\u8bb0\uff1a<\/p>\n<ul>\n<li>python\uff0c\u4ee5\u53ca<\/li>\n<li>\u5b83\u7684\u201cos\u201d\u6a21\u5757<\/li>\n<\/ul>\n<p>\u5904\u7406\u672a\u6307\u5b9a\u7684\u5b57\u7b26\u7f16\u7801\u3002<\/p>\n<p>\u8fd9\u662f\u6211\u5728\u8ba9\u6211\u7684\u7a0b\u5e8f foldatry \u5904\u7406\u65e7\u6587\u4ef6\u7cfb\u7edf\u65f6\u5fc5\u987b\u89e3\u51b3\u7684\u95ee\u9898\u3002<\/p>\n<p><em>\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4ec5\u6d89\u53ca\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u540d\u79f0\uff0c\u6211\u5c1a\u672a\u68c0\u67e5\u6587\u4ef6\u5185\u5bb9\u7684\u60c5\u51b5\u3002<\/em><\/p>\n<p>\u6211\u7684\u51b3\u5b9a\u662f\uff1a<\/p>\n<ul>\n<li>\u8ba9 python os \u6a21\u5757\u4ece\u6587\u4ef6\u7cfb\u7edf\u83b7\u53d6\u540d\u79f0\u5e76\u5c06\u5b83\u4eec\u653e\u5165 python unicode \u5b57\u7b26\u4e32\u4e2d &#8211; \u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4e0d\u4f1a\u7f16\u5199\u4ee3\u7801\u6765\u544a\u8bc9\u5b83\u5982\u4f55\u89e3\u91ca\u540d\u79f0\u7684\u7f16\u7801\uff0c\u76f4\u5230<strong>\u4e4b\u540e<\/strong> \u5b83\u5c06\u5b83\u4eec\u5b58\u50a8\u5728 python \u53d8\u91cf\u4e2d\uff1b<\/li>\n<li>\u7f16\u5199\u4ee5\u4e0b\u51fd\u6570\u7528\u4e8e\u663e\u793a\u8def\u5f84\u6587\u4ef6\u540d\uff08\u5230\u7ec8\u7aef\u6216\u5c4f\u5e55\u5c0f\u90e8\u4ef6\uff09 &#8211; \u8fd9\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u5426\u5219\u7a0b\u5e8f\u5c31\u4f1a\u5d29\u6e83\u3002<\/li>\n<\/ul>\n<pre>\n python\ndef pfn4print( p_pfn ):\n    return p_pfn.encode('utf-8', 'surrogateescape').decode('cp1252', 'backslashreplace') \n\n\n\n<\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0ccp1252 \u662f iso-8859-1 \u7f16\u7801\u6807\u51c6\u7684 windows-1252 \u8d85\u96c6\u3002<\/p>\n<p>\u8be5\u4ee3\u7801\u7684\u57fa\u672c\u539f\u7406\u662f\uff1a<\/p>\n<ul>\n<li>python os \u6a21\u5757\u9ed8\u8ba4\u5c06\u4ece\u6587\u4ef6\u7cfb\u7edf\u83b7\u53d6\u7684\u5b57\u8282\u653e\u5165 python unicode \u5b57\u7b26\u4e32\u4e2d\uff1b<\/li>\n<li>\u7136\u540e\u6211\u4eec\u60f3\u5c06\u5176\u8f6c\u56de\u539f\u59cb\u5b57\u8282\u5e8f\u5217\uff0c\u8fd9\u662f\u901a\u8fc7encode(&#8216;utf-8&#8217;, &#8216;surrogateescape&#8217;)\u5b9e\u73b0\u7684<\/li>\n<li>\u8fd9\u662f\u6709\u6548\u7684\uff0c\u56e0\u4e3a os \u6a21\u5757\u7684\u6587\u6863\u8bf4\u8fd9\u5c31\u662f\u5236\u4f5c python unicode \u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u5373\u5b83\u4f7f\u7528\u4e86\u201csurrogateescape\u201d<\/li>\n<\/ul>\n<p>\u7136\u540e\u4e3a\u4e86\u663e\u793a\uff0c\u5b83\u4f7f\u7528 windows-1252 \u7f16\u89e3\u7801\u5668\uff0c\u539f\u56e0\u6709\u4e24\u4e2a\uff1a<\/p>\n<ul>\n<li>\u5b83\u5728\u5f88\u957f\u4e00\u6bb5\u65f6\u95f4\u5185\u662f\u6700\u5e38\u89c1\u7684\uff1a\u6bd4\u65e9\u671f\u7684 ms-dos \u96c6\u548c\u5176\u4ed6\u8bed\u8a00\u96c6\u90fd\u957f\uff1b<\/li>\n<li>\u56e0\u4e3a\u5b83\u51e0\u4e4e\u53ef\u4ee5\u8868\u793a 8 \u4f4d\u5b57\u8282\u6a21\u5f0f\u7684\u6240\u6709 256 \u4e2a\u5b57\u7b26\uff0c\u6240\u4ee5\u5b83\u901a\u5e38\u5e94\u8be5\u663e\u793a\u4e00\u4e9b\u5185\u5bb9\uff0c\u5373\u4f7f\u5b83\u4e0d\u662f\u6700\u521d\u7684\u9884\u671f\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<p>\u66ff\u4ee3\u65b9\u6848\u662f\u59cb\u7ec8\u5c06\u6240\u6709\u8def\u5f84\u548c\u6587\u4ef6\u540d\u4f5c\u4e3a\u5b57\u8282\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u8fd9\u4f3c\u4e4e\u5de5\u4f5c\u91cf\u592a\u5927\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u5982\u679c\u60a8\u5bf9 unicode \u4e86\u89e3\u5f88\u591a\uff0c\u60a8\u53ef\u80fd\u4f1a\u77e5\u9053\u5e76\u975e\u6240\u6709\u5b57\u8282\u5e8f\u5217\u5728\u5176\u6807\u51c6\u7f16\u7801\u4e2d\u90fd\u662f\u6709\u6548\u7684\u3002<\/p>\n<p>\u90a3\u4e48\u5f53 python os \u6a21\u5757\u9047\u5230\u8fd9\u4e9b\uff08\u5373\u5728\u5b57\u8282\u5e8f\u5217\u4e2d\uff0c\u5b83\u6ca1\u6709\u88ab\u544a\u77e5\u5982\u4f55\u89e3\u7801\uff09\u65f6\uff0c\u5b83\u4f1a\u505a\u4ec0\u4e48\uff1f <\/p>\n<p>\u55ef\uff0c\u8fd9\u5c31\u662f\u201csurrogateescape\u201d\u7684\u7528\u6b66\u4e4b\u5730\uff0c\u56e0\u4e3a\u8fd9\u5b9e\u9645\u4e0a\u8ba9 python \u5c06\u9519\u8bef\u7684\u5b57\u8282\u5e8f\u5217\u5b58\u50a8\u4e3a\uff1a<\/p>\n<ul>\n<li>\u65e0\u6548\uff1b\u548c<\/li>\n<li>\u5b83\u7684\u987a\u5e8f\u662f\u4ec0\u4e48\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48encode(&#8216;utf-8&#8217;, &#8216;surrogateescape&#8217;) \u7ed9\u6211\u4eec\u8fd4\u56de\u539f\u59cb\u5b57\u8282\u3002<\/p>\n<p>\u73b0\u5728\u6211\u627f\u8ba4\u8fd9\u770b\u8d77\u6765\u6709\u70b9\u201c\u795e\u5947\u201d\u3002\u867d\u7136\u60a8\u5f53\u7136\u53ef\u4ee5\u66f4\u6df1\u5165\u5730\u4e86\u89e3\u5982\u4f55\uff1a<\/p>\n<ul>\n<li>python \u5728\u5185\u90e8\u5904\u7406 unicode\uff1b<\/li>\n<li>\u4ee5\u53ca\u5b83\u77e5\u9053\u7684\u6240\u6709\u4e0d\u540c\u7f16\u7801\uff1b<\/li>\n<li>\u4ee5\u53ca\u7f16\u7801\u548c\u89e3\u7801\u65f6\u53ef\u7528\u7684\u6240\u6709\u9009\u9879\uff1b<\/li>\n<\/ul>\n<p>\u4f46\u6211\u7684\u731c\u6d4b\u662f\uff0c\u4f60\u4e0d\u9700\u8981\u8d70\u90a3\u4e48\u6df1\uff0c\u53ea\u9700\u76f8\u4fe1\u201csurrogateescape\u201d\u5c31\u80fd\u5b8c\u6210\u5b83\u7684\u4f0e\u4fe9\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u8fd9\u90fd\u662f\u4e3a\u4e86<em>\u7a0b\u5e8f\u4e0d\u4f1a\u5d29\u6e83<\/em>\u5904\u7406\u3002\u6700\u7ec8\uff0c\u6b63\u786e\u5904\u7406\u672a\u6307\u5b9a\u7684\u5b57\u7b26\u7f16\u7801\u5c31\u662f\u786e\u5b9a\u54ea\u79cd\u7f16\u7801\u5bf9\u5176\u6709\u6548\u3002\u6709\u4e00\u4e9b\u5de5\u5177\u53ef\u4ee5\u5f88\u597d\u5730\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u4f46\u8981\u786e\u5b9a\u5b83\u9700\u8981\u4eba\u7c7b\u7684\u5224\u65ad &#8211; \u672c\u8d28\u4e0a\u662f\u56e0\u4e3a\u5b83\u662f\u7531\u4eba\u7c7b\u7684\u4e0d\u5e78\u4e8b\u4ef6\u9020\u6210\u7684\u3002<\/p>\n<p>\u9644\uff1a\u4e0b\u9762\u7684\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5176 unicode \u5e8f\u6570\u5217\u8868\uff08\u5373\u4ee3\u7801\u70b9\u5e8f\u5217\uff09\u3002\u65b9\u4fbf\u68c0\u67e5 python \u771f\u6b63\u8ba4\u4e3a\u7684 unicode \u5b57\u7b26\u4e32\u3002<\/p>\n<pre>\n python\ndef string_as_list_of_code_points( p_str):\n    return list( ord(a_char) for a_char in list(p_str) )\n\n\n<\/pre>\n<p>\u8fd9\u6765\u81ea\u6211\u5728 windows 98 \u65f6\u4ee3\u5236\u4f5c\u7684 cd-rom\u3002\u56e0\u6b64\uff0c\u6587\u4ef6\u540d\u80af\u5b9a\u4e0d\u662f\u4ee5 unicode \u5f62\u5f0f\u5b8c\u6210\u7684\u3002<\/p>\n<p>\u6211\u4eec\u7684\u793a\u4f8b\u6587\u4ef6\u540d\u662f\uff1a<\/p>\n<ul>\n<li> \u6bdb\u76ae \u4e2d\u95f4\u7684\u5b57\u6bcd\u662f\u201c\u5e26\u6709\u5206\u97f3\u7b26\u7684\u62c9\u4e01\u6587\u5c0f\u5199\u5b57\u6bcd u\u201d<\/li>\n<\/ul>\n<p>\u5728 cd-rom \u4e0a\uff0c\u7f16\u7801\u662f\u8fd9\u4e09\u4e2a\u5b57\u8282\u7684\u5e8f\u5217\uff1a<\/p>\n<ul>\n<li> 0x66 0xfc 0x72 <\/li>\n<\/ul>\n<p>\u901a\u8fc7 os \u6a21\u5757\u8bfb\u5165 python3 &#8211; \u5b83\u53d8\u6210\u4ee5\u4e0b unicode \u4ee3\u7801\u70b9\u5e8f\u5217\uff1a<\/p>\n<ul>\n<li> u+0066 u+fffd\uff01\uff01fc u+0072 <\/li>\n<\/ul>\n<p>\u5982\u679c\u6211\u4eec\u6210\u529f\u8f6c\u6362 &#8211; \u89c1\u4e0b\u6587 &#8211; \u90a3\u4e48 python \u5c06\u4fdd\u5b58\u8fd9\u4e2a unicode\u201c\u4ee3\u7801\u70b9\u201d\u5e8f\u5217\uff1a<\/p>\n<ul>\n<li> u+0066 u+00fc u+0072 <\/li>\n<\/ul>\n<p>\u5982\u679c python \u5c06\u5176\u5199\u5165 utf-8\uff0c\u5b83\u5c06\u53d8\u6210\u8fd9\u56db\u4e2a\u5b57\u8282\uff1a<\/p>\n<ul>\n<li> 0x66 0xc3 0xbc 0x72 \u662f\uff1a\u4e00\u4e2a\u4e00\u5b57\u8282\u5b57\u7b26\uff0c\u4e00\u4e2a\u4e8c\u5b57\u8282\u5b57\u7b26\uff0c\u4e00\u4e2a\u4e00\u5b57\u8282\u5b57\u7b26\u3002<\/li>\n<\/ul>\n<p>\u6211\u5bf9 windows-nt \u5185\u90e8\u4f7f\u7528\u7684\u7406\u89e3\u662f\u201cucs-2\u201d\uff0c\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\uff0c\u5c06\u662f\uff1a<\/p>\n<ul>\n<li> 0x00 0x66 0x00 0xfc 0x00 0x72 \u662f\u4e09\u4e2a\u4e24\u5b57\u8282\u5b57\u7b26\u3002<\/li>\n<\/ul>\n<p>\u53c2\u89c1\uff1a<\/p>\n<ul>\n<li>\u5e26\u6709\u5206\u97f3\u7b26\u53f7\u7684\u62c9\u4e01\u6587\u5c0f\u5199\u5b57\u6bcd u<\/li>\n<li>\u6765\u81ea unicode \u7ec4\uff1alatin-1 supplement<\/li>\n<\/ul>\n<p>\u597d\u5427\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5b83\u5728 python \u4ea4\u4e92\u5f0f\u4f1a\u8bdd\u4e2d\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u4ee5\u4e0b\u5185\u5bb9\u662f\u4ece\u7ec8\u7aef\u7a97\u53e3\u526a\u4e0b\u6765\u7684\u3002<\/p>\n<pre>\n\n$ python3\npython 3.10.12 (main, sep 11 2024, 15:47:36) [gcc 11.4.0] on linux\ntype \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n&gt;&gt;&gt; s_0 = \"f\u00fcr\"\n&gt;&gt;&gt; print( list( ord(a_char) for a_char in list(s_0) ) )\n[102, 252, 114]\n&gt;&gt;&gt; ba_1 = s_0.encode( \"utf-8\", \"surrogateescape\")\n&gt;&gt;&gt; print( list( ba_1 ) )\n[102, 195, 188, 114]\n&gt;&gt;&gt; ba_2 = s_0.encode(\"cp1252\", \"backslashreplace\")\n&gt;&gt;&gt; print( list( ba_2 ) )\n[102, 252, 114]\n&gt;&gt;&gt; s_1 = ba_2.decode(\"utf-8\", \"surrogateescape\")\n&gt;&gt;&gt; print( list( ord(a_char) for a_char in list(s_1) ) )\n[102, 56572, 114]\n&gt;&gt;&gt; ba_3 = s_1.encode( \"utf-8\", \"surrogateescape\")\n&gt;&gt;&gt; print( list( ba_2 ) )\n[102, 252, 114]\n&gt;&gt;&gt; s_2 = ba_3.decode(\"cp1252\")\n&gt;&gt;&gt; print( list( ord(a_char) for a_char in list(s_2) ) )\n[102, 252, 114]\n&gt;&gt;&gt; print( s_2)\nf\u00fcr\n\n\n\n<\/pre>\n<p>\u56de\u987e\u4e00\u4e0b\u4e4b\u524d\u7684\u8bc4\u8bba\uff1a<\/p>\n<ul>\n<li>ba_2 \u662f\u8be5\u5b57\u7b26\u4e32\u5728 cd-rom \u4e0a\u7684\u5b58\u653e\u65b9\u5f0f<\/li>\n<li>s_1 \u662f\u5f53\u6211\u4eec\u8981\u6c42 python \u64cd\u4f5c\u7cfb\u7edf\u5728\u4e0d\u6307\u5b9a\u7f16\u7801\u7684\u60c5\u51b5\u4e0b\u8bfb\u53d6 ba_2 \u65f6\u53d1\u751f\u7684\u60c5\u51b5 &#8211; \u56e0\u6b64\u5b83\u5047\u5b9a utf-8 \u4f46\u968f\u540e\u5fc5\u987b\u5904\u7406 252 \u4f5c\u4e3a utf-* \u5b57\u7b26\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u65e0\u6548\u7684\u4e8b\u5b9e\uff0c\u6240\u4ee5\u64cd\u4f5c\u7cfb\u7edf\u5c06\u5176\u5b58\u50a8\u4e3a\u4ee3\u7406<\/li>\n<li>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5b83\u662f\u5982\u4f55\u5728 ba_1 \u4e2d\u6b63\u786e\u7f16\u7801\u4e3a utf-8<\/li>\n<\/ul>\n<p>\u6839\u636e\u60a8\u5bf9\u4ee3\u7801\u70b9\u548c\u7f16\u7801\u7684\u719f\u6089\u7a0b\u5ea6\uff0c\u53ef\u80fd\u4f1a\u6709\u4e24\u4e2a\u60ca\u559c\uff1a<\/p>\n<ul>\n<li>\u4e09\u4e2a\u5b57\u7b26\u7684 utf-8 \u7f16\u7801\u9700\u8981\u56db\u4e2a\u5b57\u8282 &#8211; \u672c\u8d28\u4e0a\u662f\u56e0\u4e3a\u201c\u00fc\u201d\u9700\u8981\u4e24\u4e2a\u5b57\u8282<\/li>\n<li>\u201c\u5728 utf-8 \u4e2d\u65e0\u6548\u7684\u4e8b\u60c5\u201d\u7684\u95ee\u9898\u4f1a\u4ee5\u4e00\u79cd\u5947\u602a\u7684\u65b9\u5f0f\u5904\u7406\uff0c\u4f46\u8fd9\u662f\u53ef\u4ee5\u9006\u8f6c\u7684<\/li>\n<\/ul>\n<p>\u540c\u6837\uff0c\u53ef\u80fd<strong>\u4e0d\u4f1a<\/strong>\u5f15\u8d77\u60a8\u6ce8\u610f\u7684\u662f\uff1a<\/p>\n<ul>\n<li>\u8fd9\u4e2a\u793a\u4f8b\u4ecd\u7136<em>\u7b80\u5355<\/em>\uff0c\u56e0\u4e3a\u201c\u00fc\u201d\u7684\u65e7\u5f0f windows-1252 \u5b57\u8282\u7f16\u7801\u4e0e\u5176 unicode \u4ee3\u7801\u70b9\u7f16\u53f7\u76f8\u5339\u914d\uff1a252<\/li>\n<\/ul>\n<p>\u54e6\uff0c\u8fd8\u6709\uff1a<\/p>\n<ul>\n<li>\u6211\u5b9e\u9645\u4e0a\u4e0d\u8bb0\u5f97\u6211\u4ece\u54ea\u91cc\u5f97\u5230 u+fffd!!fc \u7b26\u53f7\u3002<\/li>\n<li>\u6211\u4ec0\u81f3\u53ef\u80fd\u5df2\u7ecf\u7f16\u9020\u4e86\u5b83\uff0c\u4f5c\u4e3a\u4e00\u79cd\u6df7\u5408\u65b9\u5f0f\u6765\u8868\u8fbe\u201c\u5c06\u663e\u793a\u4e3a unicode \u66ff\u6362\u5b57\u7b26\uff0c\u4f46\u77e5\u9053\u5b83\u6765\u81ea\u5341\u516d\u8fdb\u5236 fc\u201d<\/li>\n<li>\u5982\u679c\u4f60\u67e5\u770b python \u4f1a\u8bdd\uff0c\u4f60\u4f1a\u770b\u5230 s_1 \u4e2d\u95f4\u7684\u4ee3\u7801\u70b9\u662f\u5341\u8fdb\u5236 56572\uff0c\u5373\u5341\u516d\u8fdb\u5236 0xdcfc<\/li>\n<\/ul>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u793a\u4f8b\uff0c\u5176\u4e2d\u5b57\u7b26\u7684 windows-1252 \u5b57\u8282\u7f16\u7801\u5b57\u8282<strong>\u4e0d<\/strong>\u4e0e\u5176 unicode \u4ee3\u7801\u70b9\u7f16\u53f7\u5339\u914d\u3002<\/p>\n<ul>\n<li>\u6b27\u5143\u7b26\u53f7<\/li>\n<li>30 \u6b27\u5143<\/li>\n<li>\u5728 windows-1252 \u4e2d\u7f16\u7801\u4e3a 0x80 <\/li>\n<li>\u5728 unicode \u4e2d\uff0c\u8fd9\u662f\u4ee3\u7801\u70b9 u+20ac\uff08\u5341\u8fdb\u5236 8364\uff09<\/li>\n<li>\uff08\u5982\u679c\u60a8\u60f3\u77e5\u9053\uff0c\u8fd9\u79cd\u5dee\u5f02\u662f\u56e0\u4e3a microsoft \u51b3\u5b9a\u4f7f\u7528\u201cc1 \u63a7\u5236\u4ee3\u7801\u201d\u8303\u56f4\u5185\u7684\u4f4d\u7f6e\uff0c\u800c unicode \u9009\u62e9\u4fdd\u7559\u8be5\u8303\u56f4\uff09<\/li>\n<\/ul>\n<p>\u53c2\u89c1\uff1a<\/p>\n<ul>\n<li>\u6b27\u5143\u7b26\u53f7<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u7684\u64cd\u4f5c\u5e8f\u5217\u4e0e\u6211\u4eec\u5728\u793a\u4f8b 1 \u4e2d\u6267\u884c\u7684\u64cd\u4f5c\u76f8\u540c\uff0c\u4f46\u8fd9\u6b21\u4f7f\u7528\u4e86\u4ee5\u6b27\u5143\u7b26\u53f7\u5f00\u5934\u7684\u77ed\u5b57\u7b26\u4e32\u3002<\/p>\n<pre>\n\n&gt;&gt;&gt; s_0 = \"\u20ac30\"\n&gt;&gt;&gt; print( list( ord(a_char) for a_char in list(s_0) ) )\n[8364, 51, 48]\n&gt;&gt;&gt; ba_1 = s_0.encode( \"utf-8\", \"surrogateescape\")\n&gt;&gt;&gt; print( list( ba_1 ) )\n[226, 130, 172, 51, 48]\n&gt;&gt;&gt; ba_2 = s_0.encode(\"cp1252\", \"backslashreplace\")\n&gt;&gt;&gt; print( list( ba_2 ) )\n[128, 51, 48]\n&gt;&gt;&gt; s_1 = ba_2.decode(\"utf-8\", \"surrogateescape\")\n&gt;&gt;&gt; print( list( ord(a_char) for a_char in list(s_1) ) )\n[56448, 51, 48]\n&gt;&gt;&gt; ba_3 = s_1.encode( \"utf-8\", \"surrogateescape\")\n&gt;&gt;&gt; print( list( ba_2 ) )\n[128, 51, 48]\n&gt;&gt;&gt; s_2 = ba_3.decode(\"cp1252\")\n&gt;&gt;&gt; print( list( ord(a_char) for a_char in list(s_2) ) )\n[8364, 51, 48]\n&gt;&gt;&gt; print( s_2)\n\u20ac30\n\n\n\n<\/pre>\n<p>\u8fd9\u6b21\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<ul>\n<li>ba_1 \u662f utf-8 \u7f16\u7801\uff0c\u6ce8\u610f\u6b27\u5143\u7b26\u53f7\u9700\u8981 <em>\u4e09\u4e2a<\/em> \u4e2a\u5b57\u8282<\/li>\n<li>ba_2 \u662f\u8be5\u5b57\u7b26\u4e32\u5728 windows 98 \u7b49\u4e2d\u7684\u5f62\u5f0f<\/li>\n<li>s_1 \u662f\u5f53\u6211\u4eec\u8981\u6c42 python \u64cd\u4f5c\u7cfb\u7edf\u5728\u4e0d\u6307\u5b9a\u7f16\u7801\u7684\u60c5\u51b5\u4e0b\u8bfb\u53d6 ba_2 \u65f6\u53d1\u751f\u7684\u60c5\u51b5 &#8211; \u56e0\u6b64\u5b83\u5047\u5b9a utf-8 \u4f46\u968f\u540e\u5fc5\u987b\u5904\u7406\u5b83\u6240\u5f97\u5230\u7684\u5185\u5bb9<\/li>\n<li>\u6211\u4eec\u4ecd\u7136\u5b58\u5728\u4ee5\u5947\u602a\u7684\u65b9\u5f0f\u5904\u7406\u201c\u5728 utf-8 \u4e2d\u65e0\u6548\u7684\u4e8b\u60c5\u201d\u7684\u95ee\u9898\uff0c\u4f46\u8fd9\u53ef\u4ee5\u9006\u8f6c<\/li>\n<li>\u6b64\u65f6\u4f60\u4f1a\u770b\u5230\uff0cs_1\u524d\u9762\u7684\u7801\u4f4d\u662f\u5341\u8fdb\u5236\u768456448\uff0c\u4e5f\u5c31\u662f\u5341\u516d\u8fdb\u5236\u76840xdc80<\/li>\n<\/ul>\n<p>\u6211\u4e5f\u8bb8\u5e94\u8be5\u8865\u5145\u4e00\u70b9\uff0c\u6211\u5e76\u4e0d\u58f0\u79f0\u8fd9\u91cc\u663e\u793a\u7684\u65b9\u6cd5\u662f\u5b8c\u6574\u7684\u6216\u65e0\u61c8\u53ef\u51fb\u7684\u3002\u8fd9\u53ea\u662f\u6211\u6dfb\u52a0\u5230\u6211\u7684\u7a0b\u5e8f\u4e2d\u4ee5\u5e94\u5bf9\u5b83\u6240\u9047\u5230\u7684\u60c5\u51b5\u7684\u5185\u5bb9\u3002\u6211\u786e\u5b9e\u8ba1\u5212\u5728\u665a\u4e9b\u65f6\u5019\u91cd\u65b0\u5ba1\u89c6\u8fd9\u4e00\u5207\uff0c\u4f46\u8fd8\u6709\u5f88\u591a\u91cd\u8981\u7684\u5176\u4ed6\u529f\u80fd\u5c06\u9996\u5148\u51fa\u73b0\u3002\u6240\u4ee5\uff0c\u6839\u672c\u4e0d\u662f\u201c\u73b0\u5728\u5f88\u5feb\u201d\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u4f7f\u7528 Python os \u6a21\u5757\u548c Unicode \u8fdb\u884c\u5b57\u7b26\u7f16\u7801\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Python os \u6a21\u5757\u548c&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204010","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204010"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204010\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}