{"id":1133027,"date":"2025-01-08T21:02:39","date_gmt":"2025-01-08T13:02:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133027.html"},"modified":"2025-01-08T21:02:41","modified_gmt":"2025-01-08T13:02:41","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86-xe5%e5%8f%98%e6%88%90%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1133027.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06 xe5\u53d8\u6210\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102416\/6994658f-3266-4945-a7cb-121b80e9ab42.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06 xe5\u53d8\u6210\u4e2d\u6587\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5c06 XE5 \u8f6c\u6362\u4e3a\u4e2d\u6587\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u7f16\u7801\u89e3\u7801\u3001\u5229\u7528\u5b57\u5178\u6620\u5c04\u3001\u4f7f\u7528\u5916\u90e8\u5e93\u3002<\/strong> \u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7f16\u7801\u548c\u89e3\u7801\u7684\u65b9\u6cd5\u6765\u8f6c\u6362\u5b57\u7b26\u7f16\u7801\uff1b\u5176\u6b21\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6620\u5c04\u5c06\u7279\u5b9a\u7684\u7f16\u7801\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u4e2d\u6587\u5b57\u7b26\uff1b\u6700\u540e\uff0c\u4f7f\u7528\u5916\u90e8\u5e93\u5982 <code>chardet<\/code> \u6216 <code>codecs<\/code> \u8fdb\u884c\u7f16\u7801\u68c0\u6d4b\u548c\u8f6c\u6362\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u5178\u6620\u5c04\uff0c\u5c06\u7279\u5b9a\u7684\u7f16\u7801\u5bf9\u5e94\u5230\u4e2d\u6587\u5b57\u7b26\u3002\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u4e14\u6613\u4e8e\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7f16\u7801\u89e3\u7801\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u7801\u548c\u89e3\u7801\u7684\u65b9\u6cd5\u6765\u8f6c\u6362\u5b57\u7b26\u7f16\u7801\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>input_str = &#39;xe5&#39;<\/p>\n<p>output_str = input_str.encode(&#39;latin1&#39;).decode(&#39;utf-8&#39;)<\/p>\n<p>print(output_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b57\u5178\u6620\u5c04\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u6620\u5c04\u662f\u4e00\u79cd\u76f4\u63a5\u800c\u6709\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u6784\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5c06\u7279\u5b9a\u7684\u7f16\u7801\u4e0e\u5bf9\u5e94\u7684\u4e2d\u6587\u5b57\u7b26\u8fdb\u884c\u6620\u5c04\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5c06\u7f16\u7801\u4e0e\u4e2d\u6587\u5b57\u7b26\u8fdb\u884c\u6620\u5c04\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>encoding_dict = {<\/p>\n<p>    &#39;xe5&#39;: &#39;\u54c8&#39;,<\/p>\n<p>    # \u6dfb\u52a0\u66f4\u591a\u7684\u6620\u5c04<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b9e\u73b0\u8f6c\u6362\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u7ed9\u5b9a\u7684\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def convert_to_chinese(encoded_str):<\/p>\n<p>    return encoding_dict.get(encoded_str, encoded_str)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>input_str = &#39;xe5&#39;<\/p>\n<p>output_str = convert_to_chinese(input_str)<\/p>\n<p>print(output_str)  # \u8f93\u51fa: \u54c8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5916\u90e8\u5e93<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u7f16\u7801\u8f6c\u6362\u53ef\u80fd\u6bd4\u8f83\u590d\u6742\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5916\u90e8\u5e93\u6765\u5b8c\u6210\u4efb\u52a1\u3002\u4f8b\u5982 <code>chardet<\/code> \u548c <code>codecs<\/code> \u5e93\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528 <code>chardet<\/code> \u5e93<\/h4>\n<\/p>\n<p><p><code>chardet<\/code> \u5e93\u53ef\u4ee5\u81ea\u52a8\u68c0\u6d4b\u7f16\u7801\u5e76\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b89\u88c5 chardet \u5e93<\/p>\n<h2><strong>pip install chardet<\/strong><\/h2>\n<p>import chardet<\/p>\n<p>def detect_and_convert(input_str):<\/p>\n<p>    detected = chardet.detect(input_str.encode())<\/p>\n<p>    encoding = detected[&#39;encoding&#39;]<\/p>\n<p>    return input_str.encode().decode(encoding)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>input_str = &#39;xe5&#39;<\/p>\n<p>output_str = detect_and_convert(input_str)<\/p>\n<p>print(output_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528 <code>codecs<\/code> \u5e93<\/h4>\n<\/p>\n<p><p><code>codecs<\/code> \u5e93\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7075\u6d3b\u7684\u7f16\u7801\u8f6c\u6362\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import codecs<\/p>\n<p>def convert_with_codecs(input_str):<\/p>\n<p>    return codecs.decode(input_str.encode(&#39;latin1&#39;), &#39;utf-8&#39;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>input_str = &#39;xe5&#39;<\/p>\n<p>output_str = convert_with_codecs(input_str)<\/p>\n<p>print(output_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5728Python\u4e2d\u5c06XE5\u7f16\u7801\u8f6c\u6362\u4e3a\u4e2d\u6587\u7684\u51e0\u79cd\u65b9\u6cd5\uff1a<strong>\u7f16\u7801\u89e3\u7801\u3001\u5b57\u5178\u6620\u5c04\u3001\u4f7f\u7528\u5916\u90e8\u5e93<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u201cxe5\u201d\u8f6c\u6362\u4e3a\u4e2d\u6587\uff1f<\/strong><br \/>\u8981\u5c06\u201cxe5\u201d\u8f6c\u6362\u4e3a\u4e2d\u6587\uff0c\u9996\u5148\u9700\u8981\u660e\u786e\u5b83\u7684\u7f16\u7801\u65b9\u5f0f\u3002\u5047\u8bbe\u201cxe5\u201d\u662f\u4ee5\u5341\u516d\u8fdb\u5236\u5f62\u5f0f\u8868\u793a\u7684Unicode\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684\u5b57\u7b26\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u5c06\u5176\u89e3\u7801\u4e3a\u5b57\u8282\uff0c\u7136\u540e\u518d\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>bytes.fromhex()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u8282\uff0c\u518d\u4f7f\u7528<code>.decode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u8282\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u4e0d\u540c\u7f16\u7801\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u7f16\u7801\u65b9\u5f0f\u6765\u5904\u7406\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982UTF-8\u3001GBK\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528<code>str.encode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282\uff0c\u4f7f\u7528<code>bytes.decode()<\/code>\u5c06\u5b57\u8282\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u3002\u9700\u8981\u6839\u636e\u5b57\u7b26\u4e32\u7684\u539f\u59cb\u7f16\u7801\u683c\u5f0f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u786e\u4fdd\u5b57\u7b26\u80fd\u6b63\u786e\u663e\u793a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u6d4b\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u7c7b\u578b\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>chardet<\/code>\u5e93\u6765\u68c0\u6d4b\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u7c7b\u578b\u3002\u8be5\u5e93\u4f1a\u5206\u6790\u8f93\u5165\u6570\u636e\u5e76\u8fd4\u56de\u6700\u53ef\u80fd\u7684\u7f16\u7801\u683c\u5f0f\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u4f7f\u7528<code>pip install chardet<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002\u901a\u8fc7\u8c03\u7528<code>chardet.detect()<\/code>\u51fd\u6570\u5e76\u4f20\u5165\u5b57\u8282\u6570\u636e\uff0c\u53ef\u4ee5\u83b7\u53d6\u7f16\u7801\u4fe1\u606f\uff0c\u4ece\u800c\u9009\u62e9\u5408\u9002\u7684\u89e3\u7801\u65b9\u5f0f\u3002<\/p>\n<p><strong>\u5982\u679c\u201cxe5\u201d\u8868\u793a\u7684\u662f\u67d0\u4e2a\u7279\u5b9a\u7684\u4e2d\u6587\u5b57\u7b26\uff0c\u6211\u8be5\u5982\u4f55\u627e\u5230\u5b83\u5bf9\u5e94\u7684\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>ord()<\/code>\u548c<code>chr()<\/code>\u51fd\u6570\u8fdb\u884c\u5b57\u7b26\u548c\u7f16\u7801\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u5982\u679c\u77e5\u9053\u201cxe5\u201d\u7684\u5341\u516d\u8fdb\u5236\u503c\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u540e\uff0c\u4f7f\u7528<code>chr()<\/code>\u51fd\u6570\u627e\u5230\u5bf9\u5e94\u7684\u4e2d\u6587\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>chr(0xe5)<\/code>\u6765\u83b7\u53d6\u5bf9\u5e94\u7684\u5b57\u7b26\uff0c\u6ce8\u610f\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u7684\u7f16\u7801\u683c\u5f0f\u4e0e\u5b57\u7b26\u96c6\u4e00\u81f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5c06 XE5 \u8f6c\u6362\u4e3a\u4e2d\u6587\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u7f16\u7801\u89e3\u7801\u3001\u5229\u7528\u5b57\u5178\u6620\u5c04\u3001\u4f7f\u7528\u5916\u90e8\u5e93\u3002 \u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 [&hellip;]","protected":false},"author":3,"featured_media":1133032,"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\/1133027"}],"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=1133027"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133027\/revisions"}],"predecessor-version":[{"id":1133034,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133027\/revisions\/1133034"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1133032"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}