{"id":1172283,"date":"2025-01-15T16:47:15","date_gmt":"2025-01-15T08:47:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172283.html"},"modified":"2025-01-15T16:47:18","modified_gmt":"2025-01-15T08:47:18","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e4%b8%ad%e6%96%87%e4%b9%b1%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1172283.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8f93\u51fa\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\/26074414\/55eac841-d590-4601-916f-b7061f4b0fe8.webp\" alt=\"python\u4e2d\u5982\u4f55\u8f93\u51fa\u4e2d\u6587\u4e71\u7801\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u8f93\u51fa\u4e2d\u6587\u4e71\u7801\u901a\u5e38\u662f\u7531\u4e8e\u5b57\u7b26\u7f16\u7801\u6216\u89e3\u7801\u4e0d\u6b63\u786e\u5bfc\u81f4\u7684\u3002<strong>\u8981\u907f\u514d\u4e2d\u6587\u4e71\u7801\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7\u786e\u4fdd\u6587\u4ef6\u7f16\u7801\u4e3aUTF-8\u3001\u6b63\u786e\u8bbe\u7f6ePython\u7684\u7f16\u7801\u3001\u4f7f\u7528\u6b63\u786e\u7684\u7ec8\u7aef\u7f16\u7801<\/strong>\u7b49\u63aa\u65bd\u3002\u63a5\u4e0b\u6765\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u70b9\uff1a<strong>\u786e\u4fdd\u6587\u4ef6\u7f16\u7801\u4e3aUTF-8<\/strong>\u3002\u5728\u4fdd\u5b58Python\u6587\u4ef6\u65f6\uff0c\u9009\u62e9UTF-8\u7f16\u7801\u683c\u5f0f\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u88ab\u6b63\u786e\u8bc6\u522b\u548c\u5904\u7406\u3002\u5177\u4f53\u64cd\u4f5c\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5728\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u7f16\u8f91Python\u6587\u4ef6\u65f6\uff0c\u9009\u62e9\u4fdd\u5b58\u4e3aUTF-8\u7f16\u7801\u3002<\/li>\n<li>\u5728Python\u811a\u672c\u7684\u5f00\u5934\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\uff0c\u4f8b\u5982 <code># -*- coding: utf-8 -*-<\/code>\uff0c\u786e\u4fdd\u89e3\u91ca\u5668\u77e5\u9053\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/li>\n<\/ol>\n<p><h3>\u6587\u4ef6\u7f16\u7801\u4e0e\u7f16\u7801\u58f0\u660e<\/h3>\n<\/p>\n<p><h4>UTF-8\u7f16\u7801<\/h4>\n<\/p>\n<p><p>UTF-8\u662f\u4e00\u79cd\u53ef\u53d8\u957f\u5ea6\u5b57\u7b26\u7f16\u7801\uff0c\u53ef\u4ee5\u517c\u5bb9ASCII\u5b57\u7b26\u96c6\uff0c\u5e76\u4e14\u53ef\u4ee5\u8868\u793a\u51e0\u4e4e\u6240\u6709\u73b0\u5b58\u7684\u4e66\u5199\u7cfb\u7edf\u5b57\u7b26\u3002\u4f7f\u7528UTF-8\u7f16\u7801\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u88ab\u6b63\u786e\u8bc6\u522b\u548c\u5904\u7406\u3002\u5927\u591a\u6570\u73b0\u4ee3\u7f16\u8f91\u5668\u548cIDE\u90fd\u652f\u6301UTF-8\u7f16\u7801\uff0c\u5728\u4fdd\u5b58\u6587\u4ef6\u65f6\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528UTF-8\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u7801\u58f0\u660e<\/h4>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u6587\u4ef6\u5934\u90e8\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\u6765\u6307\u660e\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002\u901a\u5e38\uff0c\u7f16\u7801\u58f0\u660e\u7684\u683c\u5f0f\u5982\u4e0b\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\u6587\u4ef6\u4f7f\u7528\u7684\u662fUTF-8\u7f16\u7801\u3002\u786e\u4fdd\u5728\u6587\u4ef6\u4e2d\u5305\u542b\u8fd9\u4e00\u58f0\u660e\uff0c\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>Python\u5b57\u7b26\u4e32\u7f16\u7801\u4e0e\u89e3\u7801<\/h3>\n<\/p>\n<p><p>\u5728Python 3\u4e2d\uff0c\u5b57\u7b26\u4e32\u662f\u4ee5Unicode\u5f62\u5f0f\u5b58\u50a8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u4e2d\u6587\u5b57\u7b26\u800c\u4e0d\u4f1a\u51fa\u73b0\u4e71\u7801\u3002\u7136\u800c\uff0c\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u4ecd\u7136\u9700\u8981\u6ce8\u610f\u7f16\u7801\u548c\u89e3\u7801\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u7801\u4e0e\u89e3\u7801<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>encode<\/code>\u548c<code>decode<\/code>\u65b9\u6cd5\u8fdb\u884c\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u548c\u89e3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7f16\u7801\u4e3aUTF-8\u5b57\u8282\u5e8f\u5217<\/p>\n<p>s = &#39;\u4e2d\u6587&#39;<\/p>\n<p>s_utf8 = s.encode(&#39;utf-8&#39;)<\/p>\n<h2><strong>\u89e3\u7801\u4e3aUnicode\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>s_unicode = s_utf8.decode(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u5b57\u7b26\u4e32\u5728\u4e0d\u540c\u7f16\u7801\u4e4b\u95f4\u8f6c\u6362\u65f6\u4e0d\u4f1a\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u63a7\u5236\u53f0\u4e0e\u7ec8\u7aef\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>\u5728\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u7ec8\u7aef\u6216\u63a7\u5236\u53f0\u7684\u7f16\u7801\u8bbe\u7f6e\u4e5f\u53ef\u80fd\u4f1a\u5f71\u54cd\u8f93\u51fa\u7ed3\u679c\u3002\u786e\u4fdd\u7ec8\u7aef\u6216\u63a7\u5236\u53f0\u7684\u7f16\u7801\u4e0ePython\u811a\u672c\u4f7f\u7528\u7684\u7f16\u7801\u4e00\u81f4\uff0c\u53ef\u4ee5\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>Windows\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5728Windows\u73af\u5883\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>chcp<\/code>\u547d\u4ee4\u67e5\u770b\u548c\u8bbe\u7f6e\u63a7\u5236\u53f0\u7684\u4ee3\u7801\u9875\u3002\u4f8b\u5982\uff0c\u8bbe\u7f6e\u63a7\u5236\u53f0\u4e3aUTF-8\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">chcp 65001<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>Linux\u548cMac\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5728Linux\u548cMac\u73af\u5883\u4e0b\uff0c\u7ec8\u7aef\u901a\u5e38\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u548c\u8bbe\u7f6e<code>LANG<\/code>\u548c<code>LC_ALL<\/code>\u73af\u5883\u53d8\u91cf\u6765\u786e\u4fdd\u7f16\u7801\u4e00\u81f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">export LANG=en_US.UTF-8<\/p>\n<p>export LC_ALL=en_US.UTF-8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u65f6\uff0c\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u4e5f\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u7684<code>encoding<\/code>\u53c2\u6570\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6587\u4ef6<\/p>\n<p>with open(&#39;file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:<\/p>\n<p>    content = f.read()<\/p>\n<h2><strong>\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;file.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as f:<\/p>\n<p>    f.write(&#39;\u4e2d\u6587\u5185\u5bb9&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\uff0c\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u8bfb\u5199\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u5904\u7406\u7f51\u7edc\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f51\u7edc\u6570\u636e\u65f6\uff0c\u786e\u4fdd\u6570\u636e\u7684\u7f16\u7801\u683c\u5f0f\u4e0e\u63a5\u6536\u65b9\u4e00\u81f4\u4e5f\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u53d1\u9001HTTP\u8bf7\u6c42\u65f6\uff0c\u53ef\u4ee5\u5728\u8bf7\u6c42\u5934\u4e2d\u6307\u5b9a\u5b57\u7b26\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>headers = {<\/p>\n<p>    &#39;Content-Type&#39;: &#39;text\/html; charset=utf-8&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.get(&#39;https:\/\/example.com&#39;, headers=headers)<\/p>\n<p>response.encoding = &#39;utf-8&#39;<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a\u8bf7\u6c42\u5934\u4e2d\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u53ef\u4ee5\u786e\u4fdd\u63a5\u6536\u5230\u7684\u6570\u636e\u88ab\u6b63\u786e\u89e3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u6570\u636e\u5e93\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u786e\u4fdd\u6570\u636e\u5e93\u8fde\u63a5\u548c\u67e5\u8be2\u4f7f\u7528\u7684\u7f16\u7801\u683c\u5f0f\u4e0e\u6570\u636e\u5e93\u914d\u7f6e\u4e00\u81f4\uff0c\u53ef\u4ee5\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528MySQL\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u5728\u8fde\u63a5\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u5b57\u7b26\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;database&#39;,<\/p>\n<p>    charset=&#39;utf8mb4&#39;<\/p>\n<p>)<\/p>\n<p>with connection.cursor() as cursor:<\/p>\n<p>    cursor.execute(&#39;SELECT * FROM table&#39;)<\/p>\n<p>    result = cursor.fetchall()<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a\u8fde\u63a5\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5e93\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<\/p>\n<p><h3>Web\u5f00\u53d1\u4e2d\u7684\u7f16\u7801\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884cWeb\u5f00\u53d1\u65f6\uff0c\u786e\u4fdd\u7f51\u9875\u548c\u670d\u52a1\u5668\u7aef\u4ee3\u7801\u4f7f\u7528\u4e00\u81f4\u7684\u7f16\u7801\u683c\u5f0f\u4e5f\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728HTML\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>meta<\/code>\u6807\u7b7e\u6307\u5b9a\u5b57\u7b26\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html lang=&quot;en&quot;&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;title&gt;Document&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;p&gt;\u4e2d\u6587\u5185\u5bb9&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u65f6\uff0c\u5728\u670d\u52a1\u5668\u7aef\u4ee3\u7801\u4e2d\uff0c\u786e\u4fdd\u54cd\u5e94\u5934\u4e2d\u7684\u5b57\u7b26\u7f16\u7801\u4e0e\u7f51\u9875\u4e00\u81f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, Response<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    response = Response(&#39;&lt;p&gt;\u4e2d\u6587\u5185\u5bb9&lt;\/p&gt;&#39;)<\/p>\n<p>    response.headers[&#39;Content-Type&#39;] = &#39;text\/html; charset=utf-8&#39;<\/p>\n<p>    return response<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    app.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u786e\u4fdd\u7f51\u9875\u548c\u670d\u52a1\u5668\u7aef\u4ee3\u7801\u4f7f\u7528\u4e00\u81f4\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u53ef\u4ee5\u907f\u514dWeb\u5e94\u7528\u4e2d\u7684\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u4e5f\u9700\u8981\u6ce8\u610f\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528Pandas\u5e93\u5904\u7406\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u6765\u907f\u514d\u4e71\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;, encoding=&#39;utf-8&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684\u6570\u636e\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;data_output.csv&#39;, encoding=&#39;utf-8&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u8bfb\u5199\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5904\u7406\u4e2d\u6587<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u6a21\u5f0f\u5b57\u7b26\u4e32\u548c\u5f85\u5339\u914d\u5b57\u7b26\u4e32\u4f7f\u7528\u4e00\u81f4\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u5339\u914d\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Unicode\u5b57\u7b26\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = re.compile(r&#39;[\\u4e00-\\u9fa5]+&#39;)<\/p>\n<p>text = &#39;\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684\u5b57\u7b26\u4e32&#39;<\/p>\n<p>matches = pattern.findall(text)<\/p>\n<p>print(matches)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528Unicode\u5b57\u7b26\u8303\u56f4\uff0c\u53ef\u4ee5\u786e\u4fdd\u6b63\u5219\u8868\u8fbe\u5f0f\u6b63\u786e\u5339\u914d\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u786e\u4fdd\u6587\u4ef6\u7f16\u7801\u4e3aUTF-8\u3001\u6b63\u786e\u8bbe\u7f6ePython\u7684\u7f16\u7801\u3001\u4f7f\u7528\u6b63\u786e\u7684\u7ec8\u7aef\u7f16\u7801\u7b49\u63aa\u65bd\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514dPython\u4e2d\u8f93\u51fa\u4e2d\u6587\u4e71\u7801\u7684\u95ee\u9898\u3002\u540c\u65f6\uff0c\u5728\u8fdb\u884c\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u3001\u5904\u7406\u7f51\u7edc\u6570\u636e\u3001\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3001\u8fdb\u884cWeb\u5f00\u53d1\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u6ce8\u610f\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u8fc7\u8fd9\u4e9b\u63aa\u65bd\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728Python\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u65f6\u4e0d\u4f1a\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u5e38\u89c1\u95ee\u9898\u89e3\u51b3<\/h3>\n<\/p>\n<p><h4>\u95ee\u9898\u4e00\uff1a\u63a7\u5236\u53f0\u8f93\u51fa\u4e2d\u6587\u4e71\u7801<\/h4>\n<\/p>\n<p><p>\u5728Windows\u73af\u5883\u4e0b\uff0c\u9ed8\u8ba4\u7684\u63a7\u5236\u53f0\u7f16\u7801\u53ef\u80fd\u4e0d\u662fUTF-8\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4e2d\u6587\u8f93\u51fa\u4e71\u7801\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u63a7\u5236\u53f0\u7684\u4ee3\u7801\u9875\u4e3a65001\uff08UTF-8\uff09\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">chcp 65001<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Linux\u548cMac\u73af\u5883\u4e0b\uff0c\u901a\u5e38\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u548c\u8bbe\u7f6e<code>LANG<\/code>\u548c<code>LC_ALL<\/code>\u73af\u5883\u53d8\u91cf\u6765\u786e\u4fdd\u7f16\u7801\u4e00\u81f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">export LANG=en_US.UTF-8<\/p>\n<p>export LC_ALL=en_US.UTF-8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u95ee\u9898\u4e8c\uff1a\u8bfb\u53d6\u6587\u4ef6\u65f6\u51fa\u73b0\u4e71\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u5982\u679c\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e71\u7801\u95ee\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:<\/p>\n<p>    content = f.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u786e\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>chardet<\/code>\u5e93\u6765\u81ea\u52a8\u68c0\u6d4b\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import chardet<\/p>\n<p>with open(&#39;file.txt&#39;, &#39;rb&#39;) as f:<\/p>\n<p>    raw_data = f.read()<\/p>\n<p>    result = chardet.detect(raw_data)<\/p>\n<p>    encoding = result[&#39;encoding&#39;]<\/p>\n<p>with open(&#39;file.txt&#39;, &#39;r&#39;, encoding=encoding) as f:<\/p>\n<p>    content = f.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u95ee\u9898\u4e09\uff1a\u5199\u5165\u6587\u4ef6\u65f6\u51fa\u73b0\u4e71\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e71\u7801\u95ee\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as f:<\/p>\n<p>    f.write(&#39;\u4e2d\u6587\u5185\u5bb9&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u95ee\u9898\u56db\uff1a\u5904\u7406\u7f51\u7edc\u6570\u636e\u65f6\u51fa\u73b0\u4e71\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f51\u7edc\u6570\u636e\u65f6\uff0c\u5982\u679c\u6570\u636e\u7684\u7f16\u7801\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e71\u7801\u95ee\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u8bf7\u6c42\u5934\u4e2d\u6307\u5b9a\u5b57\u7b26\u7f16\u7801\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>headers = {<\/p>\n<p>    &#39;Content-Type&#39;: &#39;text\/html; charset=utf-8&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.get(&#39;https:\/\/example.com&#39;, headers=headers)<\/p>\n<p>response.encoding = &#39;utf-8&#39;<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u95ee\u9898\u4e94\uff1a\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\u51fa\u73b0\u4e71\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u5982\u679c\u6570\u636e\u5e93\u8fde\u63a5\u548c\u67e5\u8be2\u4f7f\u7528\u7684\u7f16\u7801\u683c\u5f0f\u4e0d\u4e00\u81f4\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e71\u7801\u95ee\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u8fde\u63a5\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u5b57\u7b26\u7f16\u7801\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\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;database&#39;,<\/p>\n<p>    charset=&#39;utf8mb4&#39;<\/p>\n<p>)<\/p>\n<p>with connection.cursor() as cursor:<\/p>\n<p>    cursor.execute(&#39;SELECT * FROM table&#39;)<\/p>\n<p>    result = cursor.fetchall()<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5b9e\u8df5\u793a\u4f8b<\/h3>\n<\/p>\n<p><h4>\u793a\u4f8b\u4e00\uff1a\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6587\u4ef6<\/p>\n<p>with open(&#39;file.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<h2><strong>\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;file_output.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\u5b57\u7b26\u7684\u6587\u4ef6\u5185\u5bb9&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u793a\u4f8b\u4e8c\uff1a\u7f51\u7edc\u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>headers = {<\/p>\n<p>    &#39;Content-Type&#39;: &#39;text\/html; charset=utf-8&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.get(&#39;https:\/\/example.com&#39;, headers=headers)<\/p>\n<p>response.encoding = &#39;utf-8&#39;<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u793a\u4f8b\u4e09\uff1a\u6570\u636e\u5e93\u64cd\u4f5c<\/h4>\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;database&#39;,<\/p>\n<p>    charset=&#39;utf8mb4&#39;<\/p>\n<p>)<\/p>\n<p>with connection.cursor() as cursor:<\/p>\n<p>    cursor.execute(&#39;SELECT * FROM table&#39;)<\/p>\n<p>    result = cursor.fetchall()<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u793a\u4f8b\u56db\uff1aWeb\u5f00\u53d1<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, Response<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    response = Response(&#39;&lt;p&gt;\u4e2d\u6587\u5185\u5bb9&lt;\/p&gt;&#39;)<\/p>\n<p>    response.headers[&#39;Content-Type&#39;] = &#39;text\/html; charset=utf-8&#39;<\/p>\n<p>    return response<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u53ef\u4ee5\u770b\u5230\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u5982\u4f55\u901a\u8fc7\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u6765\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002\u786e\u4fdd\u6587\u4ef6\u7f16\u7801\u4e3aUTF-8\u3001\u6b63\u786e\u8bbe\u7f6ePython\u7684\u7f16\u7801\u3001\u4f7f\u7528\u6b63\u786e\u7684\u7ec8\u7aef\u7f16\u7801\u7b49\u63aa\u65bd\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514dPython\u4e2d\u8f93\u51fa\u4e2d\u6587\u4e71\u7801\u7684\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\u7f16\u7801\u4e0d\u5339\u914d\u6240\u5f15\u8d77\u7684\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u5efa\u8bae\u5728\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u65f6\u660e\u786e\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u4f8b\u5982\u4f7f\u7528UTF-8\u7f16\u7801\u3002\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u7684<code>encoding<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u4ece\u800c\u907f\u514d\u4e71\u7801\u3002<\/p>\n<p><strong>\u5728\u7ec8\u7aef\u6216\u63a7\u5236\u53f0\u4e2d\u5982\u4f55\u6b63\u786e\u663e\u793a\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u786e\u4fdd\u4f60\u7684\u7ec8\u7aef\u6216\u63a7\u5236\u53f0\u652f\u6301UTF-8\u7f16\u7801\u662f\u5173\u952e\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6216\u8c03\u6574\u7ec8\u7aef\u8bbe\u7f6e\u6765\u5b9e\u73b0\u3002\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>chcp 65001<\/code>\u547d\u4ee4\u5207\u6362\u5230UTF-8\u7f16\u7801\uff0c\u800c\u5728Linux\u548cMac\u7cfb\u7edf\u4e2d\uff0c\u901a\u5e38\u9ed8\u8ba4\u652f\u6301UTF-8\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u7f51\u9875\u4e2d\u4f7f\u7528Python\u8f93\u51fa\u4e2d\u6587\u800c\u4e0d\u51fa\u73b0\u4e71\u7801\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u751f\u6210\u7f51\u9875\u5185\u5bb9\u65f6\uff0c\u786e\u4fddHTTP\u54cd\u5e94\u5934\u4e2d\u8bbe\u7f6e\u4e86\u6b63\u786e\u7684Content-Type\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Flask\u6846\u67b6\u65f6\uff0c\u8bbe\u7f6e\u54cd\u5e94\u5934\u4e3a<code>Content-Type: text\/html; charset=utf-8<\/code>\u3002\u540c\u65f6\uff0c\u786e\u4fddHTML\u6587\u6863\u4e2d\u6307\u5b9a\u4e86UTF-8\u7f16\u7801\uff0c\u901a\u8fc7<code>&lt;meta charset=&quot;UTF-8&quot;&gt;<\/code>\u6807\u7b7e\u6765\u4fdd\u8bc1\u4e2d\u6587\u5b57\u7b26\u7684\u6b63\u786e\u663e\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8f93\u51fa\u4e2d\u6587\u4e71\u7801\u901a\u5e38\u662f\u7531\u4e8e\u5b57\u7b26\u7f16\u7801\u6216\u89e3\u7801\u4e0d\u6b63\u786e\u5bfc\u81f4\u7684\u3002\u8981\u907f\u514d\u4e2d\u6587\u4e71\u7801\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7\u786e\u4fdd\u6587\u4ef6\u7f16\u7801\u4e3a [&hellip;]","protected":false},"author":3,"featured_media":1172289,"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\/1172283"}],"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=1172283"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172283\/revisions"}],"predecessor-version":[{"id":1172290,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172283\/revisions\/1172290"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1172289"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1172283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1172283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1172283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}