{"id":964271,"date":"2024-12-27T04:26:41","date_gmt":"2024-12-26T20:26:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/964271.html"},"modified":"2024-12-27T04:26:43","modified_gmt":"2024-12-26T20:26:43","slug":"python%e5%a6%82%e4%bd%95%e5%bf%ab%e9%80%9f%e6%89%93encoding","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/964271.html","title":{"rendered":"python\u5982\u4f55\u5feb\u901f\u6253encoding"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181132\/4a77e329-24e8-4269-93d4-baa68cbaf69f.webp\" alt=\"python\u5982\u4f55\u5feb\u901f\u6253encoding\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5feb\u901f\u5904\u7406\u7f16\u7801\u7684\u5173\u952e\u5728\u4e8e\uff1a\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3002\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u65f6\uff0c\u8981\u6839\u636e\u4f60\u7684\u6570\u636e\u6765\u6e90\u548c\u76ee\u6807\u6765\u51b3\u5b9a\uff0c\u6bd4\u5982UTF-8\u3001ISO-8859-1\u7b49\u90fd\u662f\u5e38\u7528\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u5982<code>str.encode()<\/code>\u548c<code>bytes.decode()<\/code>\u53ef\u4ee5\u5feb\u901f\u5b9e\u73b0\u5b57\u7b26\u4e32\u4e0e\u5b57\u8282\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u5904\u7406\u5f02\u5e38\u65f6\uff0c\u4f7f\u7528\u9519\u8bef\u5904\u7406\u53c2\u6570\u5982<code>errors=&#39;ignore&#39;<\/code>\u6216<code>errors=&#39;replace&#39;<\/code>\u6765\u907f\u514d\u56e0\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u7684\u7a0b\u5e8f\u5d29\u6e83\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u8981\u70b9\u4e4b\u524d\uff0c\u9996\u5148\u8981\u4e86\u89e3\u7f16\u7801\u5728\u7f16\u7a0b\u548c\u6570\u636e\u5904\u7406\u4e2d\u626e\u6f14\u7684\u89d2\u8272\u3002\u7f16\u7801\u662f\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5b57\u8282\u7684\u8fc7\u7a0b\uff0c\u800c\u89e3\u7801\u5219\u662f\u5c06\u5b57\u8282\u8f6c\u6362\u4e3a\u5b57\u7b26\u7684\u8fc7\u7a0b\u3002\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u5728\u6570\u636e\u4f20\u8f93\u3001\u5b58\u50a8\u548c\u5904\u7406\u65f6\u81f3\u5173\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u591a\u8bed\u8a00\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u683c\u5f0f\u662f\u5904\u7406\u7f16\u7801\u95ee\u9898\u7684\u7b2c\u4e00\u6b65\u3002\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u5f0f\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7f16\u7801\u683c\u5f0f\u53ca\u5176\u9002\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>UTF-8<\/strong><br \/>UTF-8\u662f\u76ee\u524d\u6700\u5e7f\u6cdb\u4f7f\u7528\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u652f\u6301\u6240\u6709\u7684Unicode\u5b57\u7b26\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u56fd\u9645\u5316\u5e94\u7528\u3002\u5b83\u7684\u4f18\u52bf\u5728\u4e8e\u5bf9\u82f1\u6587\u5b57\u7b26\u4f7f\u7528\u4e00\u4e2a\u5b57\u8282\uff0c\u5bf9\u5176\u4ed6\u5b57\u7b26\u4f7f\u7528\u591a\u4e2a\u5b57\u8282\uff0c\u56e0\u6b64\u6bd4\u8f83\u8282\u7701\u7a7a\u95f4\uff0c\u7279\u522b\u662f\u5bf9\u4ee5\u82f1\u6587\u4e3a\u4e3b\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>ISO-8859-1<\/strong><br \/>ISO-8859-1\u53c8\u79f0\u4e3aLatin-1\uff0c\u662f\u4e00\u79cd\u5355\u5b57\u8282\u7f16\u7801\uff0c\u4e3b\u8981\u7528\u4e8e\u897f\u6b27\u8bed\u8a00\u3002\u5b83\u4e0d\u652f\u6301\u4e2d\u6587\u7b49\u4e9a\u6d32\u5b57\u7b26\uff0c\u56e0\u6b64\u5728\u9700\u8981\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c\u65f6\u53ef\u80fd\u4e0d\u9002\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>ASCII<\/strong><br \/>ASCII\u662f\u6700\u57fa\u7840\u7684\u7f16\u7801\uff0c\u53ea\u652f\u6301\u82f1\u6587\u5b57\u7b26\u548c\u4e00\u4e9b\u63a7\u5236\u5b57\u7b26\u3002\u5b83\u662fUTF-8\u7684\u5b50\u96c6\uff0c\u56e0\u6b64\u5728\u5904\u7406\u82f1\u6587\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528ASCII\u4ee5\u8282\u7701\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u4e0d\u4ec5\u80fd\u786e\u4fdd\u6570\u636e\u7684\u6b63\u786e\u89e3\u7801\uff0c\u4e5f\u80fd\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u7f16\u7801\u8f6c\u6362<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\u6765\u5904\u7406\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u5904\u7406\u7f16\u7801\u95ee\u9898\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>str.encode()<\/strong><br \/><code>str.encode()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6307\u5b9a\u7f16\u7801\u7684\u5b57\u8282\u5bf9\u8c61\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b83\u4f7f\u7528UTF-8\u7f16\u7801\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a<code>encoding<\/code>\u53c2\u6570\u6765\u66f4\u6539\u7f16\u7801\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, \u4e16\u754c&quot;<\/p>\n<p>encoded_text = text.encode(&#39;utf-8&#39;)<\/p>\n<p>print(encoded_text)  # \u8f93\u51fa: b&#39;Hello, \\xe4\\xb8\\x96\\xe7\\x95\\x8c&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>bytes.decode()<\/strong><br \/><code>bytes.decode()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4e0e<code>str.encode()<\/code>\u76f8\u5bf9\u5e94\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6307\u5b9a<code>encoding<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">encoded_text = b&#39;Hello, \\xe4\\xb8\\x96\\xe7\\x95\\x8c&#39;<\/p>\n<p>text = encoded_text.decode(&#39;utf-8&#39;)<\/p>\n<p>print(text)  # \u8f93\u51fa: Hello, \u4e16\u754c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u4e0d\u540c\u7f16\u7801\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u4ece\u800c\u6ee1\u8db3\u4e0d\u540c\u5e94\u7528\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f16\u7801\u95ee\u9898\u65f6\uff0c\u5f02\u5e38\u60c5\u51b5\u4e0d\u53ef\u907f\u514d\u3002\u4e3a\u4e86\u907f\u514d\u56e0\u7f16\u7801\u9519\u8bef\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u9519\u8bef\u5904\u7406\u7b56\u7565\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>errors=&#39;ignore&#39;<\/strong><br \/>\u4f7f\u7528<code>errors=&#39;ignore&#39;<\/code>\u53c2\u6570\u53ef\u4ee5\u5728\u9047\u5230\u65e0\u6cd5\u89e3\u7801\u7684\u5b57\u8282\u65f6\u5ffd\u7565\u9519\u8bef\uff0c\u5e76\u7ee7\u7eed\u5904\u7406\u5269\u4f59\u7684\u5b57\u8282\u3002\u867d\u7136\u8fd9\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff0c\u4f46\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">encoded_text = b&#39;Hello, \\xe4\\xb8\\x96\\xe7\\x95\\x8c\\xff&#39;<\/p>\n<p>text = encoded_text.decode(&#39;utf-8&#39;, errors=&#39;ignore&#39;)<\/p>\n<p>print(text)  # \u8f93\u51fa: Hello, \u4e16\u754c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>errors=&#39;replace&#39;<\/strong><br \/>\u4f7f\u7528<code>errors=&#39;replace&#39;<\/code>\u53c2\u6570\u53ef\u4ee5\u5728\u9047\u5230\u65e0\u6cd5\u89e3\u7801\u7684\u5b57\u8282\u65f6\u7528\u66ff\u4ee3\u5b57\u7b26\uff08\u901a\u5e38\u662f<code>?<\/code>\uff09\u66ff\u6362\u9519\u8bef\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = encoded_text.decode(&#39;utf-8&#39;, errors=&#39;replace&#39;)<\/p>\n<p>print(text)  # \u8f93\u51fa: Hello, \u4e16\u754c\ufffd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>errors=&#39;strict&#39;<\/strong><br \/><code>errors=&#39;strict&#39;<\/code>\u662f\u9ed8\u8ba4\u7684\u9519\u8bef\u5904\u7406\u7b56\u7565\uff0c\u5728\u9047\u5230\u9519\u8bef\u65f6\u4f1a\u629b\u51fa<code>UnicodeDecodeError<\/code>\u5f02\u5e38\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u5728\u7f16\u7801\u9519\u8bef\u53d1\u751f\u65f6\u7acb\u5373\u5f97\u5230\u901a\u77e5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u9519\u8bef\u5904\u7406\u7b56\u7565\uff0c\u4f60\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u786e\u4fdd\u5728\u5904\u7406\u7f16\u7801\u95ee\u9898\u65f6\u4e0d\u4f1a\u56e0\u4e3a\u610f\u5916\u60c5\u51b5\u800c\u4e2d\u65ad\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7f16\u7801\u5728\u6570\u636e\u5904\u7406\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u4e2d\uff0c\u7f16\u7801\u95ee\u9898\u65e0\u5904\u4e0d\u5728\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u4ece\u4e00\u79cd\u683c\u5f0f\u8f6c\u6362\u5230\u53e6\u4e00\u79cd\u683c\u5f0f\u65f6\u3002\u5728\u4ee5\u4e0b\u573a\u666f\u4e2d\uff0c\u7f16\u7801\u5904\u7406\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bfb\u53d6\u6587\u4ef6<\/strong><br \/>\u5f53\u4f60\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u5fc5\u987b\u77e5\u9053\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4ee5\u4fbf\u6b63\u786e\u5730\u89e3\u7801\u5185\u5bb9\u3002\u5728Python\u4e2d\uff0c<code>open()<\/code>\u51fd\u6570\u7684<code>encoding<\/code>\u53c2\u6570\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;data.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<\/li>\n<li>\n<p><strong>\u5199\u5165\u6587\u4ef6<\/strong><br \/>\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u540c\u6837\u9700\u8981\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u4ee5\u786e\u4fdd\u6587\u4ef6\u53ef\u4ee5\u88ab\u5176\u4ed6\u7a0b\u5e8f\u6b63\u786e\u8bfb\u53d6\u3002<\/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<\/li>\n<li>\n<p><strong>\u7f51\u7edc\u4f20\u8f93<\/strong><br \/>\u7f51\u7edc\u4f20\u8f93\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u4ee5\u5b57\u8282\u5f62\u5f0f\u53d1\u9001\uff0c\u56e0\u6b64\u5728\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u65f6\uff0c\u9700\u8981\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2asocket\u5bf9\u8c61<\/strong><\/h2>\n<p>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>s.connect((&#39;localhost&#39;, 12345))<\/p>\n<h2><strong>\u53d1\u9001\u6570\u636e<\/strong><\/h2>\n<p>message = &#39;Hello, world!&#39;<\/p>\n<p>s.sendall(message.encode(&#39;utf-8&#39;))<\/p>\n<h2><strong>\u63a5\u6536\u6570\u636e<\/strong><\/h2>\n<p>data = s.recv(1024)<\/p>\n<p>print(&#39;Received&#39;, data.decode(&#39;utf-8&#39;))<\/p>\n<p>s.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5bf9\u7f16\u7801\u7684\u6df1\u5165\u7406\u89e3\uff0c\u4f60\u53ef\u4ee5\u5728\u6570\u636e\u5904\u7406\u7684\u5404\u4e2a\u73af\u8282\u66f4\u597d\u5730\u7ba1\u7406\u548c\u8f6c\u6362\u6570\u636e\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7f16\u7801\u4e0e\u591a\u8bed\u8a00\u652f\u6301<\/p>\n<\/p>\n<p><p>\u5728\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u591a\u8bed\u8a00\u652f\u6301\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u9700\u6c42\u3002\u7f16\u7801\u5728\u5b9e\u73b0\u591a\u8bed\u8a00\u652f\u6301\u4e2d\u626e\u6f14\u4e86\u5173\u952e\u89d2\u8272\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u56fd\u9645\u5316\u4e0e\u672c\u5730\u5316<\/strong><br \/>\u56fd\u9645\u5316\uff08i18n\uff09\u662f\u6307\u8bbe\u8ba1\u8f6f\u4ef6\u65f6\u8003\u8651\u5230\u591a\u8bed\u8a00\u652f\u6301\uff0c\u800c\u672c\u5730\u5316\uff08l10n\uff09\u5219\u662f\u6307\u5c06\u8f6f\u4ef6\u9002\u914d\u5230\u7279\u5b9a\u8bed\u8a00\u548c\u6587\u5316\u3002\u5728\u5b9e\u73b0\u56fd\u9645\u5316\u548c\u672c\u5730\u5316\u65f6\uff0c\u4f7f\u7528\u9002\u5f53\u7684\u7f16\u7801\uff08\u901a\u5e38\u662fUTF-8\uff09\u53ef\u4ee5\u786e\u4fdd\u8f6f\u4ef6\u80fd\u591f\u5904\u7406\u591a\u79cd\u8bed\u8a00\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Unicode<\/strong><br \/>Unicode\u662f\u4e00\u4e2a\u5b57\u7b26\u96c6\uff0c\u5b83\u4e3a\u4e16\u754c\u4e0a\u51e0\u4e4e\u6240\u6709\u7684\u5b57\u7b26\u5206\u914d\u4e86\u552f\u4e00\u7684\u7f16\u7801\u3002\u901a\u8fc7\u4f7f\u7528Unicode\u53ca\u5176\u76f8\u5173\u7f16\u7801\uff08\u5982UTF-8\u3001UTF-16\uff09\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bed\u8a00\u68c0\u6d4b\u4e0e\u7ffb\u8bd1<\/strong><br \/>\u5728\u9700\u8981\u652f\u6301\u591a\u8bed\u8a00\u6587\u672c\u5904\u7406\u65f6\uff0c\u81ea\u52a8\u68c0\u6d4b\u6587\u672c\u8bed\u8a00\u548c\u63d0\u4f9b\u7ffb\u8bd1\u529f\u80fd\u662f\u5e38\u89c1\u9700\u6c42\u3002Python\u7684<code>langdetect<\/code>\u5e93\u53ef\u4ee5\u5e2e\u52a9\u68c0\u6d4b\u6587\u672c\u8bed\u8a00\uff0c\u800c<code>googletrans<\/code>\u5e93\u5219\u53ef\u4ee5\u7528\u4e8e\u6587\u672c\u7ffb\u8bd1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from langdetect import detect<\/p>\n<p>from googletrans import Translator<\/p>\n<p>text = &quot;Bonjour tout le monde&quot;<\/p>\n<p>language = detect(text)<\/p>\n<p>print(f&quot;Detected language: {language}&quot;)<\/p>\n<p>translator = Translator()<\/p>\n<p>translation = translator.translate(text, dest=&#39;en&#39;)<\/p>\n<p>print(f&quot;Translation: {translation.text}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5229\u7528Python\u7684\u4e30\u5bcc\u5e93\u548c\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u591a\u8bed\u8a00\u652f\u6301\uff0c\u4ece\u800c\u6269\u5c55\u8f6f\u4ef6\u7684\u7528\u6237\u7fa4\u4f53\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u7f16\u7801\u7684\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u7f16\u7801\u64cd\u4f5c\u53ef\u80fd\u6210\u4e3a\u6027\u80fd\u74f6\u9888\u3002\u56e0\u6b64\uff0c\u5728\u8bbe\u8ba1\u548c\u4f18\u5316\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6279\u91cf\u5904\u7406<\/strong><br \/>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528\u6279\u91cf\u64cd\u4f5c\u4ee5\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\u3002\u4f8b\u5982\uff0c\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u5e76\u5904\u7406\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;large_file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        process(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f13\u5b58\u7ed3\u679c<\/strong><br \/>\u5bf9\u4e8e\u91cd\u590d\u7684\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8003\u8651\u7f13\u5b58\u7ed3\u679c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002Python\u7684<code>functools.lru_cache<\/code>\u88c5\u9970\u5668\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import lru_cache<\/p>\n<p>@lru_cache(maxsize=1024)<\/p>\n<p>def encode_text(text, encoding=&#39;utf-8&#39;):<\/p>\n<p>    return text.encode(encoding)<\/p>\n<h2><strong>\u4f7f\u7528\u7f13\u5b58<\/strong><\/h2>\n<p>encoded_text = encode_text(&quot;Hello, world!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/strong><br \/>\u5728\u5904\u7406\u5927\u91cf\u6587\u672c\u6570\u636e\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>bytearray<\/code>\u800c\u4e0d\u662f<code>bytes<\/code>\u8fdb\u884c\u53ef\u53d8\u5b57\u8282\u5e8f\u5217\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = bytearray(b&#39;Hello, world!&#39;)<\/p>\n<p>data.extend(b&#39; More data&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u7684\u6027\u80fd\u4f18\u5316\u63aa\u65bd\uff0c\u4f60\u53ef\u4ee5\u5728\u4e0d\u727a\u7272\u7a0b\u5e8f\u529f\u80fd\u7684\u60c5\u51b5\u4e0b\u663e\u8457\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u901f\u5ea6\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u7f16\u7801\u5728\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/tag\/AI\" target=\"_blank\">\u4eba\u5de5\u667a\u80fd<\/a>\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u548c\u4eba\u5de5\u667a\u80fd\u9886\u57df\uff0c\u7f16\u7801\u95ee\u9898\u540c\u6837\u91cd\u8981\uff0c\u56e0\u4e3a\u6570\u636e\u901a\u5e38\u6765\u81ea\u591a\u79cd\u4e0d\u540c\u7684\u6765\u6e90\uff0c\u5e76\u53ef\u80fd\u4ee5\u591a\u79cd\u4e0d\u540c\u7684\u683c\u5f0f\u5b58\u5728\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u6e05\u6d17<\/strong><br \/>\u5728\u8fdb\u884c\u6570\u636e\u5206\u6790\u4e4b\u524d\uff0c\u5fc5\u987b\u786e\u4fdd\u6240\u6709\u6570\u636e\u4ee5\u4e00\u81f4\u7684\u7f16\u7801\u683c\u5f0f\u5b58\u5728\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684<code>pandas<\/code>\u5e93\u6765\u5b9e\u73b0\uff0c\u5176<code>read_csv()<\/code>\u548c<code>to_csv()<\/code>\u51fd\u6570\u90fd\u652f\u6301\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_csv(&#39;data.csv&#39;, encoding=&#39;utf-8&#39;)<\/p>\n<p>df.to_csv(&#39;cleaned_data.csv&#39;, encoding=&#39;utf-8&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09<\/strong><br \/>\u5728NLP\u4efb\u52a1\u4e2d\uff0c\u6587\u672c\u6570\u636e\u7684\u9884\u5904\u7406\u662f\u5173\u952e\u6b65\u9aa4\uff0c\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u5728\u6b64\u8fc7\u7a0b\u4e2d\u5c24\u4e3a\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528\u8bcd\u5411\u91cf\u6a21\u578b\uff08\u5982Word2Vec\uff09\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u6240\u6709\u6587\u672c\u4ee5\u76f8\u540c\u7684\u7f16\u7801\u683c\u5f0f\u5b58\u5728\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/strong><br \/>\u5728\u5f00\u53d1\u8de8\u5e73\u53f0\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u5fc5\u987b\u8003\u8651\u5230\u4e0d\u540c\u5e73\u53f0\u53ef\u80fd\u4f7f\u7528\u4e0d\u540c\u7684\u9ed8\u8ba4\u7f16\u7801\u683c\u5f0f\u3002\u56e0\u6b64\uff0c\u786e\u4fdd\u6570\u636e\u5728\u4f20\u8f93\u548c\u5904\u7406\u8fc7\u7a0b\u4e2d\u59cb\u7ec8\u4f7f\u7528\u7edf\u4e00\u7684\u7f16\u7801\u683c\u5f0f\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u548c\u4eba\u5de5\u667a\u80fd\u4e2d\uff0c\u6b63\u786e\u5904\u7406\u7f16\u7801\u95ee\u9898\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u51c6\u786e\u6027\uff0c\u8fd8\u80fd\u4e3a\u6a21\u578b\u8bad\u7ec3\u548c\u9884\u6d4b\u63d0\u4f9b\u66f4\u53ef\u9760\u7684\u6570\u636e\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u4ee5\u4e0a\u5185\u5bb9\u7684\u6df1\u5165\u7406\u89e3\u548c\u5e94\u7528\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u66f4\u9ad8\u6548\u5730\u5904\u7406\u7f16\u7801\u95ee\u9898\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u6027\u80fd\u3002\u7f16\u7801\u95ee\u9898\u867d\u7136\u590d\u6742\uff0c\u4f46\u901a\u8fc7\u5408\u7406\u7684\u7b56\u7565\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u663e\u8457\u7b80\u5316\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8bbe\u7f6e\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a<code>encoding<\/code>\u53c2\u6570\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>open(&#39;filename.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;)<\/code>\u3002\u8fd9\u5c06\u4ee5UTF-8\u7f16\u7801\u683c\u5f0f\u8bfb\u53d6\u6587\u4ef6\uff0c\u786e\u4fdd\u6b63\u786e\u89e3\u6790\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u89c1\u7684Python\u7f16\u7801\u683c\u5f0f\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u7f16\u7801\u683c\u5f0f\uff0c\u6700\u5e38\u89c1\u7684\u5305\u62ecUTF-8\u3001ISO-8859-1\u3001ASCII\u7b49\u3002UTF-8\u662f\u73b0\u4ee3\u5e94\u7528\u4e2d\u6700\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u80fd\u591f\u5904\u7406\u51e0\u4e4e\u6240\u6709\u8bed\u8a00\u7684\u5b57\u7b26\u3002\u800cASCII\u5219\u4ec5\u652f\u6301\u57fa\u672c\u7684\u62c9\u4e01\u5b57\u7b26\uff0c\u9002\u5408\u4e8e\u7b80\u5355\u7684\u6587\u672c\u6587\u4ef6\u3002\u5728\u9009\u62e9\u7f16\u7801\u683c\u5f0f\u65f6\uff0c\u5e94\u8003\u8651\u6587\u4ef6\u5185\u5bb9\u7684\u8bed\u8a00\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e2d\u7684\u7f16\u7801\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6587\u4ef6\u6216\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f16\u7801\u9519\u8bef\u3002\u53ef\u4ee5\u901a\u8fc7<code>errors<\/code>\u53c2\u6570\u6765\u63a7\u5236\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>open(&#39;filename.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;, errors=&#39;ignore&#39;)<\/code>\u53ef\u4ee5\u5ffd\u7565\u7f16\u7801\u9519\u8bef\uff0c\u800c\u4f7f\u7528<code>errors=&#39;replace&#39;<\/code>\u5219\u4f1a\u7528\u66ff\u4ee3\u5b57\u7b26\u6765\u66ff\u6362\u65e0\u6548\u7684\u7f16\u7801\u3002\u8fd9\u6709\u52a9\u4e8e\u63d0\u9ad8\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\uff0c\u786e\u4fdd\u5728\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u65f6\u4e0d\u4f1a\u56e0\u4e3a\u7f16\u7801\u95ee\u9898\u800c\u5d29\u6e83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5feb\u901f\u5904\u7406\u7f16\u7801\u7684\u5173\u952e\u5728\u4e8e\uff1a\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3002\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u65f6\uff0c\u8981 [&hellip;]","protected":false},"author":3,"featured_media":964274,"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\/964271"}],"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=964271"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964271\/revisions"}],"predecessor-version":[{"id":964276,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964271\/revisions\/964276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/964274"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=964271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=964271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=964271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}