{"id":986463,"date":"2024-12-27T07:45:06","date_gmt":"2024-12-26T23:45:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/986463.html"},"modified":"2024-12-27T07:45:07","modified_gmt":"2024-12-26T23:45:07","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e6%98%be%e7%a4%ba%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/986463.html","title":{"rendered":"python\u5982\u4f55\u8ba9\u663e\u793a\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063030\/70c85ccc-ba24-460c-82a6-051ac1a76aa1.webp\" alt=\"python\u5982\u4f55\u8ba9\u663e\u793a\u4e2d\u6587\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u663e\u793a\u4e2d\u6587\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528UTF-8\u7f16\u7801\u3001\u786e\u4fdd\u7f16\u8f91\u5668\u652f\u6301\u4e2d\u6587\u3001\u4f7f\u7528\u5408\u9002\u7684\u5b57\u4f53\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528UTF-8\u7f16\u7801\u662f\u6700\u5173\u952e\u7684\uff0c\u56e0\u4e3aUTF-8\u662f\u56fd\u9645\u901a\u7528\u7684\u5b57\u7b26\u7f16\u7801\u6807\u51c6\uff0c\u652f\u6301\u4e2d\u6587\u7b49\u591a\u79cd\u8bed\u8a00\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPython 3\u5df2\u7ecf\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8\u8bbe\u7f6e\u7f16\u7801\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u786e\u4fddPython\u7a0b\u5e8f\u6b63\u786e\u663e\u793a\u4e2d\u6587\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001UTF-8\u7f16\u7801\u8bbe\u7f6e<\/p>\n<\/p>\n<p><p>\u786e\u4fddPython\u7a0b\u5e8f\u4f7f\u7528UTF-8\u7f16\u7801\u662f\u663e\u793a\u4e2d\u6587\u7684\u5173\u952e\u3002Python 3\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4f46\u5728\u67d0\u4e9b\u7cfb\u7edf\u6216\u7f16\u8f91\u5668\u73af\u5883\u4e0b\u53ef\u80fd\u9700\u8981\u624b\u52a8\u6307\u5b9a\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5728Python\u6587\u4ef6\u4e2d\u6307\u5b9a\u7f16\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u6587\u4ef6\u7684\u9876\u90e8\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff0c\u53ef\u4ee5\u660e\u786e\u6307\u5b9a\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\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\u884c\u4ee3\u7801\u544a\u8bc9Python\u89e3\u91ca\u5668\u4f7f\u7528UTF-8\u7f16\u7801\u89e3\u91ca\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26\u3002\u8fd9\u5728Python 2\u4e2d\u5c24\u5176\u91cd\u8981\uff0c\u56e0\u4e3aPython 2\u9ed8\u8ba4\u4f7f\u7528ASCII\u7f16\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ec8\u7aef\u6216\u547d\u4ee4\u884c\u663e\u793a\u4e2d\u6587<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7ec8\u7aef\u6216\u547d\u4ee4\u884c\u73af\u5883\u7684\u7f16\u7801\u8bbe\u7f6e\u53ef\u80fd\u4e0d\u652f\u6301\u4e2d\u6587\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7ec8\u7aef\u7684\u7f16\u7801\u4e3aUTF-8\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u66f4\u6539\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">chcp 65001<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Linux\u548cmacOS\u7cfb\u7edf\u4e2d\uff0c\u901a\u5e38\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4e0d\u9700\u8981\u989d\u5916\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u7f16\u8f91\u5668\u548c\u73af\u5883\u914d\u7f6e<\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684\u4ee3\u7801\u7f16\u8f91\u5668\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u663e\u793a\u662f\u53e6\u4e00\u4e2a\u91cd\u8981\u56e0\u7d20\u3002\u5927\u591a\u6570\u73b0\u4ee3\u7f16\u8f91\u5668\uff0c\u5982VSCode\u3001PyCharm\u3001Sublime Text\u7b49\uff0c\u9ed8\u8ba4\u652f\u6301UTF-8\u7f16\u7801\u5e76\u80fd\u6b63\u786e\u663e\u793a\u4e2d\u6587\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u914d\u7f6e\u7f16\u8f91\u5668<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>VSCode<\/strong>\uff1a\u6253\u5f00\u8bbe\u7f6e\uff0c\u641c\u7d22\u201cfiles.encoding\u201d\uff0c\u786e\u4fdd\u5176\u503c\u4e3a\u201cutf8\u201d\u3002<\/li>\n<li><strong>PyCharm<\/strong>\uff1a\u5728File -&gt; Settings -&gt; Editor -&gt; File Encodings\u4e2d\uff0c\u5c06Project Encoding\u8bbe\u7f6e\u4e3aUTF-8\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u9009\u62e9\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u7684\u5b57\u4f53\u53ef\u4ee5\u907f\u514d\u5b57\u7b26\u663e\u793a\u4e3a\u65b9\u5757\u6216\u4e71\u7801\u3002\u5e38\u7528\u7684\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\u5305\u62ecMicrosoft YaHei\u3001SimSun\u3001Arial Unicode MS\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u6b63\u786e\u6267\u884c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b57\u7b26\u4e32\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>Python 3\u4e2d\u7684\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662fUnicode\u7f16\u7801\uff0c\u53ef\u4ee5\u76f4\u63a5\u5904\u7406\u4e2d\u6587\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">chinese_text = &quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01&quot;<\/p>\n<p>print(chinese_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u6b63\u786e\u8f93\u51fa\u201c\u4f60\u597d\uff0c\u4e16\u754c\uff01\u201d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6587\u4ef6\u8bfb\u5199<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8bfb\u5199\u5305\u542b\u4e2d\u6587\u7684\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528UTF-8\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;chinese_text.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u540c\u6837\u6307\u5b9a\u7f16\u7801\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(chinese_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5904\u7406\u4e2d\u6587\u8f93\u51fa\u548c\u8f93\u5165<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u8f93\u5165\u8f93\u51fa\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u5408\u9002\u7684\u8f93\u5165\u65b9\u6cd5\u548c\u8f93\u51fa\u8bbe\u5907\u6765\u652f\u6301\u4e2d\u6587\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u547d\u4ee4\u884c\u8f93\u5165<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528input()\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u4e2d\u6587\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e2d\u6587\uff1a&quot;)<\/p>\n<p>print(&quot;\u4f60\u8f93\u5165\u7684\u662f\uff1a&quot;, user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u547d\u4ee4\u884c\u73af\u5883\u652f\u6301\u4e2d\u6587\u8f93\u5165\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u6765\u8f93\u51fa\u5305\u542b\u4e2d\u6587\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;\u5f20\u4e09&quot;<\/p>\n<p>print(f&quot;\u4f60\u597d, {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u4f7f\u7528.format()\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;\u4f60\u597d, {}!&quot;.format(name))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5e38\u89c1\u9519\u8bef\u53ca\u89e3\u51b3\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f16\u7801\u9519\u8bef\u6216\u663e\u793a\u95ee\u9898\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>UnicodeEncodeError<\/strong><\/p>\n<\/p>\n<p><p>\u8fd9\u901a\u5e38\u53d1\u751f\u5728\u5c06Unicode\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282\u5b57\u7b26\u4e32\u65f6\u3002\u4f7f\u7528UTF-8\u7f16\u7801\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">utf8_text = chinese_text.encode(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>UnicodeDecodeError<\/strong><\/p>\n<\/p>\n<p><p>\u8fd9\u901a\u5e38\u53d1\u751f\u5728\u5c06\u5b57\u8282\u5b57\u7b26\u4e32\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32\u65f6\u3002\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u89e3\u7801\u5b57\u8282\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">decoded_text = byte_text.decode(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u663e\u793a\u4e71\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u5728\u7ec8\u7aef\u6216\u7f16\u8f91\u5668\u4e2d\u770b\u5230\u4e71\u7801\uff0c\u68c0\u67e5\u7f16\u7801\u8bbe\u7f6e\u662f\u5426\u4e3aUTF-8\uff0c\u5e76\u786e\u4fdd\u4f7f\u7528\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fddPython\u7a0b\u5e8f\u4e2d\u6b63\u786e\u663e\u793a\u548c\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002\u4e86\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u5728Python\u4e2d\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6b63\u786e\u663e\u793a\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\u663e\u793a\u4e2d\u6587\u5b57\u7b26\uff0c\u901a\u5e38\u9700\u8981\u786e\u4fdd\u4f60\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u5e76\u4e14\u5728\u6253\u5370\u8f93\u51fa\u65f6\u8bbe\u7f6e\u5408\u9002\u7684\u7f16\u7801\u3002\u5728Python 3\u4e2d\uff0c\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u56e0\u6b64\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u76f4\u63a5\u4f7f\u7528\u4e2d\u6587\u5b57\u7b26\u5373\u53ef\u3002\u5982\u679c\u9700\u8981\u5728\u7ec8\u7aef\u6216\u63a7\u5236\u53f0\u4e2d\u663e\u793a\u4e2d\u6587\uff0c\u786e\u4fdd\u4f60\u7684\u7ec8\u7aef\u652f\u6301UTF-8\u7f16\u7801\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u65f6\uff0c\u7f16\u7801\u95ee\u9898\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e71\u7801\u3002\u53ef\u4ee5\u4f7f\u7528<code>str.encode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282\u7c7b\u578b\uff0c\u6216\u4f7f\u7528<code>bytes.decode()<\/code>\u5c06\u5b57\u8282\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u786e\u4fdd\u5728\u5904\u7406\u6587\u4ef6\u8bfb\u5199\u65f6\u4f7f\u7528\u76f8\u5e94\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4f8b\u5982<code>open(&#39;filename.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;)<\/code>\uff0c\u4ee5\u907f\u514d\u4e71\u7801\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bfb\u53d6\u548c\u5199\u5165\u5305\u542b\u4e2d\u6587\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u8bfb\u53d6\u548c\u5199\u5165\u5305\u542b\u4e2d\u6587\u7684\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u65f6\u6307\u5b9a<code>encoding=&#39;utf-8&#39;<\/code>\u53ef\u4ee5\u786e\u4fdd\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u8bfb\u53d6\u6587\u4ef6\u65f6\u53ef\u4ee5\u4f7f\u7528<code>with open(&#39;file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:<\/code>\uff0c\u5199\u5165\u6587\u4ef6\u65f6\u540c\u6837\u9700\u8981\u6307\u5b9a\u7f16\u7801\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u4e0d\u5339\u914d\u800c\u5bfc\u81f4\u7684\u6570\u636e\u4e22\u5931\u6216\u4e71\u7801\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u663e\u793a\u4e2d\u6587\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528UTF-8\u7f16\u7801\u3001\u786e\u4fdd\u7f16\u8f91\u5668\u652f\u6301\u4e2d\u6587\u3001\u4f7f\u7528\u5408\u9002\u7684\u5b57\u4f53\u3002\u5176\u4e2d\uff0c\u4f7f\u7528UTF- [&hellip;]","protected":false},"author":3,"featured_media":986470,"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\/986463"}],"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=986463"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986463\/revisions"}],"predecessor-version":[{"id":986472,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986463\/revisions\/986472"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/986470"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=986463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=986463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=986463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}