{"id":1060018,"date":"2024-12-31T15:30:18","date_gmt":"2024-12-31T07:30:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1060018.html"},"modified":"2024-12-31T15:30:23","modified_gmt":"2024-12-31T07:30:23","slug":"python%e9%87%8cutf-8%e5%a6%82%e4%bd%95%e8%a7%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1060018.html","title":{"rendered":"python\u91ccutf-8\u5982\u4f55\u89e3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/19b0f5b0-2e21-478d-b23f-5bb88dc460d0.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u91ccutf-8\u5982\u4f55\u89e3\u7801\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0cUTF-8\u7684\u89e3\u7801\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528 <code>str<\/code> \u7c7b\u7684 <code>encode()<\/code> \u548c <code>decode()<\/code> \u65b9\u6cd5\u3001 <code>codecs<\/code> \u6a21\u5757\u3001\u4ee5\u53ca <code>bytes<\/code> \u5bf9\u8c61\u7684 <code>decode()<\/code> \u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5c06UTF-8\u7f16\u7801\u7684\u5b57\u8282\u6570\u636e\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528 <code>str<\/code> \u7c7b\u7684 <code>encode()<\/code> \u548c <code>decode()<\/code> \u65b9\u6cd5\u3001<code>bytes<\/code> \u5bf9\u8c61\u7684 <code>decode()<\/code> \u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u5728Python\u4e2d\u5904\u7406UTF-8\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u5c06\u5b57\u8282\u6570\u636e\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528 <code>decode()<\/code> \u65b9\u6cd5\u6765\u5b8c\u6210\u8fd9\u4e00\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5305\u542bUTF-8\u7f16\u7801\u5b57\u8282\u6570\u636e\u7684\u53d8\u91cf <code>byte_data<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>byte_data.decode(&#39;utf-8&#39;)<\/code> \u5c06\u5176\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u4e14\u6613\u4e8e\u4f7f\u7528\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528 <code>decode()<\/code> \u65b9\u6cd5\u89e3\u7801UTF-8<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u89e3\u7801UTF-8\u7f16\u7801\u7684\u5b57\u8282\u6570\u636e\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 <code>decode()<\/code> \u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><p>1.1\u3001\u57fa\u672c\u7528\u6cd5<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2aUTF-8\u7f16\u7801\u7684\u5b57\u8282\u6570\u636e<\/p>\n<p>byte_data = b&#39;\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd&#39;<\/p>\n<h2><strong>\u4f7f\u7528decode()\u65b9\u6cd5\u5c06\u5b57\u8282\u6570\u636e\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>decoded_string = byte_data.decode(&#39;utf-8&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u89e3\u7801\u540e\u7684\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>print(decoded_string)  # \u8f93\u51fa\uff1a\u4f60\u597d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>byte_data<\/code> \u662f\u4e00\u4e2a\u5305\u542bUTF-8\u7f16\u7801\u5b57\u8282\u6570\u636e\u7684\u53d8\u91cf\u3002\u901a\u8fc7\u8c03\u7528 <code>byte_data.decode(&#39;utf-8&#39;)<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u5176\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32 <code>decoded_string<\/code>\u3002<\/p>\n<\/p>\n<p><p>1.2\u3001\u5904\u7406\u89e3\u7801\u9519\u8bef<\/p>\n<\/p>\n<p><p>\u5728\u89e3\u7801\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u65e0\u6cd5\u89e3\u7801\u7684\u5b57\u8282\u6570\u636e\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u6765\u63a7\u5236\u89e3\u7801\u884c\u4e3a\u3002\u5e38\u7528\u7684\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u5305\u62ec <code>ignore<\/code>\uff08\u5ffd\u7565\u9519\u8bef\uff09\u3001<code>replace<\/code>\uff08\u7528\u66ff\u4ee3\u5b57\u7b26\u66ff\u6362\u9519\u8bef\uff09\u548c <code>strict<\/code>\uff08\u4e25\u683c\u6a21\u5f0f\uff0c\u629b\u51fa\u5f02\u5e38\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u65e0\u6548\u5b57\u8282\u6570\u636e\u7684\u53d8\u91cf<\/p>\n<p>invalid_byte_data = b&#39;\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd\\x80&#39;<\/p>\n<h2><strong>\u4f7f\u7528ignore\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u89e3\u7801<\/strong><\/h2>\n<p>decoded_string_ignore = invalid_byte_data.decode(&#39;utf-8&#39;, errors=&#39;ignore&#39;)<\/p>\n<p>print(decoded_string_ignore)  # \u8f93\u51fa\uff1a\u4f60\u597d<\/p>\n<h2><strong>\u4f7f\u7528replace\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u89e3\u7801<\/strong><\/h2>\n<p>decoded_string_replace = invalid_byte_data.decode(&#39;utf-8&#39;, errors=&#39;replace&#39;)<\/p>\n<p>print(decoded_string_replace)  # \u8f93\u51fa\uff1a\u4f60\u597d\ufffd<\/p>\n<h2><strong>\u4f7f\u7528strict\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u89e3\u7801\uff08\u9ed8\u8ba4\uff09<\/strong><\/h2>\n<p>try:<\/p>\n<p>    decoded_string_strict = invalid_byte_data.decode(&#39;utf-8&#39;, errors=&#39;strict&#39;)<\/p>\n<p>except UnicodeDecodeError as e:<\/p>\n<p>    print(f&quot;\u89e3\u7801\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u6307\u5b9a\u4e0d\u540c\u7684\u9519\u8bef\u5904\u7406\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u89e3\u7801\u8fc7\u7a0b\u4e2d\u7684\u9519\u8bef\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528 <code>codecs<\/code> \u6a21\u5757\u89e3\u7801UTF-8<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684 <code>codecs<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u5173\u4e8e\u7f16\u7801\u548c\u89e3\u7801\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528 <code>codecs<\/code> \u6a21\u5757\u4e2d\u7684 <code>decode()<\/code> \u65b9\u6cd5\u6765\u89e3\u7801UTF-8\u7f16\u7801\u7684\u5b57\u8282\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import codecs<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2aUTF-8\u7f16\u7801\u7684\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>byte_data = b&#39;\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd&#39;<\/p>\n<h2><strong>\u4f7f\u7528codecs\u6a21\u5757\u7684decode()\u65b9\u6cd5\u5c06\u5b57\u8282\u6570\u636e\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>decoded_string = codecs.decode(byte_data, &#39;utf-8&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u89e3\u7801\u540e\u7684\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>print(decoded_string)  # \u8f93\u51fa\uff1a\u4f60\u597d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>codecs<\/code> \u6a21\u5757\u7684 <code>decode()<\/code> \u65b9\u6cd5\u4e0e <code>bytes<\/code> \u5bf9\u8c61\u7684 <code>decode()<\/code> \u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u548c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528 <code>str<\/code> \u7c7b\u7684 <code>encode()<\/code> \u548c <code>decode()<\/code> \u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u5bf9\u8c61\uff08<code>str<\/code> \u7c7b\uff09\u4e5f\u63d0\u4f9b\u4e86 <code>encode()<\/code> \u548c <code>decode()<\/code> \u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5728\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u6570\u636e\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>3.1\u3001\u5b57\u7b26\u4e32\u7f16\u7801\u4e3aUTF-8\u5b57\u8282\u6570\u636e<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32<\/p>\n<p>string_data = &#39;\u4f60\u597d&#39;<\/p>\n<h2><strong>\u4f7f\u7528encode()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3aUTF-8\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>encoded_byte_data = string_data.encode(&#39;utf-8&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u7f16\u7801\u540e\u7684\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>print(encoded_byte_data)  # \u8f93\u51fa\uff1ab&#39;\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3.2\u3001\u5b57\u8282\u6570\u636e\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2aUTF-8\u7f16\u7801\u7684\u5b57\u8282\u6570\u636e<\/p>\n<p>byte_data = b&#39;\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd&#39;<\/p>\n<h2><strong>\u4f7f\u7528decode()\u65b9\u6cd5\u5c06\u5b57\u8282\u6570\u636e\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>decoded_string = byte_data.decode(&#39;utf-8&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u89e3\u7801\u540e\u7684\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>print(decoded_string)  # \u8f93\u51fa\uff1a\u4f60\u597d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7 <code>str<\/code> \u7c7b\u7684 <code>encode()<\/code> \u548c <code>decode()<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u6570\u636e\u4e4b\u95f4\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u6587\u4ef6\u4e2d\u7684UTF-8\u7f16\u7801\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u8bfb\u53d6\u548c\u5904\u7406\u5305\u542bUTF-8\u7f16\u7801\u6570\u636e\u7684\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u6765\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>4.1\u3001\u8bfb\u53d6UTF-8\u7f16\u7801\u7684\u6587\u4ef6<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84<\/p>\n<p>file_path = &#39;utf8_text.txt&#39;<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file_content = file.read()<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>print(file_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u6307\u5b9a <code>encoding=&#39;utf-8&#39;<\/code> \u53c2\u6570\uff0c\u53ef\u4ee5\u786e\u4fdd\u4ee5UTF-8\u7f16\u7801\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>4.2\u3001\u5199\u5165UTF-8\u7f16\u7801\u7684\u6587\u4ef6<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84<\/p>\n<p>file_path = &#39;utf8_text.txt&#39;<\/p>\n<h2><strong>\u5b9a\u4e49\u8981\u5199\u5165\u7684\u5b57\u7b26\u4e32\u6570\u636e<\/strong><\/h2>\n<p>string_data = &#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;<\/p>\n<h2><strong>\u6253\u5f00\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9<\/strong><\/h2>\n<p>with open(file_path, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(string_data)<\/p>\n<h2><strong>\u8f93\u51fa\u63d0\u793a\u4fe1\u606f<\/strong><\/h2>\n<p>print(&#39;\u6587\u4ef6\u5199\u5165\u5b8c\u6210&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u6307\u5b9a <code>encoding=&#39;utf-8&#39;<\/code> \u53c2\u6570\uff0c\u53ef\u4ee5\u786e\u4fdd\u4ee5UTF-8\u7f16\u7801\u5199\u5165\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u7f51\u7edc\u4f20\u8f93\u4e2d\u7684UTF-8\u7f16\u7801\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5904\u7406UTF-8\u7f16\u7801\u7684\u6570\u636e\u4f20\u8f93\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u7f51\u7edc\u5e93\u548c\u7f16\u7801\u89e3\u7801\u65b9\u6cd5\u6765\u5904\u7406\u7f51\u7edc\u4f20\u8f93\u4e2d\u7684UTF-8\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>5.1\u3001\u53d1\u9001UTF-8\u7f16\u7801\u7684\u6570\u636e<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u5b9a\u4e49\u670d\u52a1\u5668\u5730\u5740\u548c\u7aef\u53e3<\/strong><\/h2>\n<p>server_address = (&#39;localhost&#39;, 8080)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aTCP\/IP\u5957\u63a5\u5b57<\/strong><\/h2>\n<p>sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u8fde\u63a5\u670d\u52a1\u5668<\/strong><\/h2>\n<p>sock.connect(server_address)<\/p>\n<p>try:<\/p>\n<p>    # \u5b9a\u4e49\u8981\u53d1\u9001\u7684\u5b57\u7b26\u4e32\u6570\u636e<\/p>\n<p>    string_data = &#39;\u4f60\u597d\uff0c\u670d\u52a1\u5668\uff01&#39;<\/p>\n<p>    # \u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3aUTF-8\u5b57\u8282\u6570\u636e<\/p>\n<p>    byte_data = string_data.encode(&#39;utf-8&#39;)<\/p>\n<p>    # \u53d1\u9001\u5b57\u8282\u6570\u636e<\/p>\n<p>    sock.sendall(byte_data)<\/p>\n<p>    # \u8f93\u51fa\u63d0\u793a\u4fe1\u606f<\/p>\n<p>    print(&#39;\u6570\u636e\u53d1\u9001\u5b8c\u6210&#39;)<\/p>\n<p>finally:<\/p>\n<p>    # \u5173\u95ed\u5957\u63a5\u5b57<\/p>\n<p>    sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u8c03\u7528 <code>encode(&#39;utf-8&#39;)<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3aUTF-8\u5b57\u8282\u6570\u636e\uff0c\u7136\u540e\u901a\u8fc7\u5957\u63a5\u5b57\u53d1\u9001\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>5.2\u3001\u63a5\u6536\u5e76\u89e3\u7801UTF-8\u7f16\u7801\u7684\u6570\u636e<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u5b9a\u4e49\u670d\u52a1\u5668\u5730\u5740\u548c\u7aef\u53e3<\/strong><\/h2>\n<p>server_address = (&#39;localhost&#39;, 8080)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aTCP\/IP\u5957\u63a5\u5b57<\/strong><\/h2>\n<p>sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u7ed1\u5b9a\u670d\u52a1\u5668\u5730\u5740\u548c\u7aef\u53e3<\/strong><\/h2>\n<p>sock.bind(server_address)<\/p>\n<h2><strong>\u76d1\u542c\u4f20\u5165\u8fde\u63a5<\/strong><\/h2>\n<p>sock.listen(1)<\/p>\n<p>print(&#39;\u7b49\u5f85\u8fde\u63a5...&#39;)<\/p>\n<h2><strong>\u63a5\u53d7\u8fde\u63a5<\/strong><\/h2>\n<p>connection, client_address = sock.accept()<\/p>\n<p>try:<\/p>\n<p>    print(&#39;\u8fde\u63a5\u6765\u81ea:&#39;, client_address)<\/p>\n<p>    # \u63a5\u6536\u6570\u636e<\/p>\n<p>    byte_data = connection.recv(1024)<\/p>\n<p>    # \u5c06\u5b57\u8282\u6570\u636e\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32<\/p>\n<p>    decoded_string = byte_data.decode(&#39;utf-8&#39;)<\/p>\n<p>    # \u8f93\u51fa\u89e3\u7801\u540e\u7684\u5b57\u7b26\u4e32<\/p>\n<p>    print(&#39;\u63a5\u6536\u5230\u7684\u6570\u636e:&#39;, decoded_string)<\/p>\n<p>finally:<\/p>\n<p>    # \u5173\u95ed\u8fde\u63a5<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u8c03\u7528 <code>decode(&#39;utf-8&#39;)<\/code> \u65b9\u6cd5\u5c06\u63a5\u6536\u5230\u7684\u5b57\u8282\u6570\u636e\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u8f93\u51fa\u89e3\u7801\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406JSON\u6570\u636e\u4e2d\u7684UTF-8\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0c\u901a\u5e38\u4f1a\u6d89\u53caUTF-8\u7f16\u7801\u7684\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684 <code>json<\/code> \u6a21\u5757\u6765\u5904\u7406JSON\u6570\u636e\u7684\u7f16\u7801\u548c\u89e3\u7801\u3002<\/p>\n<\/p>\n<p><p>6.1\u3001\u5c06Python\u5bf9\u8c61\u7f16\u7801\u4e3aJSON\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2aPython\u5bf9\u8c61<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;message&#39;: &#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;<\/p>\n<p>}<\/p>\n<h2><strong>\u5c06Python\u5bf9\u8c61\u7f16\u7801\u4e3aJSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_string = json.dumps(data, ensure_ascii=False)<\/p>\n<h2><strong>\u8f93\u51faJSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>print(json_string)  # \u8f93\u51fa\uff1a{&quot;message&quot;: &quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u8bbe\u7f6e <code>ensure_ascii=False<\/code> \u53c2\u6570\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u7f16\u7801JSON\u5b57\u7b26\u4e32\u65f6\u4fdd\u7559\u975eASCII\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>6.2\u3001\u5c06JSON\u5b57\u7b26\u4e32\u89e3\u7801\u4e3aPython\u5bf9\u8c61<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2aJSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_string = &#39;{&quot;message&quot;: &quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01&quot;}&#39;<\/p>\n<h2><strong>\u5c06JSON\u5b57\u7b26\u4e32\u89e3\u7801\u4e3aPython\u5bf9\u8c61<\/strong><\/h2>\n<p>data = json.loads(json_string)<\/p>\n<h2><strong>\u8f93\u51fa\u89e3\u7801\u540e\u7684Python\u5bf9\u8c61<\/strong><\/h2>\n<p>print(data)  # \u8f93\u51fa\uff1a{&#39;message&#39;: &#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u8c03\u7528 <code>json.loads()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5c06JSON\u5b57\u7b26\u4e32\u89e3\u7801\u4e3aPython\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u7684UTF-8\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406UTF-8\u7f16\u7801\u7684\u8f93\u5165\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684 <code>argparse<\/code> \u6a21\u5757\u6765\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5e76\u786e\u4fdd\u6b63\u786e\u5904\u7406UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<h2><strong>\u521b\u5efaArgumentParser\u5bf9\u8c61<\/strong><\/h2>\n<p>parser = argparse.ArgumentParser(description=&#39;\u5904\u7406UTF-8\u7f16\u7801\u7684\u547d\u4ee4\u884c\u53c2\u6570&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/h2>\n<p>parser.add_argument(&#39;message&#39;, type=str, help=&#39;\u8f93\u5165\u7684\u6d88\u606f&#39;)<\/p>\n<h2><strong>\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/h2>\n<p>args = parser.parse_args()<\/p>\n<h2><strong>\u8f93\u51fa\u8f93\u5165\u7684\u6d88\u606f<\/strong><\/h2>\n<p>print(&#39;\u8f93\u5165\u7684\u6d88\u606f:&#39;, args.message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528 <code>argparse<\/code> \u6a21\u5757\uff0c\u53ef\u4ee5\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u5e76\u786e\u4fdd\u6b63\u786e\u5904\u7406UTF-8\u7f16\u7801\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5904\u7406UTF-8\u7f16\u7801\u7684\u89e3\u7801\u64cd\u4f5c\u975e\u5e38\u5e38\u89c1\u3002\u901a\u8fc7\u4f7f\u7528 <code>decode()<\/code> \u65b9\u6cd5\u3001<code>codecs<\/code> \u6a21\u5757\u3001<code>str<\/code> \u7c7b\u7684 <code>encode()<\/code> \u548c <code>decode()<\/code> \u65b9\u6cd5\uff0c\u4ee5\u53ca\u5904\u7406\u6587\u4ef6\u3001\u7f51\u7edc\u4f20\u8f93\u3001JSON\u6570\u636e\u548c\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u7684UTF-8\u7f16\u7801\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u548c\u89e3\u7801UTF-8\u7f16\u7801\u7684\u6570\u636e\u3002\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u5904\u7406\u56fd\u9645\u5316\u548c\u591a\u8bed\u8a00\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528UTF-8\u7f16\u7801\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>UTF-8\u7f16\u7801\u662f\u4e00\u79cd\u5e38\u7528\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u8868\u793a\u4e16\u754c\u4e0a\u51e0\u4e4e\u6240\u6709\u7684\u5b57\u7b26\u3002\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662f\u4ee5Unicode\u5f62\u5f0f\u5b58\u50a8\u7684\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2aUTF-8\u7f16\u7801\u7684\u5b57\u8282\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7.decode(&#39;utf-8&#39;)\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3aUnicode\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">byte_string = b&#39;\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd&#39;  # UTF-8\u7f16\u7801\u7684\u5b57\u8282\u4e32\nunicode_string = byte_string.decode(&#39;utf-8&#39;)  # \u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32\nprint(unicode_string)  # \u8f93\u51fa\uff1a\u4f60\u597d\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u9047\u5230\u89e3\u7801\u9519\u8bef\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5728\u89e3\u7801\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5b57\u7b26\u65e0\u6cd5\u88ab\u6b63\u786e\u89e3\u7801\u7684\u60c5\u51b5\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>errors<\/code>\u53c2\u6570\u6765\u63a7\u5236\u9519\u8bef\u5904\u7406\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>errors=&#39;ignore&#39;<\/code>\u4f1a\u5ffd\u7565\u65e0\u6cd5\u89e3\u7801\u7684\u5b57\u7b26\uff0c\u800c\u4f7f\u7528<code>errors=&#39;replace&#39;<\/code>\u5219\u4f1a\u7528\u4e00\u4e2a\u66ff\u4ee3\u5b57\u7b26\uff08\u901a\u5e38\u662f?\uff09\u4ee3\u66ff\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">byte_string = b&#39;\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd\\x80&#39;  # \u5305\u542b\u65e0\u6cd5\u89e3\u7801\u7684\u5b57\u8282\nunicode_string = byte_string.decode(&#39;utf-8&#39;, errors=&#39;replace&#39;)  # \u7528?\u66ff\u4ee3\u9519\u8bef\u5b57\u7b26\nprint(unicode_string)  # \u8f93\u51fa\uff1a\u4f60\u597d?\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5c06Unicode\u5b57\u7b26\u4e32\u91cd\u65b0\u7f16\u7801\u4e3aUTF-8\u683c\u5f0f\uff1f<\/strong><br \/>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u56deUTF-8\u7f16\u7801\u7684\u5b57\u8282\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528.encode(&#39;utf-8&#39;)\u65b9\u6cd5\u5b8c\u6210\u6b64\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">unicode_string = &#39;\u4f60\u597d&#39;\nbyte_string = unicode_string.encode(&#39;utf-8&#39;)  # \u7f16\u7801\u4e3aUTF-8\u5b57\u8282\u4e32\nprint(byte_string)  # \u8f93\u51fa\uff1ab&#39;\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd&#39;\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728Python\u4e2d\u9ad8\u6548\u5730\u5904\u7406UTF-8\u7f16\u7801\u548c\u89e3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0cUTF-8\u7684\u89e3\u7801\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528 str \u7c7b\u7684 encode() \u548c deco [&hellip;]","protected":false},"author":3,"featured_media":1060029,"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\/1060018"}],"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=1060018"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060018\/revisions"}],"predecessor-version":[{"id":1060032,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060018\/revisions\/1060032"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1060029"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1060018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1060018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1060018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}