{"id":1011476,"date":"2024-12-27T11:30:12","date_gmt":"2024-12-27T03:30:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1011476.html"},"modified":"2024-12-27T11:30:14","modified_gmt":"2024-12-27T03:30:14","slug":"python-%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e4%b8%ad%e6%96%87%e4%b9%b1%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1011476.html","title":{"rendered":"python \u5982\u4f55\u6dfb\u52a0\u4e2d\u6587\u4e71\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085613\/b956c4a3-534d-4b9b-ae80-9caa71e35654.webp\" alt=\"python \u5982\u4f55\u6dfb\u52a0\u4e2d\u6587\u4e71\u7801\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6dfb\u52a0\u4e2d\u6587\u4e71\u7801\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u7f16\u7801\u3001\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84<\/strong>\u7b49\u65b9\u5f0f\u6765\u907f\u514d\u3002\u8981\u8be6\u7ec6\u4e86\u89e3\u5982\u4f55\u907f\u514d\u4e2d\u6587\u4e71\u7801\u95ee\u9898\uff0c\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u8fdb\u884c\u5206\u6790\u548c\u89e3\u51b3\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6307\u5b9a\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4e2d\u6587\u4e71\u7801\u901a\u5e38\u662f\u7531\u4e8e\u7f16\u7801\u4e0d\u4e00\u81f4\u6216\u672a\u6b63\u786e\u6307\u5b9a\u7f16\u7801\u5bfc\u81f4\u7684\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u7684\u5f00\u5934\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\uff0c\u5982 <code># -*- coding: utf-8 -*-<\/code>\u3002\u8fd9\u5c06\u544a\u8bc9Python\u89e3\u91ca\u5668\u4f7f\u7528UTF-8\u7f16\u7801\u6765\u89e3\u6790\u6587\u4ef6\u5185\u5bb9\uff0c\u4ece\u800c\u6709\u6548\u907f\u514d\u7531\u4e8e\u7f16\u7801\u4e0d\u540c\u6b65\u800c\u4ea7\u751f\u7684\u4e2d\u6587\u4e71\u7801\u95ee\u9898\u3002<\/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>\u6b64\u5916\uff0c\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u4e5f\u9700\u8981\u6307\u5b9a\u7f16\u7801\u3002\u4f8b\u5982\uff0c\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u53ef\u4ee5\u4f7f\u7528 <code>open<\/code> \u51fd\u6570\u7684 <code>encoding<\/code> \u53c2\u6570\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 file:<\/p>\n<p>    content = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u7b26\u96c6<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u7684\u5b57\u7b26\u96c6\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002UTF-8\u662f\u4e00\u79cd\u5e38\u7528\u7684\u5b57\u7b26\u96c6\uff0c\u5b83\u80fd\u591f\u5f88\u597d\u5730\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u3002\u786e\u4fdd\u6587\u4ef6\u672c\u8eab\u4fdd\u5b58\u4e3aUTF-8\u7f16\u7801\uff0c\u5e76\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\u4f7f\u7528UTF-8\u5b57\u7b26\u96c6\u3002<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u6587\u4ef6\u662f\u4ee5\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\u4fdd\u5b58\u7684\uff08\u5982GBK\uff09\uff0c\u5219\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u9700\u8981\u6307\u5b9a\u76f8\u5e94\u7684\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;gbk&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u6587\u4ef6\u8def\u5f84\u53ef\u80fd\u4f1a\u6d89\u53ca\u4e2d\u6587\u5b57\u7b26\uff0c\u8fd9\u4e5f\u53ef\u80fd\u5bfc\u81f4\u4e71\u7801\u95ee\u9898\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\u6216\u539f\u59cb\u5b57\u7b26\u4e32\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Unicode\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">path = u&quot;C:\\\\\u8def\u5f84\\\\\u5230\\\\\u6587\u4ef6.txt&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">path = r&quot;C:\\\u8def\u5f84\\\u5230\\\u6587\u4ef6.txt&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5b57\u7b26\u4e32\u7f16\u7801\u4e0e\u89e3\u7801<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u6709\u65f6\u9700\u8981\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3002Python\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\uff0c\u4ee5\u9632\u6b62\u4e71\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>encode<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282\u5bf9\u8c61\uff0c\u4f7f\u7528 <code>decode<\/code> \u65b9\u6cd5\u5c06\u5b57\u8282\u5bf9\u8c61\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7f16\u7801\u5b57\u7b26\u4e32\u4e3a\u5b57\u8282\u5bf9\u8c61<\/p>\n<p>byte_string = &quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;.encode(&#39;utf-8&#39;)<\/p>\n<h2><strong>\u89e3\u7801\u5b57\u8282\u5bf9\u8c61\u4e3a\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><p>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7b80\u5316\u7f16\u7801\u95ee\u9898\u3002\u4f8b\u5982\uff0c<code>chardet<\/code> \u662f\u4e00\u4e2a\u53ef\u4ee5\u81ea\u52a8\u68c0\u6d4b\u6587\u4ef6\u7f16\u7801\u7684\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u6587\u4ef6\u7684\u5b9e\u9645\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import chardet<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    raw_data = file.read()<\/p>\n<p>    result = chardet.detect(raw_data)<\/p>\n<p>    encoding = result[&#39;encoding&#39;]<\/p>\n<p>    # \u4f7f\u7528\u68c0\u6d4b\u5230\u7684\u7f16\u7801\u683c\u5f0f\u8bfb\u53d6\u6587\u4ef6<\/p>\n<p>    content = raw_data.decode(encoding)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u7f51\u7edc\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f51\u7edc\u6570\u636e\u65f6\uff0c\u4f8b\u5982\u901a\u8fc7HTTP\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u4e5f\u53ef\u80fd\u9047\u5230\u7f16\u7801\u95ee\u9898\u3002\u901a\u5e38\uff0cHTTP\u54cd\u5e94\u5934\u4f1a\u5305\u542b\u7f16\u7801\u4fe1\u606f\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>requests<\/code> \u5e93\u83b7\u53d6\u5e76\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u89e3\u7801\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p>response.encoding = response.apparent_encoding  # \u4f7f\u7528\u68c0\u6d4b\u5230\u7684\u7f16\u7801<\/p>\n<p>content = response.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\u7684\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u5728\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u65f6\uff0c\u786e\u4fdd\u6570\u636e\u5e93\u8fde\u63a5\u7684\u7f16\u7801\u914d\u7f6e\u4e0ePython\u4ee3\u7801\u4e2d\u7684\u7f16\u7801\u4e00\u81f4\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528MySQL\u65f6\uff0c\u53ef\u4ee5\u5728\u8fde\u63a5\u65f6\u6307\u5b9a\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>connection = pymysql.connect(host=&#39;localhost&#39;,<\/p>\n<p>                             user=&#39;user&#39;,<\/p>\n<p>                             password=&#39;passwd&#39;,<\/p>\n<p>                             db=&#39;database&#39;,<\/p>\n<p>                             charset=&#39;utf8mb4&#39;)  # \u6307\u5b9aUTF-8\u7f16\u7801<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u5904\u7406Python\u63a7\u5236\u53f0\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cPython\u63a7\u5236\u53f0\u6216\u7ec8\u7aef\u53ef\u80fd\u4e0d\u652f\u6301UTF-8\u7f16\u7801\uff0c\u5bfc\u81f4\u8f93\u51fa\u4e2d\u6587\u65f6\u51fa\u73b0\u4e71\u7801\u3002\u53ef\u4ee5\u5c1d\u8bd5\u4fee\u6539\u7ec8\u7aef\u7684\u7f16\u7801\u8bbe\u7f6e\u4e3aUTF-8\uff0c\u6216\u8005\u4f7f\u7528IDE\u6765\u8fd0\u884cPython\u4ee3\u7801\uff0c\u8fd9\u6837\u901a\u5e38\u53ef\u4ee5\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u907f\u514d\u4e2d\u6587\u4e71\u7801\uff0c\u5173\u952e\u5728\u4e8e<strong>\u4e00\u81f4\u7684\u7f16\u7801\u5904\u7406<\/strong>\uff0c\u65e0\u8bba\u662f\u5728\u4ee3\u7801\u4e2d\u6307\u5b9a\u7f16\u7801\u3001\u6587\u4ef6\u8bfb\u5199\u3001\u6570\u636e\u5e93\u4ea4\u4e92\u8fd8\u662f\u7f51\u7edc\u6570\u636e\u5904\u7406\uff0c\u90fd\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u4e00\u81f4\u7684\u7f16\u7801\u683c\u5f0f\u3002\u901a\u8fc7\u4e86\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u548c\u89e3\u51b3Python\u7a0b\u5e8f\u4e2d\u7684\u4e2d\u6587\u4e71\u7801\u95ee\u9898\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\u4e71\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4e2d\u6587\u4e71\u7801\u901a\u5e38\u662f\u7531\u4e8e\u5b57\u7b26\u7f16\u7801\u4e0d\u5339\u914d\u9020\u6210\u7684\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528UTF-8\u7f16\u7801\u6765\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\uff0c\u786e\u4fdd\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u6dfb\u52a0<code>encoding=&#39;utf-8&#39;<\/code>\u53c2\u6570\uff0c\u4ece\u800c\u907f\u514d\u4e71\u7801\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5728Python\u7a0b\u5e8f\u4e2d\u5982\u4f55\u786e\u4fdd\u8f93\u51fa\u7684\u4e2d\u6587\u5b57\u7b26\u6b63\u5e38\u663e\u793a\uff1f<\/strong><br \/>\u8981\u786e\u4fdd\u5728Python\u7a0b\u5e8f\u4e2d\u8f93\u51fa\u7684\u4e2d\u6587\u5b57\u7b26\u6b63\u5e38\u663e\u793a\uff0c\u5efa\u8bae\u5728\u4ee3\u7801\u6587\u4ef6\u7684\u5f00\u5934\u6dfb\u52a0<code># -*- coding: utf-8 -*-<\/code>\u6765\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u3002\u540c\u65f6\uff0c\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u4e2d\u6587\u65f6\uff0c\u786e\u4fdd\u7ec8\u7aef\u6216\u63a7\u5236\u53f0\u652f\u6301UTF-8\u7f16\u7801\uff0c\u8fd9\u6837\u5c31\u80fd\u6b63\u786e\u663e\u793a\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u9047\u5230\u4e2d\u6587\u4e71\u7801\uff0c\u8be5\u5982\u4f55\u8c03\u8bd5\u548c\u6392\u67e5\u95ee\u9898\uff1f<\/strong><br \/>\u8c03\u8bd5\u4e2d\u6587\u4e71\u7801\u95ee\u9898\u65f6\uff0c\u53ef\u4ee5\u9010\u6b65\u68c0\u67e5\u6570\u636e\u7684\u8f93\u5165\u548c\u8f93\u51fa\u73af\u8282\u3002\u9996\u5148\uff0c\u786e\u8ba4\u8bfb\u53d6\u7684\u6570\u636e\u6e90\uff08\u5982\u6587\u4ef6\u3001\u6570\u636e\u5e93\u7b49\uff09\u4f7f\u7528\u7684\u7f16\u7801\u683c\u5f0f\u3002\u63a5\u7740\uff0c\u68c0\u67e5\u5728\u7a0b\u5e8f\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u65f6\u662f\u5426\u8fdb\u884c\u4e86\u7f16\u7801\u8f6c\u6362\u3002\u6700\u540e\uff0c\u67e5\u770b\u8f93\u51fa\u65f6\u7684\u7ec8\u7aef\u8bbe\u7f6e\uff0c\u786e\u4fdd\u5176\u652f\u6301UTF-8\u6216\u76f8\u5e94\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6dfb\u52a0\u4e2d\u6587\u4e71\u7801\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u7f16\u7801\u3001\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84\u7b49\u65b9\u5f0f\u6765\u907f\u514d\u3002\u8981\u8be6\u7ec6\u4e86\u89e3\u5982\u4f55\u907f\u514d\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1011479,"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\/1011476"}],"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=1011476"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011476\/revisions"}],"predecessor-version":[{"id":1011484,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011476\/revisions\/1011484"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1011479"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1011476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1011476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1011476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}