{"id":947693,"date":"2024-12-26T23:53:56","date_gmt":"2024-12-26T15:53:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/947693.html"},"modified":"2024-12-26T23:53:59","modified_gmt":"2024-12-26T15:53:59","slug":"python%e5%a6%82%e4%bd%95%e5%8a%a0%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/947693.html","title":{"rendered":"Python\u5982\u4f55\u52a0\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083346\/a7969b13-a505-4eff-b93c-3e17e79e9deb.webp\" alt=\"Python\u5982\u4f55\u52a0\u4e2d\u6587\" \/><\/p>\n<p><p> <strong>Python\u652f\u6301\u4e2d\u6587\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u5408\u9002\u7684\u5b57\u7b26\u7f16\u7801\u3001\u6307\u5b9aPython\u6587\u4ef6\u7f16\u7801\u4e3aUTF-8\u3001\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e2d\u6587\u5b57\u7b26\u3001\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u53ca\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u65f6\u6ce8\u610f\u7f16\u7801\u95ee\u9898\u3002<\/strong> \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528UTF-8\u7f16\u7801\u662f\u6700\u4e3a\u5e38\u89c1\u548c\u63a8\u8350\u7684\u505a\u6cd5\uff0c\u56e0\u4e3aUTF-8\u80fd\u591f\u652f\u6301\u51e0\u4e4e\u6240\u6709\u7684\u5b57\u7b26\u96c6\u3002\u4e3a\u4e86\u4fdd\u8bc1\u4e2d\u6587\u5b57\u7b26\u7684\u6b63\u786e\u5904\u7406\u548c\u663e\u793a\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e2d\u7684\u5b57\u7b26\u4e32\u7f16\u7801\u4e00\u81f4\u975e\u5e38\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u4e2d\u6587\u7684\u65b9\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5408\u9002\u7684\u5b57\u7b26\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728Python\u4e2d\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\uff0c\u9009\u62e9\u5408\u9002\u7684\u5b57\u7b26\u7f16\u7801\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002UTF-8\u662fPython 3\u7684\u9ed8\u8ba4\u7f16\u7801\uff0c\u53ef\u4ee5\u5f88\u597d\u5730\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u3002\u56e0\u6b64\uff0c\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\uff0c\u901a\u5e38\u4e0d\u9700\u8981\u7279\u522b\u6307\u5b9a\u7f16\u7801\u3002\u4f46\u662f\uff0c\u5982\u679c\u5728Python 2\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\uff0c\u5219\u9700\u8981\u663e\u5f0f\u58f0\u660e\u6587\u4ef6\u7f16\u7801\u3002<\/p>\n<\/p>\n<ol>\n<li>Python 3\u4e2d\u7684\u5b57\u7b26\u7f16\u7801<\/li>\n<\/ol>\n<p><p>Python 3\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u56e0\u6b64\u5728Python 3\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u901a\u5e38\u4e0d\u4f1a\u51fa\u73b0\u95ee\u9898\u3002\u4e3a\u4e86\u5728Python 3\u4e2d\u4f7f\u7528\u4e2d\u6587\u5b57\u7b26\uff0c\u53ea\u9700\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Python 3\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>print(&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>Python 2\u4e2d\u7684\u5b57\u7b26\u7f16\u7801<\/li>\n<\/ol>\n<p><p>\u5728Python 2\u4e2d\uff0c\u9ed8\u8ba4\u7f16\u7801\u662fASCII\uff0c\u8fd9\u4e0d\u80fd\u76f4\u63a5\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u3002\u56e0\u6b64\uff0c\u9700\u8981\u5728\u6587\u4ef6\u5f00\u5934\u6307\u5b9a\u7f16\u7801\u4e3aUTF-8\uff0c\u4ee5\u4fbf\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>print &quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e2d\u6587\u5b57\u7b26<\/p>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u5b57\u7b26\u4e32\u7684\u58f0\u660e\u548c\u4f7f\u7528\u65b9\u5f0f\u6b63\u786e\u3002Python 3\u4e2d\uff0c\u6240\u6709\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662fUnicode\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e2d\u6587\u5b57\u7b26\u3002\u800c\u5728Python 2\u4e2d\uff0c\u9700\u8981\u4f7f\u7528<code>u<\/code>\u524d\u7f00\u6765\u6807\u8bb0Unicode\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<ol>\n<li>Python 3\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u4e32<\/li>\n<\/ol>\n<p><p>\u5728Python 3\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u4e2d\u6587\u5b57\u7b26\uff0c\u65e0\u9700\u4efb\u4f55\u989d\u5916\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Python 3\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>message = &quot;\u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26\u4e32&quot;<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>Python 2\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u4e32<\/li>\n<\/ol>\n<p><p>\u5728Python 2\u4e2d\uff0c\u9700\u8981\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u6765\u5904\u7406\u4e2d\u6587\u5b57\u7b26\uff0c\u4f7f\u7528<code>u<\/code>\u524d\u7f00\u6765\u6807\u8bb0Unicode\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<h2><strong>Python 2\u4ee3\u7801\u793a\u4f8b<\/strong><\/h2>\n<p>message = u&quot;\u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26\u4e32&quot;<\/p>\n<p>print message<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\uff0c\u4ee5\u4fbf\u5728\u4e0d\u540c\u7684\u573a\u5408\u4e0b\u6b63\u786e\u4f7f\u7528\u3002\u5e38\u89c1\u7684\u64cd\u4f5c\u5305\u62ec\u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362\u3001\u5b57\u7b26\u4e32\u957f\u5ea6\u8ba1\u7b97\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362<\/li>\n<\/ol>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>encode()<\/code>\u548c<code>decode()<\/code>\u65b9\u6cd5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u3002\u4f8b\u5982\uff0c\u5c06Unicode\u5b57\u7b26\u4e32\u7f16\u7801\u4e3aUTF-8\u5b57\u8282\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Python 3\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>unicode_str = &quot;\u4e2d\u6587&quot;<\/p>\n<p>utf8_bytes = unicode_str.encode(&#39;utf-8&#39;)<\/p>\n<p>print(utf8_bytes)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u8282\u4e32\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<h2><strong>Python 2\u4ee3\u7801\u793a\u4f8b<\/strong><\/h2>\n<p>utf8_bytes = &quot;\u4e2d\u6587&quot;.decode(&#39;utf-8&#39;)<\/p>\n<p>print utf8_bytes<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5b57\u7b26\u4e32\u957f\u5ea6\u8ba1\u7b97<\/li>\n<\/ol>\n<p><p>\u5728\u8ba1\u7b97\u4e2d\u6587\u5b57\u7b26\u4e32\u957f\u5ea6\u65f6\uff0c\u5e94\u8be5\u6ce8\u610f\u533a\u5206\u5b57\u7b26\u957f\u5ea6\u548c\u5b57\u8282\u957f\u5ea6\u3002\u5728Python\u4e2d\uff0c<code>len()<\/code>\u51fd\u6570\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u6570\uff0c\u800c\u975e\u5b57\u8282\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Python 3\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>unicode_str = &quot;\u4e2d\u6587\u5b57\u7b26&quot;<\/p>\n<p>print(len(unicode_str))  # \u8f93\u51fa\u5b57\u7b26\u6570<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u4e2d\u7684\u4e2d\u6587\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u64cd\u4f5c\u65f6\uff0c\u4e5f\u9700\u8981\u6ce8\u610f\u5b57\u7b26\u7f16\u7801\u95ee\u9898\uff0c\u4ee5\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u80fd\u591f\u6b63\u786e\u8bfb\u5199\u3002<\/p>\n<\/p>\n<ol>\n<li>\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26<\/li>\n<\/ol>\n<p><p>\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\uff0c\u4ee5\u4fbf\u6b63\u786e\u89e3\u7801\u6587\u4ef6\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u8bfb\u53d6\u4e00\u4e2aUTF-8\u7f16\u7801\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Python 3\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>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>\u5728Python 2\u4e2d\uff0c\u9700\u8981\u4f7f\u7528<code>codecs<\/code>\u6a21\u5757\u6765\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>import codecs<\/p>\n<h2><strong>Python 2\u4ee3\u7801\u793a\u4f8b<\/strong><\/h2>\n<p>with codecs.open(&#39;chinese_text.txt&#39;, &#39;r&#39;, &#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<ol start=\"2\">\n<li>\u5199\u5165\u6587\u4ef6\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26<\/li>\n<\/ol>\n<p><p>\u5728\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u540c\u6837\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\uff0c\u4ee5\u4fbf\u6b63\u786e\u5b58\u50a8\u4e2d\u6587\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u5199\u5165\u4e00\u4e2aUTF-8\u7f16\u7801\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Python 3\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>with open(&#39;chinese_output.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(&quot;\u8fd9\u662f\u5199\u5165\u6587\u4ef6\u7684\u4e2d\u6587\u5185\u5bb9&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c\u4f7f\u7528<code>codecs<\/code>\u6a21\u5757\u6765\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p>import codecs<\/p>\n<h2><strong>Python 2\u4ee3\u7801\u793a\u4f8b<\/strong><\/h2>\n<p>with codecs.open(&#39;chinese_output.txt&#39;, &#39;w&#39;, &#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(u&quot;\u8fd9\u662f\u5199\u5165\u6587\u4ef6\u7684\u4e2d\u6587\u5185\u5bb9&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u5982\u7f16\u7801\u9519\u8bef\u3001\u663e\u793a\u4e71\u7801\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u7f16\u7801\u9519\u8bef<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u5728Python\u4e2d\u9047\u5230\u7f16\u7801\u9519\u8bef\uff08\u5982UnicodeDecodeError\u6216UnicodeEncodeError\uff09\uff0c\u901a\u5e38\u662f\u56e0\u4e3a\u5b57\u7b26\u4e32\u7f16\u7801\u4e0d\u4e00\u81f4\u6216\u672a\u6b63\u786e\u6307\u5b9a\u7f16\u7801\u3002\u89e3\u51b3\u65b9\u6848\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u68c0\u67e5\u5b57\u7b26\u4e32\u7684\u7f16\u7801\uff0c\u786e\u4fdd\u4f7f\u7528UTF-8\u6216\u5176\u4ed6\u5408\u9002\u7684\u7f16\u7801\u3002<\/li>\n<li>\u5728\u6587\u4ef6\u64cd\u4f5c\u65f6\u663e\u5f0f\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u3002<\/li>\n<li>\u5728Python 2\u4e2d\u4f7f\u7528<code>u<\/code>\u524d\u7f00\u6765\u6807\u8bb0Unicode\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>\u663e\u793a\u4e71\u7801<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u4e2d\u6587\u5b57\u7b26\u663e\u793a\u4e3a\u4e71\u7801\uff0c\u901a\u5e38\u662f\u7531\u4e8e\u5b57\u7b26\u7f16\u7801\u4e0d\u5339\u914d\u5f15\u8d77\u7684\u3002\u89e3\u51b3\u65b9\u6848\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u6587\u4ef6\u7f16\u7801\u4e0e\u7a0b\u5e8f\u4e2d\u6307\u5b9a\u7684\u7f16\u7801\u4e00\u81f4\u3002<\/li>\n<li>\u68c0\u67e5\u7ec8\u7aef\u6216\u6587\u672c\u7f16\u8f91\u5668\u7684\u5b57\u7b26\u7f16\u7801\u8bbe\u7f6e\uff0c\u786e\u4fdd\u5176\u652f\u6301UTF-8\u6216\u5176\u4ed6\u5408\u9002\u7684\u7f16\u7801\u3002<\/li>\n<\/ul>\n<p><p>\u603b\u4e4b\uff0cPython\u5bf9\u4e2d\u6587\u5b57\u7b26\u7684\u652f\u6301\u4e3b\u8981\u901a\u8fc7\u5408\u9002\u7684\u5b57\u7b26\u7f16\u7801\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u6b63\u786e\u8bbe\u7f6e\u6587\u4ef6\u7f16\u7801\u3001\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u4ee5\u53ca\u5904\u7406\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u4e2d\u7684\u7f16\u7801\u95ee\u9898\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728Python\u4e2d\u6b63\u786e\u5904\u7406\u548c\u663e\u793a\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u9996\u5148\u786e\u4fdd\u4f60\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0 <code># -*- coding: utf-8 -*-<\/code> \u6765\u6307\u5b9a\u7f16\u7801\u3002\u4f7f\u7528 <code>print()<\/code> \u51fd\u6570\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\u4e32\u65f6\uff0c\u786e\u4fdd\u7ec8\u7aef\u6216\u73af\u5883\u652f\u6301UTF-8\u7f16\u7801\uff0c\u4ee5\u4fbf\u6b63\u786e\u663e\u793a\u4e2d\u6587\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\u9700\u8981\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002\u4f7f\u7528 <code>open(&#39;filename.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;)<\/code> \u6765\u8bfb\u53d6\u6587\u4ef6\uff0c\u4f7f\u7528 <code>open(&#39;filename.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;)<\/code> \u6765\u5199\u5165\u6587\u4ef6\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u5728\u6587\u4ef6\u4e2d\u5f97\u5230\u6b63\u786e\u5904\u7406\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u4e2d\u6587\u6587\u672c\uff1f<\/strong><br \/>\u5904\u7406\u4e2d\u6587\u6587\u672c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982 <code>jieba<\/code> \u8fdb\u884c\u4e2d\u6587\u5206\u8bcd\uff0c<code>pandas<\/code> \u5904\u7406\u6570\u636e\u65f6\u652f\u6301\u4e2d\u6587\u5217\u540d\uff0c<code>matplotlib<\/code> \u5728\u56fe\u8868\u4e2d\u663e\u793a\u4e2d\u6587\u6807\u7b7e\u7b49\u3002\u8fd8\u53ef\u4ee5\u5229\u7528 <code>requests<\/code> \u5e93\u83b7\u53d6\u4e2d\u6587\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u901a\u8fc7 <code>BeautifulSoup<\/code> \u89e3\u6790HTML\uff0c\u63d0\u53d6\u4e2d\u6587\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u652f\u6301\u4e2d\u6587\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u5408\u9002\u7684\u5b57\u7b26\u7f16\u7801\u3001\u6307\u5b9aPython\u6587\u4ef6\u7f16\u7801\u4e3aUTF-8\u3001\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e2d\u6587\u5b57 [&hellip;]","protected":false},"author":3,"featured_media":947700,"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\/947693"}],"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=947693"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947693\/revisions"}],"predecessor-version":[{"id":947704,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947693\/revisions\/947704"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/947700"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=947693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=947693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=947693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}