{"id":1171807,"date":"2025-01-15T16:40:09","date_gmt":"2025-01-15T08:40:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1171807.html"},"modified":"2025-01-15T16:40:11","modified_gmt":"2025-01-15T08:40:11","slug":"python%e9%87%8c%e9%9d%a2%e5%a6%82%e4%bd%95%e7%bc%96%e7%a0%81%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1171807.html","title":{"rendered":"python\u91cc\u9762\u5982\u4f55\u7f16\u7801\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074041\/14f25a0b-d48e-46f8-a9a5-20e1ef384f04.webp\" alt=\"python\u91cc\u9762\u5982\u4f55\u7f16\u7801\u683c\u5f0f\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u7f16\u7801\u683c\u5f0f\u7684\u5904\u7406\u4e3b\u8981\u6d89\u53ca\u5b57\u7b26\u7f16\u7801\u548c\u89e3\u7801\u3002<strong>\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u5904\u7406\u6587\u672c\u3001\u4f7f\u7528<code>encode()<\/code>\u548c<code>decode()<\/code>\u65b9\u6cd5\u3001\u5904\u7406\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f<\/strong>\u662f\u5173\u952e\u3002<strong>\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u5904\u7406\u6587\u672c<\/strong>\u975e\u5e38\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u786e\u4fdd\u4e86\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u5904\u7406\u5404\u79cd\u5b57\u7b26\u96c6\uff0c\u907f\u514d\u51fa\u73b0\u4e71\u7801\u6216\u7f16\u7801\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u5904\u7406\u6587\u672c<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u7f16\u7801\u662f\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u8fc7\u7a0b\uff0c\u800c\u89e3\u7801\u662f\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u56de\u5b57\u7b26\u7684\u8fc7\u7a0b\u3002\u5728Python\u4e2d\uff0c\u9ed8\u8ba4\u7684\u5b57\u7b26\u7f16\u7801\u662fUTF-8\uff0c\u8fd9\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u80fd\u591f\u8868\u793a\u51e0\u4e4e\u6240\u6709\u7684\u5b57\u7b26\u3002\u4e3a\u4e86\u786e\u4fdd\u7a0b\u5e8f\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\u4e0d\u4f1a\u51fa\u73b0\u95ee\u9898\uff0c\u6700\u597d\u5728\u5904\u7406\u6587\u672c\u65f6\u663e\u5f0f\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u9700\u8981\u5904\u7406\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684\u6587\u672c\u6587\u4ef6\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u65f6\u51fa\u73b0\u7f16\u7801\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u6b63\u786e\u5904\u7406\u6587\u672c\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<h2><strong>\u5199\u5165\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f<\/strong><\/h2>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u663e\u5f0f\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u786e\u4fdd\u4e86\u7a0b\u5e8f\u5728\u5904\u7406\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684\u6587\u672c\u6587\u4ef6\u65f6\u4e0d\u4f1a\u51fa\u73b0\u7f16\u7801\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u7b26\u7f16\u7801\u57fa\u7840<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b57\u7b26\u7f16\u7801\u7684\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u7f16\u7801\u662f\u4e00\u79cd\u5c06\u5b57\u7b26\u6620\u5c04\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u89c4\u5219\u3002\u5e38\u89c1\u7684\u5b57\u7b26\u7f16\u7801\u5305\u62ecASCII\u3001UTF-8\u3001UTF-16\u548cISO-8859-1\u7b49\u3002\u4e0d\u540c\u7684\u5b57\u7b26\u7f16\u7801\u4f7f\u7528\u4e0d\u540c\u7684\u89c4\u5219\u6765\u8868\u793a\u5b57\u7b26\uff0c\u56e0\u6b64\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\uff0c\u4e86\u89e3\u5b57\u7b26\u7f16\u7801\u7684\u6982\u5ff5\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p>ASCII\u662f\u4e00\u79cd\u65e9\u671f\u7684\u5b57\u7b26\u7f16\u7801\u6807\u51c6\uff0c\u4f7f\u75287\u4f4d\u4e8c\u8fdb\u5236\u6570\u8868\u793a128\u4e2a\u5b57\u7b26\uff0c\u5305\u62ec\u82f1\u6587\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u57fa\u672c\u7684\u6807\u70b9\u7b26\u53f7\u3002\u7531\u4e8e\u53ea\u80fd\u8868\u793a128\u4e2a\u5b57\u7b26\uff0cASCII\u65e0\u6cd5\u8868\u793a\u975e\u82f1\u8bed\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>UTF-8\u662f\u4e00\u79cd\u53d8\u957f\u5b57\u7b26\u7f16\u7801\uff0c\u4f7f\u75281\u52304\u4e2a\u5b57\u8282\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\u3002\u5b83\u5177\u6709\u826f\u597d\u7684\u5411\u540e\u517c\u5bb9\u6027\uff0c\u80fd\u591f\u8868\u793a\u51e0\u4e4e\u6240\u6709\u7684\u5b57\u7b26\u96c6\uff0c\u662f\u73b0\u4ee3\u5e94\u7528\u4e2d\u6700\u5e38\u7528\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001Python\u4e2d\u7684\u5b57\u7b26\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u7c7b\u578b\u5206\u4e3a\u4e24\u79cd\uff1a<code>str<\/code>\u548c<code>bytes<\/code>\u3002<code>str<\/code>\u7c7b\u578b\u8868\u793aUnicode\u5b57\u7b26\u4e32\uff0c<code>bytes<\/code>\u7c7b\u578b\u8868\u793a\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u5728Python 3\u4e2d\uff0c\u9ed8\u8ba4\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u662fUnicode\u5b57\u7b26\u4e32\uff0c\u8fd9\u610f\u5473\u7740\u5b57\u7b26\u4e32\u6570\u636e\u5728\u5185\u90e8\u4f7f\u7528Unicode\u7f16\u7801\u8868\u793a\u3002<\/p>\n<\/p>\n<p><p>\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\uff0c\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e<\/p>\n<p>text = &quot;\u4f60\u597d&quot;<\/p>\n<p>encoded_text = text.encode(&#39;utf-8&#39;)<\/p>\n<h2><strong>\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>decoded_text = encoded_text.decode(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u663e\u5f0f\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u7f16\u7801\u548c\u89e3\u7801\u8fc7\u7a0b\u4e2d\u7684\u4e00\u81f4\u6027\uff0c\u907f\u514d\u51fa\u73b0\u7f16\u7801\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6587\u4ef6\u7f16\u7801\u5904\u7406<\/h3>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u975e\u5e38\u91cd\u8981\u3002\u5982\u679c\u6587\u4ef6\u5305\u542b\u975eASCII\u5b57\u7b26\uff0c\u4f7f\u7528\u9ed8\u8ba4\u7f16\u7801\u683c\u5f0f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bfb\u53d6\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u8bfb\u53d6\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a\u7f16\u7801\u683c\u5f0f<code>utf-8<\/code>\uff0c\u786e\u4fdd\u4e86\u6587\u4ef6\u8bfb\u53d6\u8fc7\u7a0b\u4e2d\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5199\u5165\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u5728\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u540c\u6837\u9700\u8981\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u4ee5\u786e\u4fdd\u5199\u5165\u7684\u6587\u672c\u6570\u636e\u80fd\u591f\u6b63\u786e\u8868\u793a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a\u7f16\u7801\u683c\u5f0f<code>utf-8<\/code>\uff0c\u786e\u4fdd\u4e86\u6587\u4ef6\u5199\u5165\u8fc7\u7a0b\u4e2d\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u4e0d\u540c\u7f16\u7801\u683c\u5f0f\u7684\u6587\u672c<\/h3>\n<\/p>\n<p><h4>1\u3001\u8f6c\u6362\u7f16\u7801\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e0d\u540c\u7f16\u7801\u683c\u5f0f\u7684\u6587\u672c\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u6587\u672c\u4ece\u4e00\u79cd\u7f16\u7801\u683c\u5f0f\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7f16\u7801\u683c\u5f0f\u3002\u53ef\u4ee5\u5148\u5c06\u6587\u672c\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u518d\u7f16\u7801\u4e3a\u76ee\u6807\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06GBK\u7f16\u7801\u7684\u6587\u672c\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801<\/p>\n<p>with open(&#39;gbk_text.txt&#39;, &#39;r&#39;, encoding=&#39;gbk&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>with open(&#39;utf8_text.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5148\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u518d\u7f16\u7801\u4e3a\u76ee\u6807\u7f16\u7801\u683c\u5f0f\uff0c\u5b9e\u73b0\u4e86\u7f16\u7801\u683c\u5f0f\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u672a\u77e5\u7f16\u7801\u683c\u5f0f\u7684\u6587\u672c<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u672a\u77e5\u7f16\u7801\u683c\u5f0f\u7684\u6587\u672c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>chardet<\/code>\u5e93\u6765\u68c0\u6d4b\u6587\u672c\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import chardet<\/p>\n<h2><strong>\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e<\/strong><\/h2>\n<p>with open(&#39;unknown_encoding_text.txt&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<h2><strong>\u68c0\u6d4b\u7f16\u7801\u683c\u5f0f<\/strong><\/h2>\n<p>result = chardet.detect(data)<\/p>\n<p>encoding = result[&#39;encoding&#39;]<\/p>\n<h2><strong>\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>text = data.decode(encoding)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u68c0\u6d4b\u6587\u672c\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u6b63\u786e\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32\uff0c\u907f\u514d\u51fa\u73b0\u7f16\u7801\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7f16\u7801\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u7f16\u7801\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u7801\u548c\u89e3\u7801\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f16\u7801\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5c1d\u8bd5\u89e3\u7801\u5305\u542b\u65e0\u6548\u5b57\u8282\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\u4f1a\u5f15\u53d1<code>UnicodeDecodeError<\/code>\u3002\u53ef\u4ee5\u4f7f\u7528<code>errors<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u9519\u8bef\u5904\u7406\u7b56\u7565\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5ffd\u7565\u7f16\u7801\u9519\u8bef<\/p>\n<p>text = data.decode(&#39;utf-8&#39;, errors=&#39;ignore&#39;)<\/p>\n<h2><strong>\u4f7f\u7528\u66ff\u4ee3\u5b57\u7b26\u66ff\u6362\u65e0\u6548\u5b57\u8282<\/strong><\/h2>\n<p>text = data.decode(&#39;utf-8&#39;, errors=&#39;replace&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a\u9519\u8bef\u5904\u7406\u7b56\u7565\uff0c\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u56e0\u7f16\u7801\u9519\u8bef\u800c\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5e38\u89c1\u7f16\u7801\u9519\u8bef\u53ca\u89e3\u51b3\u65b9\u6848<\/h4>\n<\/p>\n<p><p>\u5e38\u89c1\u7684\u7f16\u7801\u9519\u8bef\u5305\u62ec<code>UnicodeDecodeError<\/code>\u548c<code>UnicodeEncodeError<\/code>\u3002\u89e3\u51b3\u8fd9\u4e9b\u9519\u8bef\u7684\u65b9\u6cd5\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u663e\u5f0f\u6307\u5b9a\u7f16\u7801\u683c\u5f0f<\/strong>\uff1a\u786e\u4fdd\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\u663e\u5f0f\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u907f\u514d\u4f7f\u7528\u9ed8\u8ba4\u7f16\u7801\u683c\u5f0f\u3002<\/li>\n<li><strong>\u68c0\u6d4b\u7f16\u7801\u683c\u5f0f<\/strong>\uff1a\u4f7f\u7528<code>chardet<\/code>\u5e93\u68c0\u6d4b\u6587\u672c\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u5e76\u6839\u636e\u68c0\u6d4b\u7ed3\u679c\u8fdb\u884c\u89e3\u7801\u3002<\/li>\n<li><strong>\u5904\u7406\u7f16\u7801\u9519\u8bef<\/strong>\uff1a\u4f7f\u7528<code>errors<\/code>\u53c2\u6570\u6307\u5b9a\u9519\u8bef\u5904\u7406\u7b56\u7565\uff0c\u907f\u514d\u7a0b\u5e8f\u56e0\u7f16\u7801\u9519\u8bef\u800c\u5d29\u6e83\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u7f16\u7801\u4e0e\u56fd\u9645\u5316<\/h3>\n<\/p>\n<p><h4>1\u3001Python\u7684\u56fd\u9645\u5316\u652f\u6301<\/h4>\n<\/p>\n<p><p>\u56fd\u9645\u5316\uff08i18n\uff09\u662f\u6307\u4f7f\u5e94\u7528\u7a0b\u5e8f\u80fd\u591f\u652f\u6301\u591a\u79cd\u8bed\u8a00\u548c\u5730\u533a\u8bbe\u7f6e\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>gettext<\/code>\u6a21\u5757\u8fdb\u884c\u56fd\u9645\u5316\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gettext<\/p>\n<h2><strong>\u8bbe\u7f6e\u7ffb\u8bd1\u6587\u4ef6\u76ee\u5f55\u548c\u8bed\u8a00<\/strong><\/h2>\n<p>gettext.bindtextdom<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n(&#39;myapp&#39;, &#39;locale&#39;)<\/p>\n<p>gettext.textdomain(&#39;myapp&#39;)<\/p>\n<p>_ = gettext.gettext<\/p>\n<h2><strong>\u4f7f\u7528\u7ffb\u8bd1\u51fd\u6570<\/strong><\/h2>\n<p>print(_(&quot;Hello, world!&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>gettext<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5e94\u7528\u7a0b\u5e8f\u7684\u591a\u8bed\u8a00\u652f\u6301\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5305\u542b\u4e2d\u6587\u548c\u65e5\u6587\u5b57\u7b26\u7684\u6587\u672c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\uff0c\u4ee5\u786e\u4fdd\u80fd\u591f\u6b63\u786e\u8868\u793a\u6240\u6709\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01\u3053\u3093\u306b\u3061\u306f\u3001\u4e16\u754c\uff01&quot;<\/p>\n<p>encoded_text = text.encode(&#39;utf-8&#39;)<\/p>\n<p>decoded_text = encoded_text.decode(&#39;utf-8&#39;)<\/p>\n<p>print(decoded_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u6b63\u786e\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7f16\u7801\u4e0e\u6570\u636e\u4ea4\u6362<\/h3>\n<\/p>\n<p><h4>1\u3001\u7f16\u7801\u5728\u6570\u636e\u4ea4\u6362\u4e2d\u7684\u91cd\u8981\u6027<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u4ea4\u6362\u8fc7\u7a0b\u4e2d\uff0c\u786e\u4fdd\u6570\u636e\u7684\u7f16\u7801\u683c\u5f0f\u4e00\u81f4\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u7f51\u7edc\u901a\u4fe1\u6216\u6587\u4ef6\u4f20\u8f93\u4e2d\uff0c\u4e0d\u540c\u7cfb\u7edf\u4e4b\u95f4\u7684\u6570\u636e\u7f16\u7801\u683c\u5f0f\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u4f7f\u7528\u4e00\u81f4\u7684\u7f16\u7801\u683c\u5f0f\u53ef\u4ee5\u907f\u514d\u6570\u636e\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u7684\u7f16\u7801\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u7f51\u7edc\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f51\u7edc\u6570\u636e\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u6570\u636e\u7f16\u7801\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f\u8fdb\u884c\u4f20\u8f93\u3002\u5728\u63a5\u6536\u6570\u636e\u65f6\uff0c\u9700\u8981\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u521b\u5efa\u5957\u63a5\u5b57<\/strong><\/h2>\n<p>sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>sock.connect((&#39;example.com&#39;, 80))<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>request = &#39;GET \/ HTTP\/1.1\\r\\nHost: example.com\\r\\n\\r\\n&#39;<\/p>\n<p>sock.send(request.encode(&#39;utf-8&#39;))<\/p>\n<h2><strong>\u63a5\u6536\u54cd\u5e94<\/strong><\/h2>\n<p>response = sock.recv(4096)<\/p>\n<p>print(response.decode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u65f6\u4f7f\u7528\u4e00\u81f4\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7f16\u7801\u4e0e\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u5e93\u7f16\u7801\u8bbe\u7f6e<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u6570\u636e\u5e93\u65f6\uff0c\u786e\u4fdd\u6570\u636e\u5e93\u7684\u7f16\u7801\u8bbe\u7f6e\u4e0e\u5e94\u7528\u7a0b\u5e8f\u7684\u7f16\u7801\u683c\u5f0f\u4e00\u81f4\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728MySQL\u6570\u636e\u5e93\u4e2d\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u5b57\u7b26\u96c6\u4e3aUTF-8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sql\">CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\uff0c\u53ef\u4ee5\u786e\u4fdd\u5b58\u50a8\u548c\u68c0\u7d22\u6570\u636e\u65f6\u4e0d\u4f1a\u51fa\u73b0\u7f16\u7801\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u6570\u636e\u5e93\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u5e93\u6570\u636e\u65f6\uff0c\u786e\u4fdd\u5728\u8fde\u63a5\u6570\u636e\u5e93\u65f6\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528MySQL\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u5728\u8fde\u63a5\u65f6\u6307\u5b9a\u5b57\u7b26\u96c6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = mysql.connector.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;user&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    database=&#39;mydb&#39;,<\/p>\n<p>    charset=&#39;utf8mb4&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<p>cursor.execute(&#39;SELECT * FROM mytable&#39;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a\u5b57\u7b26\u96c6\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u5904\u7406\u6570\u636e\u5e93\u6570\u636e\u65f6\u4e0d\u4f1a\u51fa\u73b0\u7f16\u7801\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u7f16\u7801\u4e0eWeb\u5f00\u53d1<\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406Web\u8868\u5355\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728Web\u5f00\u53d1\u4e2d\uff0c\u5904\u7406\u8868\u5355\u6570\u636e\u65f6\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u8868\u5355\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/submit&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def submit():<\/p>\n<p>    name = request.form[&#39;name&#39;]<\/p>\n<p>    message = request.form[&#39;message&#39;]<\/p>\n<p>    print(f&quot;Name: {name}, Message: {message}&quot;)<\/p>\n<p>    return &#39;Form submitted!&#39;<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u8868\u5355\u4e2d\u6307\u5b9a<code>enctype=&quot;application\/x-www-form-urlencoded&quot;<\/code>\u548c\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u8868\u5355\u6570\u636e\u7684\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406Web API\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406Web API\u6570\u636e\u65f6\uff0c\u786e\u4fdd\u8bf7\u6c42\u548c\u54cd\u5e94\u4f7f\u7528\u4e00\u81f4\u7684\u7f16\u7801\u683c\u5f0f\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u53d1\u9001\u548c\u63a5\u6536JSON\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;https:\/\/api.example.com\/data&#39;<\/p>\n<p>headers = {&#39;Content-Type&#39;: &#39;application\/json; charset=utf-8&#39;}<\/p>\n<p>data = {&#39;name&#39;: &#39;John&#39;, &#39;message&#39;: &#39;Hello, world!&#39;}<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.post(url, json=data, headers=headers)<\/p>\n<p>print(response.json())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u8bf7\u6c42\u5934\u4e2d\u6307\u5b9a<code>Content-Type<\/code>\u548c\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fddAPI\u6570\u636e\u7684\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u7f16\u7801\u4e0e\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u65e5\u5fd7\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u8bb0\u5f55\u65e5\u5fd7\u65f6\uff0c\u786e\u4fdd\u65e5\u5fd7\u6587\u4ef6\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u8bb0\u5f55\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u65e5\u5fd7\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(filename=&#39;app.log&#39;, level=logging.INFO, encoding=&#39;utf-8&#39;)<\/p>\n<p>logging.info(&#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u8bb0\u5f55\u65e5\u5fd7\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u65e5\u5fd7\u6587\u4ef6\u7684\u6b63\u786e\u8bb0\u5f55\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u65e5\u5fd7\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u5728\u8f93\u51fa\u65e5\u5fd7\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u8f93\u51fa\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u65e5\u5fd7\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger(&#39;my_logger&#39;)<\/p>\n<p>logger.setLevel(logging.INFO)<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u5fd7\u5904\u7406\u5668<\/strong><\/h2>\n<p>handler = logging.StreamHandler()<\/p>\n<p>handler.setLevel(logging.INFO)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7f16\u7801\u683c\u5f0f<\/strong><\/h2>\n<p>formatter = logging.Formatter(&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>handler.setFormatter(formatter)<\/p>\n<p>logger.addHandler(handler)<\/p>\n<h2><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/h2>\n<p>logger.info(&#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u65e5\u5fd7\u5904\u7406\u5668\u4e2d\u8bbe\u7f6e\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u65e5\u5fd7\u8f93\u51fa\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u7f16\u7801\u4e0e\u6570\u636e\u5206\u6790<\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5904\u7406CSV\u6587\u4ef6\u65f6\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u8bfb\u53d6\u5305\u542b\u975eASCII\u5b57\u7b26\u7684CSV\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;data.csv&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u8bfb\u53d6CSV\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u6b63\u786e\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406Excel\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u8bfb\u53d6\u5305\u542b\u975eASCII\u5b57\u7b26\u7684Excel\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>openpyxl<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import openpyxl<\/p>\n<h2><strong>\u52a0\u8f7dExcel\u6587\u4ef6<\/strong><\/h2>\n<p>wb = openpyxl.load_workbook(&#39;data.xlsx&#39;)<\/p>\n<h2><strong>\u9009\u62e9\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws = wb.active<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>for row in ws.iter_rows(values_only=True):<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>openpyxl<\/code>\u5e93\uff0c\u53ef\u4ee5\u786e\u4fddExcel\u6587\u4ef6\u7684\u6b63\u786e\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u7f16\u7801\u4e0e<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u6587\u672c\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u6587\u672c\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;, encoding=&#39;utf-8&#39;)<\/p>\n<h2><strong>\u5904\u7406\u6587\u672c\u6570\u636e<\/strong><\/h2>\n<p>texts = df[&#39;text&#39;].tolist()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u8bfb\u53d6CSV\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u6587\u672c\u6570\u636e\u7684\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u7279\u5f81\u5de5\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728\u7279\u5f81\u5de5\u7a0b\u4e2d\uff0c\u5904\u7406\u6587\u672c\u7279\u5f81\u65f6\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u6587\u672c\u7279\u5f81\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>TfidfVectorizer<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.feature_extraction.text import TfidfVectorizer<\/p>\n<p>texts = [&#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;, &#39;\u3053\u3093\u306b\u3061\u306f\u3001\u4e16\u754c\uff01&#39;]<\/p>\n<p>vectorizer = TfidfVectorizer()<\/p>\n<h2><strong>\u5904\u7406\u6587\u672c\u7279\u5f81<\/strong><\/h2>\n<p>X = vectorizer.fit_transform(texts)<\/p>\n<p>print(X.toarray())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>TfidfVectorizer<\/code>\uff0c\u53ef\u4ee5\u786e\u4fdd\u6587\u672c\u7279\u5f81\u7684\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u7f16\u7801\u4e0e\u6570\u636e\u5b58\u50a8<\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406JSON\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5b58\u50a8\u4e2d\uff0c\u5904\u7406JSON\u6570\u636e\u65f6\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u5b58\u50a8\u5305\u542b\u975eASCII\u5b57\u7b26\u7684JSON\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&#39;name&#39;: &#39;John&#39;, &#39;message&#39;: &#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;}<\/p>\n<h2><strong>\u5199\u5165JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.json&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    json.dump(data, file, ensure_ascii=False)<\/p>\n<h2><strong>\u8bfb\u53d6JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.json&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>    print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u5199\u5165\u548c\u8bfb\u53d6JSON\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fddJSON\u6570\u636e\u7684\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406XML\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406XML\u6570\u636e\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5305\u542b\u975eASCII\u5b57\u7b26\u7684XML\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ElementTree<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<h2><strong>\u521b\u5efaXML\u6570\u636e<\/strong><\/h2>\n<p>root = ET.Element(&#39;root&#39;)<\/p>\n<p>child = ET.SubElement(root, &#39;child&#39;)<\/p>\n<p>child.text = &#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;<\/p>\n<h2><strong>\u5199\u5165XML\u6587\u4ef6<\/strong><\/h2>\n<p>tree = ET.ElementTree(root)<\/p>\n<p>tree.write(&#39;data.xml&#39;, encoding=&#39;utf-8&#39;, xml_declaration=True)<\/p>\n<h2><strong>\u8bfb\u53d6XML\u6587\u4ef6<\/strong><\/h2>\n<p>tree = ET.parse(&#39;data.xml&#39;)<\/p>\n<p>root = tree.getroot()<\/p>\n<p>print(root[0].text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u5199\u5165\u548c\u8bfb\u53d6XML\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fddXML\u6570\u636e\u7684\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u7f16\u7801\u4e0e\u56fe\u50cf\u5904\u7406<\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u56fe\u50cf\u5143\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u5904\u7406\u56fe\u50cf\u5143\u6570\u636e\u65f6\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u56fe\u50cf\u5143\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>PIL<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>from PIL.ExifTags import TAGS<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&#39;image.jpg&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u5143\u6570\u636e<\/strong><\/h2>\n<p>exif_data = image._getexif()<\/p>\n<h2><strong>\u5904\u7406\u5143\u6570\u636e<\/strong><\/h2>\n<p>for tag, value in exif_data.items():<\/p>\n<p>    tag_name = TAGS.get(tag, tag)<\/p>\n<p>    print(f&quot;{tag_name}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>PIL<\/code>\u5e93\uff0c\u53ef\u4ee5\u786e\u4fdd\u56fe\u50cf\u5143\u6570\u636e\u7684\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u56fe\u50cf\u6587\u4ef6\u540d<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u56fe\u50cf\u6587\u4ef6\u540d\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u56fe\u50cf\u6587\u4ef6\u540d\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u5904\u7406\u56fe\u50cf\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>file_name = &#39;\u4f60\u597d.jpg&#39;<\/p>\n<p>new_file_name = file_name.encode(&#39;utf-8&#39;).decode(&#39;utf-8&#39;)<\/p>\n<p>os.rename(file_name, new_file_name)<\/p>\n<h2><strong>\u6253\u5370\u65b0\u6587\u4ef6\u540d<\/strong><\/h2>\n<p>print(new_file_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528UTF-8\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u56fe\u50cf\u6587\u4ef6\u540d\u7684\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u7f16\u7801\u4e0e\u97f3\u9891\u5904\u7406<\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u97f3\u9891\u5143\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u97f3\u9891\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u5904\u7406\u97f3\u9891\u5143\u6570\u636e\u65f6\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u97f3\u9891\u5143\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>mutagen<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mutagen.easyid3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u7f16\u7801\u683c\u5f0f\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u7f16\u7801\u683c\u5f0f\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fUTF-8\u3001ASCII\u548cISO-8859-1\u3002UTF-8\u662f\u73b0\u4ee3\u5e94\u7528\u7a0b\u5e8f\u548c\u7f51\u7ad9\u7684\u6807\u51c6\u7f16\u7801\u65b9\u5f0f\uff0c\u80fd\u591f\u5904\u7406\u591a\u79cd\u8bed\u8a00\u5b57\u7b26\uff0c\u800cASCII\u5219\u4e3b\u8981\u7528\u4e8e\u82f1\u8bed\u5b57\u7b26\u3002ISO-8859-1\u5e38\u7528\u4e8e\u897f\u6b27\u8bed\u8a00\u3002\u5728\u5904\u7406\u6587\u672c\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u683c\u5f0f\u53ef\u4ee5\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u548c\u5199\u5165\u7279\u5b9a\u7f16\u7801\u683c\u5f0f\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u6765\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>open(&#39;file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;)<\/code>\u53ef\u4ee5\u4ee5UTF-8\u683c\u5f0f\u8bfb\u53d6\u6587\u4ef6\u3002\u5199\u5165\u65f6\u540c\u6837\u53ef\u4ee5\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u5982<code>open(&#39;file.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;)<\/code>\u3002\u786e\u4fdd\u5728\u5904\u7406\u6587\u4ef6\u65f6\u5339\u914d\u6b63\u786e\u7684\u7f16\u7801\uff0c\u4ee5\u907f\u514d\u6570\u636e\u4e22\u5931\u6216\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e2d\u7684\u7f16\u7801\u9519\u8bef\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5f53\u9047\u5230\u7f16\u7801\u9519\u8bef\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>errors<\/code>\u53c2\u6570\u6765\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\u3002\u5e38\u7528\u7684\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\u5b57\u7b26\uff09\u548c<code>backslashreplace<\/code>\uff08\u7528Python\u7684\u8f6c\u4e49\u5b57\u7b26\u66ff\u6362\uff09\u3002\u4f8b\u5982\uff0c<code>open(&#39;file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;, errors=&#39;ignore&#39;)<\/code>\u5c06\u4f1a\u5728\u9047\u5230\u7f16\u7801\u9519\u8bef\u65f6\u81ea\u52a8\u5ffd\u7565\u90a3\u4e9b\u9519\u8bef\u7684\u5b57\u7b26\u3002\u9009\u62e9\u5408\u9002\u7684\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7f16\u7801\u683c\u5f0f\u7684\u5904\u7406\u4e3b\u8981\u6d89\u53ca\u5b57\u7b26\u7f16\u7801\u548c\u89e3\u7801\u3002\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u5904\u7406\u6587\u672c\u3001\u4f7f\u7528encode()\u548cde [&hellip;]","protected":false},"author":3,"featured_media":1171818,"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\/1171807"}],"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=1171807"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171807\/revisions"}],"predecessor-version":[{"id":1171819,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171807\/revisions\/1171819"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1171818"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1171807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1171807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1171807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}