{"id":997967,"date":"2024-12-27T09:28:42","date_gmt":"2024-12-27T01:28:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/997967.html"},"modified":"2024-12-27T09:28:47","modified_gmt":"2024-12-27T01:28:47","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e8%be%93%e5%87%ba%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/997967.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528python\u8f93\u51fa\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073603\/59e9ccd6-e698-43c2-b2ba-e8595f5c2d8d.webp\" alt=\"\u5982\u4f55\u4f7f\u7528python\u8f93\u51fa\u4e2d\u6587\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u8f93\u51fa\u4e2d\u6587\uff0c\u53ef\u4ee5\u4f7f\u7528\u9002\u5f53\u7684\u7f16\u7801\u3001\u8bbe\u7f6e\u7f16\u7801\u58f0\u660e\u3001\u4f7f\u7528unicode\u5b57\u7b26\u4e32\u3001\u4ee5\u53ca\u786e\u4fdd\u7ec8\u7aef\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u8bbe\u7f6e\u7f16\u7801\u58f0\u660e<\/strong>\u662f\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\uff0c\u53ef\u4ee5\u786e\u4fddPython\u89e3\u91ca\u5668\u6b63\u786e\u7406\u89e3\u6e90\u4ee3\u7801\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u652f\u6301\u591a\u79cd\u5b57\u7b26\u96c6\uff0c\u8fd9\u4f7f\u5f97\u5b83\u80fd\u591f\u5904\u7406\u548c\u8f93\u51fa\u5305\u62ec\u4e2d\u6587\u5728\u5185\u7684\u591a\u79cd\u8bed\u8a00\u3002\u7136\u800c\uff0c\u8981\u6b63\u786e\u5730\u8f93\u51fa\u4e2d\u6587\uff0c\u9700\u8981\u9075\u5faa\u4e00\u4e9b\u6b65\u9aa4\u548c\u6280\u5de7\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u9002\u5f53\u7684\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c24\u5176\u662fPython 3\uff0c\u9ed8\u8ba4\u5b57\u7b26\u7f16\u7801\u662fUTF-8\uff0c\u8fd9\u5df2\u7ecf\u53ef\u4ee5\u5f88\u597d\u5730\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u3002\u7136\u800c\uff0c\u5728Python 2\u4e2d\uff0c\u9ed8\u8ba4\u7f16\u7801\u662fASCII\uff0c\u8fd9\u5c31\u9700\u8981\u663e\u5f0f\u5730\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h4>Python 3<\/h4>\n<\/p>\n<p><p>Python 3\u4e2d\uff0c\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662fUnicode\u7f16\u7801\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u76f4\u63a5\u8f93\u51fa\u4e2d\u6587\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>Python 2<\/h4>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c\u4f60\u9700\u8981\u5728\u6587\u4ef6\u5934\u90e8\u58f0\u660e\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>print u&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c\u4f7f\u7528<code>u<\/code>\u524d\u7f00\u6765\u8868\u793aUnicode\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6e\u7f16\u7801\u58f0\u660e<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python 2\u65f6\uff0c\u8bbe\u7f6e\u7f16\u7801\u58f0\u660e\u662f\u786e\u4fdd\u6e90\u4ee3\u7801\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u88ab\u6b63\u786e\u89e3\u91ca\u7684\u5173\u952e\u6b65\u9aa4\u3002\u8fd9\u901a\u5e38\u662f\u5728\u4ee3\u7801\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\u6216\u7b2c\u4e8c\u884c\u6dfb\u52a0\u5982\u4e0b\u58f0\u660e\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\u7f16\u7801\u58f0\u660e\u544a\u8bc9Python\u89e3\u91ca\u5668\u4f7f\u7528UTF-8\u7f16\u7801\u6765\u89e3\u91ca\u6e90\u4ee3\u7801\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Unicode\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u53ef\u4ee5\u4fdd\u8bc1\u7a0b\u5e8f\u5728\u5904\u7406\u591a\u79cd\u8bed\u8a00\u5b57\u7b26\u65f6\u4e0d\u4f1a\u51fa\u73b0\u4e71\u7801\u3002\u5728Python 3\u4e2d\uff0c\u6240\u6709\u5b57\u7b26\u4e32\u9ed8\u8ba4\u90fd\u662fUnicode\uff0c\u4f46\u5728Python 2\u4e2d\uff0c\u9700\u8981\u663e\u5f0f\u58f0\u660e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">u&quot;\u8fd9\u662f\u4e00\u4e2aUnicode\u5b57\u7b26\u4e32&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u53ef\u4ee5\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u5728\u4e0d\u540c\u5e73\u53f0\u548c\u73af\u5883\u4e2d\u90fd\u80fd\u6b63\u786e\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u786e\u4fdd\u7ec8\u7aef\u652f\u6301\u4e2d\u6587\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5373\u4f7f\u4ee3\u7801\u6b63\u786e\uff0c\u8f93\u51fa\u73af\u5883\u4e5f\u9700\u8981\u80fd\u591f\u6b63\u786e\u663e\u793a\u4e2d\u6587\u5b57\u7b26\u3002\u786e\u4fdd\u4f60\u7684\u7ec8\u7aef\u6216\u547d\u4ee4\u884c\u5de5\u5177\u652f\u6301UTF-8\u7f16\u7801\u3002\u5982\u679c\u5728Windows\u7cfb\u7edf\u4e0a\u4f7f\u7528\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u66f4\u6539\u4ee3\u7801\u9875\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">chcp 65001<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u8bbe\u7f6e\u53ef\u4ee5\u4f7f\u547d\u4ee4\u63d0\u793a\u7b26\u652f\u6301UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7279\u522b\u662f\u5f53\u4f60\u9700\u8981\u5728\u56fe\u5f62\u7528\u6237\u754c\u9762\u6216\u590d\u6742\u7684\u6587\u672c\u5904\u7406\u73af\u5883\u4e2d\u5904\u7406\u4e2d\u6587\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3002\u4f8b\u5982\uff0c<code>tkinter<\/code>\u5e93\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u652f\u6301\u4e2d\u6587\u7684GUI\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>root = tk.Tk()<\/p>\n<p>label = tk.Label(root, text=&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;)<\/p>\n<p>label.pack()<\/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><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728GUI\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6b63\u786e\u663e\u793a\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6587\u4ef6\u64cd\u4f5c\u4e2d\u7684\u4e2d\u6587\u652f\u6301<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6587\u4ef6\u7684\u8bfb\u5199\u7f16\u7801\u4e5f\u662fUTF-8\u3002\u4f8b\u5982\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 f:<\/p>\n<p>    f.write(&#39;\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u4e2d\u6587\u7684\u6587\u4ef6&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u540c\u6837\u9700\u8981\u6307\u5b9a\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 f:<\/p>\n<p>    content = f.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u8c03\u8bd5\u548c\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u8f93\u51fa\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f16\u7801\u9519\u8bef\u3002\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec<code>UnicodeEncodeError<\/code>\u6216<code>UnicodeDecodeError<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e9b\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    print(&quot;\u8f93\u51fa\u4e00\u4e9b\u4e2d\u6587&quot;)<\/p>\n<p>except UnicodeEncodeError as e:<\/p>\n<p>    print(&quot;\u7f16\u7801\u9519\u8bef:&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u5b9a\u4f4d\u548c\u4fee\u590d\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528Python\u7684<code>sys<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8\u8bbe\u7f6e\u6807\u51c6\u8f93\u51fa\u6d41\u7684\u7f16\u7801\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>sys<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.stdout.reconfigure(encoding=&#39;utf-8&#39;)<\/p>\n<p>print(&quot;\u4f7f\u7528sys\u6a21\u5757\u8bbe\u7f6e\u6807\u51c6\u8f93\u51fa\u7f16\u7801&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u5728\u6240\u6709\u8f93\u51fa\u4e2d\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u6b63\u786e\u8f93\u51fa\u4e2d\u6587\u5728Python\u4e2d\u5e76\u4e0d\u590d\u6742\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7f16\u7801\u8bbe\u7f6e\u548c\u8f93\u51fa\u73af\u5883\u3002\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u5e94\u8be5\u80fd\u591f\u987a\u5229\u5730\u5728Python\u7a0b\u5e8f\u4e2d\u5904\u7406\u548c\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\u3002\u786e\u4fdd\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u3001\u8bbe\u7f6e\u9002\u5f53\u7684\u7f16\u7801\u58f0\u660e\u3001\u4ee5\u53ca\u786e\u8ba4\u7ec8\u7aef\u652f\u6301\u4e2d\u6587\u5b57\u7b26\uff0c\u662f\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u5173\u952e\u6b65\u9aa4\u3002\u540c\u65f6\uff0c\u5229\u7528Python\u4e30\u5bcc\u7684\u5e93\u548c\u6a21\u5757\u652f\u6301\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55\u4e2d\u6587\u5904\u7406\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u7f16\u7801\u4ee5\u8f93\u51fa\u4e2d\u6587\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8f93\u51fa\u4e2d\u6587\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u901a\u5e38\uff0c\u4f7f\u7528UTF-8\u7f16\u7801\u53ef\u4ee5\u6709\u6548\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002\u5728Python\u6587\u4ef6\u7684\u5f00\u5934\u6dfb\u52a0<code># -*- coding: utf-8 -*-<\/code>\u58f0\u660e\uff0c\u6216\u8005\u5728\u4f7f\u7528print\u51fd\u6570\u65f6\uff0c\u786e\u4fdd\u60a8\u7684\u7ec8\u7aef\u6216IDE\u652f\u6301UTF-8\u7f16\u7801\uff0c\u8fd9\u6837\u53ef\u4ee5\u987a\u5229\u8f93\u51fa\u4e2d\u6587\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\u548c\u5e93\uff0c\u4f8b\u5982<code>str<\/code>\u65b9\u6cd5\u548c<code>re<\/code>\u6a21\u5757\u3002\u5bf9\u4e8e\u9700\u8981\u8fdb\u884c\u4e2d\u6587\u5206\u8bcd\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u501f\u52a9\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>jieba<\/code>\uff0c\u5b83\u80fd\u591f\u5e2e\u52a9\u7528\u6237\u5c06\u4e2d\u6587\u53e5\u5b50\u62c6\u5206\u6210\u8bcd\u8bed\uff0c\u65b9\u4fbf\u8fdb\u884c\u540e\u7eed\u7684\u5206\u6790\u548c\u5904\u7406\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\u52a1\u5fc5\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>encoding=&#39;utf-8&#39;<\/code>\u53c2\u6570\u6765\u786e\u4fdd\u6587\u4ef6\u4ee5UTF-8\u7f16\u7801\u6253\u5f00\u3002\u4f8b\u5982\uff1a<code>with open(&#39;file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u6b63\u786e\u8bfb\u53d6\u548c\u5199\u5165\u4e2d\u6587\u5185\u5bb9\uff0c\u907f\u514d\u6570\u636e\u4e22\u5931\u6216\u4e71\u7801\u7684\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u8f93\u51fa\u4e2d\u6587\uff0c\u53ef\u4ee5\u4f7f\u7528\u9002\u5f53\u7684\u7f16\u7801\u3001\u8bbe\u7f6e\u7f16\u7801\u58f0\u660e\u3001\u4f7f\u7528unicode\u5b57\u7b26\u4e32\u3001\u4ee5\u53ca\u786e\u4fdd\u7ec8\u7aef\u652f\u6301\u4e2d\u6587\u5b57 [&hellip;]","protected":false},"author":3,"featured_media":997985,"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\/997967"}],"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=997967"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/997967\/revisions"}],"predecessor-version":[{"id":997987,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/997967\/revisions\/997987"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/997985"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=997967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=997967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=997967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}