{"id":1152257,"date":"2025-01-13T17:23:32","date_gmt":"2025-01-13T09:23:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1152257.html"},"modified":"2025-01-13T17:23:35","modified_gmt":"2025-01-13T09:23:35","slug":"%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%aepython%e7%bc%96%e7%a0%81%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1152257.html","title":{"rendered":"\u5982\u4f55\u8bbe\u7f6epython\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\/25182256\/4720e6ea-550b-4098-92bf-63c7ac0d3c92.webp\" alt=\"\u5982\u4f55\u8bbe\u7f6epython\u7f16\u7801\u683c\u5f0f\" \/><\/p>\n<p><p> <strong>\u8981\u8bbe\u7f6ePython\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f16\u7801\u58f0\u660e\u3001\u9009\u62e9\u5408\u9002\u7684\u6587\u672c\u7f16\u8f91\u5668\u3001\u914d\u7f6e\u7f16\u8f91\u5668\u7f16\u7801\u683c\u5f0f\u3001\u5408\u7406\u4f7f\u7528\u5b57\u7b26\u4e32\u7f16\u7801\u3001\u5904\u7406\u6587\u4ef6\u8bfb\u5199\u65f6\u6307\u5b9a\u7f16\u7801<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u91cd\u8981\u7684\u4e00\u70b9\u662f<strong>\u5728Python\u6587\u4ef6\u7684\u5f00\u5934\u4f7f\u7528\u7f16\u7801\u58f0\u660e<\/strong>\u3002\u5728Python\u6587\u4ef6\u7684\u5f00\u5934\u6dfb\u52a0\u4e00\u884c\u7f16\u7801\u58f0\u660e\uff0c\u53ef\u4ee5\u786e\u4fddPython\u89e3\u91ca\u5668\u6309\u7167\u6307\u5b9a\u7684\u7f16\u7801\u65b9\u5f0f\u89e3\u6790\u6587\u4ef6\u5185\u5bb9\u3002\u8fd9\u5728\u5904\u7406\u975eASCII\u5b57\u7b26\u65f6\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u544a\u8bc9Python\u89e3\u91ca\u5668\uff0c\u8fd9\u4e2a\u6587\u4ef6\u4f7f\u7528\u7684\u662fUTF-8\u7f16\u7801\u3002\u8fd9\u6837\uff0c\u5373\u4f7f\u6587\u4ef6\u4e2d\u5305\u542b\u975eASCII\u5b57\u7b26\uff0c\u4e5f\u80fd\u6b63\u786e\u89e3\u6790\u548c\u5904\u7406\u3002\u8fd9\u79cd\u65b9\u5f0f\u7279\u522b\u9002\u7528\u4e8e\u591a\u8bed\u8a00\u9879\u76ee\u6216\u9700\u8981\u5904\u7406\u975e\u82f1\u6587\u5b57\u7b26\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u8bbe\u7f6e\u548c\u7ba1\u7406Python\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528\u7f16\u7801\u58f0\u660e<\/strong><\/h2>\n<p><h2>1\u3001\u7f16\u7801\u58f0\u660e\u7684\u91cd\u8981\u6027<\/h2>\n<\/p>\n<p><p>\u5728Python\u6587\u4ef6\u7684\u5f00\u5934\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\u662f\u786e\u4fdd\u4ee3\u7801\u6587\u4ef6\u6b63\u786e\u89e3\u6790\u7684\u5173\u952e\u6b65\u9aa4\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPython 3\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u663e\u5f0f\u58f0\u660e\u7f16\u7801\u683c\u5f0f\u4ecd\u7136\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\uff0c\u5c24\u5176\u662f\u5f53\u4ee3\u7801\u4e2d\u5305\u542b\u975eASCII\u5b57\u7b26\u65f6\u3002\u901a\u8fc7\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\uff0c\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u7684\u5404\u79cd\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5982\u4f55\u6dfb\u52a0\u7f16\u7801\u58f0\u660e<\/h2>\n<\/p>\n<p><p>\u5728Python\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\u6216\u7b2c\u4e8c\u884c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u58f0\u660e\u544a\u8bc9Python\u89e3\u91ca\u5668\uff0c\u8fd9\u4e2a\u6587\u4ef6\u4f7f\u7528\u7684\u662fUTF-8\u7f16\u7801\u3002\u8fd9\u4e2a\u58f0\u660e\u5e94\u8be5\u4f4d\u4e8e\u6587\u4ef6\u7684\u5f00\u5934\uff0c\u901a\u5e38\u5728\u6587\u4ef6\u7684\u6ce8\u91ca\u90e8\u5206\u4e4b\u540e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">#!\/usr\/bin\/env python3<\/p>\n<h2><strong>-*- coding: utf-8 -*-<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u5373\u4f7f\u6587\u4ef6\u4e2d\u5305\u542b\u975eASCII\u5b57\u7b26\uff0c\u4e5f\u80fd\u786e\u4fdd\u8fd9\u4e9b\u5b57\u7b26\u88ab\u6b63\u786e\u89e3\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u9009\u62e9\u5408\u9002\u7684\u6587\u672c\u7f16\u8f91\u5668<\/strong><\/h2>\n<p><h2>1\u3001\u4e3a\u4ec0\u4e48\u9009\u62e9\u5408\u9002\u7684\u6587\u672c\u7f16\u8f91\u5668\u5f88\u91cd\u8981<\/h2>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6587\u672c\u7f16\u8f91\u5668\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u7f16\u7801\u95ee\u9898\u3002\u4e00\u4e2a\u597d\u7684\u6587\u672c\u7f16\u8f91\u5668\u5e94\u8be5\u80fd\u591f\u81ea\u52a8\u68c0\u6d4b\u548c\u8f6c\u6362\u6587\u4ef6\u7f16\u7801\uff0c\u5e76\u63d0\u4f9b\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u66f4\u6539\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u63a8\u8350\u7684\u6587\u672c\u7f16\u8f91\u5668<\/h2>\n<\/p>\n<ul>\n<li><strong>Visual Studio Code<\/strong>\uff1a\u652f\u6301\u591a\u79cd\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6216\u547d\u4ee4\u9762\u677f\u8f7b\u677e\u66f4\u6539\u6587\u4ef6\u7f16\u7801\u3002<\/li>\n<li><strong>PyCharm<\/strong>\uff1a\u63d0\u4f9b\u5f3a\u5927\u7684\u7f16\u7801\u652f\u6301\uff0c\u53ef\u4ee5\u81ea\u52a8\u68c0\u6d4b\u6587\u4ef6\u7f16\u7801\u5e76\u5728\u9700\u8981\u65f6\u8f6c\u6362\u3002<\/li>\n<li><strong>Sublime Text<\/strong>\uff1a\u8f7b\u91cf\u7ea7\u7f16\u8f91\u5668\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u83dc\u5355\u8f7b\u677e\u8f6c\u6362\u6587\u4ef6\u7f16\u7801\u3002<\/li>\n<\/ul>\n<p><p>\u8fd9\u4e9b\u7f16\u8f91\u5668\u4e0d\u4ec5\u652f\u6301\u591a\u79cd\u7f16\u7801\u683c\u5f0f\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63d2\u4ef6\u548c\u6269\u5c55\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u5728\u4e0d\u540c\u7f16\u7801\u683c\u5f0f\u4e4b\u95f4\u5207\u6362\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u914d\u7f6e\u7f16\u8f91\u5668\u7f16\u7801\u683c\u5f0f<\/strong><\/h2>\n<p><h2>1\u3001Visual Studio Code<\/h2>\n<\/p>\n<p><h3>\u8bbe\u7f6e\u6587\u4ef6\u9ed8\u8ba4\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5728Visual Studio Code\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6587\u4ef6\u6765\u914d\u7f6e\u9ed8\u8ba4\u7f16\u7801\u3002\u6253\u5f00\u8bbe\u7f6e\u6587\u4ef6\uff08<code>settings.json<\/code>\uff09\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">&quot;files.encoding&quot;: &quot;utf8&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u66f4\u6539\u5f53\u524d\u6587\u4ef6\u7684\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u8981\u66f4\u6539\u5f53\u524d\u6587\u4ef6\u7684\u7f16\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u9762\u677f\uff08<code>Ctrl+Shift+P<\/code>\uff09\uff0c\u8f93\u5165\u201cChange File Encoding\u201d\uff0c\u7136\u540e\u9009\u62e9\u9700\u8981\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>2\u3001PyCharm<\/h2>\n<\/p>\n<p><h3>\u8bbe\u7f6e\u9879\u76ee\u9ed8\u8ba4\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5728PyCharm\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6765\u914d\u7f6e\u9879\u76ee\u7684\u9ed8\u8ba4\u7f16\u7801\u3002\u8fdb\u5165\u8bbe\u7f6e\uff08<code>File -&gt; Settings<\/code>\uff09\uff0c\u5bfc\u822a\u5230<code>Editor -&gt; File Encodings<\/code>\uff0c\u7136\u540e\u8bbe\u7f6e\u9879\u76ee\u9ed8\u8ba4\u7f16\u7801\u548c\u5168\u5c40\u9ed8\u8ba4\u7f16\u7801\u4e3aUTF-8\u3002<\/p>\n<\/p>\n<p><h3>\u66f4\u6539\u5f53\u524d\u6587\u4ef6\u7684\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u8981\u66f4\u6539\u5f53\u524d\u6587\u4ef6\u7684\u7f16\u7801\uff0c\u53ef\u4ee5\u53f3\u952e\u70b9\u51fb\u6587\u4ef6\u9009\u9879\u5361\uff0c\u9009\u62e9<code>File Encoding<\/code>\uff0c\u7136\u540e\u9009\u62e9\u9700\u8981\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>3\u3001Sublime Text<\/h2>\n<\/p>\n<p><h3>\u8bbe\u7f6e\u9ed8\u8ba4\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5728Sublime Text\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6587\u4ef6\u6765\u914d\u7f6e\u9ed8\u8ba4\u7f16\u7801\u3002\u6253\u5f00\u8bbe\u7f6e\u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">&quot;default_encoding&quot;: &quot;UTF-8&quot;,<\/p>\n<p>&quot;fallback_encoding&quot;: &quot;UTF-8&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u66f4\u6539\u5f53\u524d\u6587\u4ef6\u7684\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u8981\u66f4\u6539\u5f53\u524d\u6587\u4ef6\u7684\u7f16\u7801\uff0c\u53ef\u4ee5\u901a\u8fc7\u83dc\u5355<code>File -&gt; Reopen with Encoding<\/code>\u9009\u62e9\u9700\u8981\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u5408\u7406\u4f7f\u7528\u5b57\u7b26\u4e32\u7f16\u7801<\/strong><\/h2>\n<p><h2>1\u3001\u5b57\u7b26\u4e32\u7f16\u7801\u4e0e\u89e3\u7801<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u6709\u4e24\u79cd\u7c7b\u578b\uff1a\u5b57\u8282\u5b57\u7b26\u4e32\u548cUnicode\u5b57\u7b26\u4e32\u3002\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3002\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u5c06Unicode\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u8282\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUnicode\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Unicode\u5b57\u7b26\u4e32<\/p>\n<p>unicode_string = &#39;\u4f60\u597d\uff0c\u4e16\u754c&#39;<\/p>\n<h2><strong>\u7f16\u7801\u4e3a\u5b57\u8282\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>byte_string = unicode_string.encode(&#39;utf-8&#39;)<\/p>\n<h2><strong>\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>decoded_string = byte_string.decode(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u907f\u514d\u7f16\u7801\u9519\u8bef\u7684\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u7f16\u7801\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u5904\u7406\u6f5c\u5728\u7684\u7f16\u7801\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    byte_string = unicode_string.encode(&#39;utf-8&#39;)<\/p>\n<p>except UnicodeEncodeError as e:<\/p>\n<p>    print(f&quot;\u7f16\u7801\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6355\u83b7\u7f16\u7801\u5f02\u5e38\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u548c\u8c03\u8bd5\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u5904\u7406\u6587\u4ef6\u8bfb\u5199\u65f6\u6307\u5b9a\u7f16\u7801<\/strong><\/h2>\n<p><h2>1\u3001\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u65f6\uff0c\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u662f\u786e\u4fdd\u6587\u4ef6\u5185\u5bb9\u6b63\u786e\u89e3\u6790\u7684\u5173\u952e\u3002\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>encoding<\/code>\u53c2\u6570\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\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><h2>2\u3001\u5199\u5165\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u5728\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u4e5f\u9700\u8981\u6307\u5b9a\u7f16\u7801\uff0c\u4ee5\u786e\u4fdd\u5199\u5165\u7684\u5185\u5bb9\u80fd\u591f\u6b63\u786e\u89e3\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(&#39;\u4f60\u597d\uff0c\u4e16\u754c&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\uff0c\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u7684\u6587\u4ef6\u5185\u5bb9\u635f\u574f\u6216\u65e0\u6cd5\u6b63\u786e\u8bfb\u53d6\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u5904\u7406\u7f51\u7edc\u6570\u636e\u7f16\u7801<\/strong><\/h2>\n<p><h2>1\u3001\u5904\u7406HTTP\u54cd\u5e94\u5185\u5bb9<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5904\u7406HTTP\u54cd\u5e94\u5185\u5bb9\u7684\u7f16\u7801\u3002\u4f7f\u7528<code>requests<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u54cd\u5e94\u5bf9\u8c61\u7684<code>encoding<\/code>\u5c5e\u6027\u6765\u5904\u7406\u54cd\u5e94\u5185\u5bb9\u7684\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;https:\/\/example.com&#39;)<\/p>\n<p>response.encoding = &#39;utf-8&#39;<\/p>\n<p>content = response.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u5904\u7406JSON\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0c\u901a\u5e38\u9700\u8981\u786e\u4fddJSON\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u683c\u5f0f\u6b63\u786e\u3002\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7f16\u7801\u53c2\u6570\u6765\u5904\u7406JSON\u6570\u636e\u7684\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&#39;message&#39;: &#39;\u4f60\u597d\uff0c\u4e16\u754c&#39;}<\/p>\n<p>json_string = json.dumps(data, ensure_ascii=False).encode(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6b63\u786e\u5904\u7406\u7f51\u7edc\u6570\u636e\u7684\u7f16\u7801\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5728\u4f20\u8f93\u548c\u89e3\u6790\u8fc7\u7a0b\u4e2d\u4e0d\u51fa\u73b0\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u7f16\u7801\u8f6c\u6362\u5de5\u5177<\/strong><\/h2>\n<p><h2>1\u3001\u4f7f\u7528<code>codecs<\/code>\u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>codecs<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u7f16\u7801\u8f6c\u6362\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u7f16\u7801\u548c\u89e3\u7801\u5b57\u7b26\u4e32\u3001\u6587\u4ef6\u7b49\u6570\u636e\u3002\u4f7f\u7528<code>codecs<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u4e0d\u540c\u7f16\u7801\u683c\u5f0f\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import codecs<\/p>\n<p>with codecs.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><h2>2\u3001\u5e38\u7528\u7f16\u7801\u8f6c\u6362\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>codecs<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u5e38\u7528\u7684\u7f16\u7801\u8f6c\u6362\u51fd\u6570\uff0c\u5982<code>encode()<\/code>\u548c<code>decode()<\/code>\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u548c\u89e3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import codecs<\/p>\n<p>unicode_string = &#39;\u4f60\u597d\uff0c\u4e16\u754c&#39;<\/p>\n<p>byte_string = codecs.encode(unicode_string, &#39;utf-8&#39;)<\/p>\n<p>decoded_string = codecs.decode(byte_string, &#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>codecs<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u548c\u8f6c\u6362\u4e0d\u540c\u7f16\u7801\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u5904\u7406\u6570\u636e\u5e93\u7f16\u7801<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6e\u6570\u636e\u5e93\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u8bbe\u7f6e\u6570\u636e\u5e93\u7f16\u7801\u662f\u786e\u4fdd\u6570\u636e\u6b63\u786e\u5b58\u50a8\u548c\u8bfb\u53d6\u7684\u5173\u952e\u3002\u4e0d\u540c\u7684\u6570\u636e\u5e93\u6709\u4e0d\u540c\u7684\u8bbe\u7f6e\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5728MySQL\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6570\u636e\u5e93\u548c\u8868\u7684\u7f16\u7801\u683c\u5f0f\u6765\u786e\u4fdd\u6570\u636e\u7684\u7f16\u7801\u4e00\u81f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sql\">CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;<\/p>\n<p>CREATE TABLE mytable (<\/p>\n<p>    id INT AUTO_INCREMENT PRIMARY KEY,<\/p>\n<p>    message VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci<\/p>\n<p>);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u8fde\u63a5\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u7684\u7f16\u7801\u6765\u786e\u4fdd\u6570\u636e\u4f20\u8f93\u7684\u7f16\u7801\u4e00\u81f4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>pymysql<\/code>\u8fde\u63a5MySQL\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8fde\u63a5\u53c2\u6570\u6765\u6307\u5b9a\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>connection = pymysql.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;mydatabase&#39;,<\/p>\n<p>    charset=&#39;utf8mb4&#39;,<\/p>\n<p>    use_unicode=True<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u6570\u636e\u5e93\u548c\u8fde\u63a5\u7684\u7f16\u7801\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5728\u5b58\u50a8\u548c\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u4e0d\u51fa\u73b0\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u4e5d\u3001\u5904\u7406\u547d\u4ee4\u884c\u8f93\u5165\u8f93\u51fa\u7f16\u7801<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6e\u547d\u4ee4\u884c\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u547d\u4ee4\u884c\u8f93\u5165\u8f93\u51fa\u65f6\uff0c\u8bbe\u7f6e\u547d\u4ee4\u884c\u7684\u7f16\u7801\u662f\u786e\u4fdd\u6570\u636e\u6b63\u786e\u663e\u793a\u548c\u8bfb\u53d6\u7684\u5173\u952e\u3002\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u547d\u4ee4\u884c\u6709\u4e0d\u540c\u7684\u8bbe\u7f6e\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>chcp<\/code>\u547d\u4ee4\u6765\u66f4\u6539\u547d\u4ee4\u884c\u7684\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">chcp 65001<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u65f6\uff0c\u786e\u4fdd\u53c2\u6570\u7684\u7f16\u7801\u683c\u5f0f\u6b63\u786e\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u7684\u9519\u8bef\u3002\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u53ef\u4ee5\u83b7\u53d6\u548c\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>for arg in sys.argv:<\/p>\n<p>    print(arg.encode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u547d\u4ee4\u884c\u7f16\u7801\u548c\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u7684\u7f16\u7801\uff0c\u53ef\u4ee5\u786e\u4fdd\u547d\u4ee4\u884c\u8f93\u5165\u8f93\u51fa\u7684\u7f16\u7801\u4e00\u81f4\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u3001\u5904\u7406\u65e5\u5fd7\u6587\u4ef6\u7f16\u7801<\/strong><\/h2>\n<p><h2>1\u3001\u914d\u7f6e\u65e5\u5fd7\u6587\u4ef6\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u65e5\u5fd7\u6587\u4ef6\u65f6\uff0c\u914d\u7f6e\u65e5\u5fd7\u6587\u4ef6\u7684\u7f16\u7801\u662f\u786e\u4fdd\u65e5\u5fd7\u5185\u5bb9\u6b63\u786e\u8bb0\u5f55\u548c\u8bfb\u53d6\u7684\u5173\u952e\u3002\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6587\u4ef6\u5904\u7406\u5668\u7684\u7f16\u7801\u6765\u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u7684\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(<\/p>\n<p>    filename=&#39;example.log&#39;,<\/p>\n<p>    level=logging.DEBUG,<\/p>\n<p>    format=&#39;%(asctime)s %(message)s&#39;,<\/p>\n<p>    encoding=&#39;utf-8&#39;<\/p>\n<p>)<\/p>\n<p>logging.debug(&#39;\u8fd9\u662f\u4e00\u6761\u8c03\u8bd5\u4fe1\u606f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u65e5\u5fd7\u6587\u4ef6\u8bfb\u53d6\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u6b63\u786e\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u7684\u8bfb\u53d6\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.log&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u914d\u7f6e\u65e5\u5fd7\u6587\u4ef6\u7684\u7f16\u7801\uff0c\u53ef\u4ee5\u786e\u4fdd\u65e5\u5fd7\u5185\u5bb9\u5728\u8bb0\u5f55\u548c\u8bfb\u53d6\u8fc7\u7a0b\u4e2d\u4e0d\u51fa\u73b0\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e00\u3001\u5904\u7406GUI\u5e94\u7528\u7f16\u7801<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6eGUI\u5e94\u7528\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u53d1GUI\u5e94\u7528\u65f6\uff0c\u8bbe\u7f6e\u5e94\u7528\u7684\u7f16\u7801\u662f\u786e\u4fdd\u754c\u9762\u663e\u793a\u548c\u5904\u7406\u6570\u636e\u6b63\u786e\u7684\u5173\u952e\u3002\u4e0d\u540c\u7684GUI\u6846\u67b6\u6709\u4e0d\u540c\u7684\u8bbe\u7f6e\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528<code>tkinter<\/code>\u5f00\u53d1GUI\u5e94\u7528\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7a97\u53e3\u7684\u7f16\u7801\u6765\u786e\u4fdd\u754c\u9762\u663e\u793a\u6b63\u786e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&#39;\u4f60\u597d\uff0c\u4e16\u754c&#39;)<\/p>\n<p>root.m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u5904\u7406GUI\u8f93\u5165\u8f93\u51fa\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406GUI\u5e94\u7528\u7684\u8f93\u5165\u8f93\u51fa\u65f6\uff0c\u786e\u4fdd\u6570\u636e\u7684\u7f16\u7801\u683c\u5f0f\u6b63\u786e\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u7684\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_submit():<\/p>\n<p>    user_input = entry.get()<\/p>\n<p>    print(user_input.encode(&#39;utf-8&#39;))<\/p>\n<p>root = tk.Tk()<\/p>\n<p>entry = tk.Entry(root)<\/p>\n<p>entry.pack()<\/p>\n<p>button = tk.Button(root, text=&#39;Submit&#39;, command=on_submit)<\/p>\n<p>button.pack()<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6eGUI\u5e94\u7528\u7684\u7f16\u7801\u548c\u5904\u7406\u8f93\u5165\u8f93\u51fa\u7684\u7f16\u7801\uff0c\u53ef\u4ee5\u786e\u4fdd\u754c\u9762\u663e\u793a\u548c\u6570\u636e\u5904\u7406\u7684\u7f16\u7801\u4e00\u81f4\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e8c\u3001\u5904\u7406\u591a\u8bed\u8a00\u652f\u6301\u7f16\u7801<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6e\u591a\u8bed\u8a00\u6587\u4ef6\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u8bed\u8a00\u652f\u6301\u65f6\uff0c\u8bbe\u7f6e\u591a\u8bed\u8a00\u6587\u4ef6\u7684\u7f16\u7801\u662f\u786e\u4fdd\u6587\u672c\u5185\u5bb9\u6b63\u786e\u89e3\u6790\u548c\u663e\u793a\u7684\u5173\u952e\u3002\u901a\u5e38\uff0c\u591a\u8bed\u8a00\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4ee5\u652f\u6301\u591a\u79cd\u8bed\u8a00\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># messages.py<\/p>\n<p>messages = {<\/p>\n<p>    &#39;en&#39;: &#39;Hello, World!&#39;,<\/p>\n<p>    &#39;zh&#39;: &#39;\u4f60\u597d\uff0c\u4e16\u754c&#39;,<\/p>\n<p>    &#39;es&#39;: &#39;\u00a1Hola, Mundo!&#39;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u52a0\u8f7d\u591a\u8bed\u8a00\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u52a0\u8f7d\u591a\u8bed\u8a00\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u6b63\u786e\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u7684\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>with open(&#39;messages.json&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    messages = json.load(file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u591a\u8bed\u8a00\u6587\u4ef6\u7684\u7f16\u7801\u548c\u6b63\u786e\u52a0\u8f7d\u6587\u4ef6\uff0c\u53ef\u4ee5\u786e\u4fdd\u591a\u8bed\u8a00\u652f\u6301\u7684\u6587\u672c\u5185\u5bb9\u5728\u89e3\u6790\u548c\u663e\u793a\u8fc7\u7a0b\u4e2d\u4e0d\u51fa\u73b0\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e09\u3001\u5904\u7406\u90ae\u4ef6\u7f16\u7801<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6e\u90ae\u4ef6\u5185\u5bb9\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u90ae\u4ef6\u65f6\uff0c\u8bbe\u7f6e\u90ae\u4ef6\u5185\u5bb9\u7684\u7f16\u7801\u662f\u786e\u4fdd\u90ae\u4ef6\u5185\u5bb9\u6b63\u786e\u663e\u793a\u548c\u4f20\u8f93\u7684\u5173\u952e\u3002\u4f7f\u7528<code>email<\/code>\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u90ae\u4ef6\u5bf9\u8c61\u7684\u7f16\u7801\u6765\u6307\u5b9a\u90ae\u4ef6\u5185\u5bb9\u7684\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from email.mime.text import MIMEText<\/p>\n<p>message = MIMEText(&#39;\u4f60\u597d\uff0c\u4e16\u754c&#39;, &#39;plain&#39;, &#39;utf-8&#39;)<\/p>\n<p>message[&#39;Subject&#39;] = &#39;\u6d4b\u8bd5\u90ae\u4ef6&#39;<\/p>\n<p>message[&#39;From&#39;] = &#39;sender@example.com&#39;<\/p>\n<p>message[&#39;To&#39;] = &#39;recipient@example.com&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u53d1\u9001\u90ae\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u53d1\u9001\u90ae\u4ef6\u65f6\uff0c\u786e\u4fdd\u90ae\u4ef6\u5185\u5bb9\u7684\u7f16\u7801\u683c\u5f0f\u6b63\u786e\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u7684\u90ae\u4ef6\u5185\u5bb9\u635f\u574f\u6216\u65e0\u6cd5\u6b63\u786e\u663e\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import smtplib<\/p>\n<p>with smtplib.SMTP(&#39;smtp.example.com&#39;) as server:<\/p>\n<p>    server.login(&#39;user&#39;, &#39;password&#39;)<\/p>\n<p>    server.sendmail(&#39;sender@example.com&#39;, &#39;recipient@example.com&#39;, message.as_string())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u90ae\u4ef6\u5185\u5bb9\u7684\u7f16\u7801\u548c\u6b63\u786e\u53d1\u9001\u90ae\u4ef6\uff0c\u53ef\u4ee5\u786e\u4fdd\u90ae\u4ef6\u5185\u5bb9\u5728\u4f20\u8f93\u548c\u663e\u793a\u8fc7\u7a0b\u4e2d\u4e0d\u51fa\u73b0\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u56db\u3001\u5904\u7406Excel\u6587\u4ef6\u7f16\u7801<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6eExcel\u6587\u4ef6\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406Excel\u6587\u4ef6\u65f6\uff0c\u8bbe\u7f6e\u6587\u4ef6\u7684\u7f16\u7801\u662f\u786e\u4fdd\u6570\u636e\u6b63\u786e\u8bfb\u53d6\u548c\u5199\u5165\u7684\u5173\u952e\u3002\u4f7f\u7528<code>pandas<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6587\u4ef6\u7684\u7f16\u7801\u53c2\u6570\u6765\u6307\u5b9aExcel\u6587\u4ef6\u7684\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_excel(&#39;example.xlsx&#39;, encoding=&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u5199\u5165Excel\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5199\u5165Excel\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u6b63\u786e\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u7684\u6587\u4ef6\u5185\u5bb9\u635f\u574f\u6216\u65e0\u6cd5\u6b63\u786e\u8bfb\u53d6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df.to_excel(&#39;example.xlsx&#39;, encoding=&#39;utf-8&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6eExcel\u6587\u4ef6\u7684\u7f16\u7801\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5728\u8bfb\u53d6\u548c\u5199\u5165\u8fc7\u7a0b\u4e2d\u4e0d\u51fa\u73b0\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e94\u3001\u5904\u7406API\u6570\u636e\u7f16\u7801<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6eAPI\u8bf7\u6c42\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406API\u8bf7\u6c42\u65f6\uff0c\u8bbe\u7f6e\u8bf7\u6c42\u7684\u7f16\u7801\u662f\u786e\u4fdd\u6570\u636e\u6b63\u786e\u4f20\u8f93\u548c\u89e3\u6790\u7684\u5173\u952e\u3002\u4f7f\u7528<code>requests<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u5934\u7684\u7f16\u7801\u6765\u6307\u5b9a\u8bf7\u6c42\u7684\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>headers = {&#39;Content-Type&#39;: &#39;application\/json; charset=utf-8&#39;}<\/p>\n<p>response = requests.post(&#39;https:\/\/api.example.com\/data&#39;, headers=headers, json={&#39;message&#39;: &#39;\u4f60\u597d\uff0c\u4e16\u754c&#39;})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u5904\u7406API\u54cd\u5e94\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406API\u54cd\u5e94\u65f6\uff0c\u786e\u4fdd\u54cd\u5e94\u5185\u5bb9\u7684\u7f16\u7801\u683c\u5f0f\u6b63\u786e\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u7684\u6570\u636e\u89e3\u6790\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">response.encoding = &#39;utf-8&#39;<\/p>\n<p>data = response.json()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6eAPI\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u7f16\u7801\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5728\u4f20\u8f93\u548c\u89e3\u6790\u8fc7\u7a0b\u4e2d\u4e0d\u51fa\u73b0\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u516d\u3001\u5904\u7406XML\u6587\u4ef6\u7f16\u7801<\/strong><\/h2>\n<p><h2>1\u3001\u8bbe\u7f6eXML\u6587\u4ef6\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406XML\u6587\u4ef6\u65f6\uff0c\u8bbe\u7f6e\u6587\u4ef6\u7684\u7f16\u7801\u662f\u786e\u4fdd\u6570\u636e\u6b63\u786e\u89e3\u6790\u548c\u5199\u5165\u7684\u5173\u952e\u3002\u4f7f\u7528<code>xml.etree.ElementTree<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eXML\u58f0\u660e\u7684\u7f16\u7801\u6765\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<p>root = ET.Element(&#39;root&#39;)<\/p>\n<p>tree = ET.ElementTree(root)<\/p>\n<p>tree.write(&#39;example.xml&#39;, encoding=&#39;utf-8&#39;, xml_declaration=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u8bfb\u53d6XML\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6XML\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u6b63\u786e\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u7684\u89e3\u6790\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tree = ET.parse(&#39;example.xml&#39;)<\/p>\n<p>root = tree.getroot()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6eXML\u6587\u4ef6\u7684\u7f16\u7801\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5728\u89e3\u6790\u548c\u5199\u5165\u8fc7\u7a0b\u4e2d\u4e0d\u51fa\u73b0\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff0c\u901a\u8fc7\u5728Python\u6587\u4ef6\u4e2d\u4f7f\u7528\u7f16\u7801\u58f0\u660e\u3001\u9009\u62e9\u5408\u9002\u7684\u6587\u672c\u7f16\u8f91\u5668\u3001\u914d\u7f6e\u7f16\u8f91\u5668\u7f16\u7801\u683c\u5f0f\u3001\u5408\u7406\u4f7f\u7528\u5b57\u7b26\u4e32\u7f16\u7801\u3001\u5904\u7406\u6587\u4ef6\u8bfb\u5199\u65f6\u6307\u5b9a\u7f16\u7801\uff0c\u4ee5\u53ca\u5904\u7406\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u7f16\u7801\u95ee\u9898\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5728\u89e3\u6790\u3001\u4f20\u8f93\u548c\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u4e0d\u51fa\u73b0\u7f16\u7801\u9519\u8bef\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u548c\u7ba1\u7406Python\u7f16\u7801\u683c\u5f0f\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u5f53\u524d\u7f16\u7801\u683c\u5f0f\uff1f<\/strong><br \/>\u8981\u68c0\u67e5Python\u811a\u672c\u7684\u5f53\u524d\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u4e2d\u7684<code>getdefaultencoding()<\/code>\u65b9\u6cd5\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import sys\nprint(sys.getdefaultencoding())\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u5f53\u524d\u7684\u9ed8\u8ba4\u7f16\u7801\u683c\u5f0f\uff0c\u901a\u5e38\u662f&#39;utf-8&#39;\uff0c\u4f46\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u4e0d\u540c\u7f16\u7801\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u5728\u5904\u7406\u4e0d\u540c\u7f16\u7801\u7684\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u7684<code>encoding<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;filename.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n    content = file.read()\n<\/code><\/pre>\n<p>\u8fd9\u6837\u80fd\u591f\u786e\u4fdd\u4ee5\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u907f\u514d\u51fa\u73b0\u7f16\u7801\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u66f4\u6539\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>encode()<\/code>\u548c<code>decode()<\/code>\u65b9\u6cd5\u6765\u66f4\u6539\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u683c\u5f0f\u3002\u5bf9\u4e8ePython 3\uff0c\u5b57\u7b26\u4e32\u662f\u4ee5Unicode\u683c\u5f0f\u5b58\u50a8\u7684\uff0c\u4f7f\u7528<code>encode()<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u8282\u4e32\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;\nencoded_string = original_string.encode(&#39;utf-8&#39;)\n<\/code><\/pre>\n<p>\u8981\u5c06\u5b57\u8282\u4e32\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\uff1a  <\/p>\n<pre><code class=\"language-python\">decoded_string = encoded_string.decode(&#39;utf-8&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u4e0d\u540c\u7f16\u7801\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u8bbe\u7f6ePython\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f16\u7801\u58f0\u660e\u3001\u9009\u62e9\u5408\u9002\u7684\u6587\u672c\u7f16\u8f91\u5668\u3001\u914d\u7f6e\u7f16\u8f91\u5668\u7f16\u7801\u683c\u5f0f\u3001\u5408\u7406\u4f7f\u7528\u5b57\u7b26\u4e32\u7f16\u7801\u3001\u5904 [&hellip;]","protected":false},"author":3,"featured_media":1152264,"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\/1152257"}],"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=1152257"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152257\/revisions"}],"predecessor-version":[{"id":1152266,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152257\/revisions\/1152266"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1152264"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1152257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1152257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1152257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}