{"id":1167016,"date":"2025-01-15T15:38:46","date_gmt":"2025-01-15T07:38:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167016.html"},"modified":"2025-01-15T15:38:49","modified_gmt":"2025-01-15T07:38:49","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e8%a7%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1167016.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8fdb\u884c\u89e3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25211010\/1abecfef-4cf4-459a-879a-78edbc4bd403.webp\" alt=\"python\u4e2d\u5982\u4f55\u8fdb\u884c\u89e3\u7801\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8fdb\u884c\u89e3\u7801\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u4f7f\u7528base64\u6a21\u5757\u3001\u4f7f\u7528codecs\u6a21\u5757\u3001\u5904\u7406\u6587\u4ef6\u7f16\u7801<\/strong>\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u5b83\u4eec\u7684\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u6709\u4e00\u4e2a\u65b9\u6cd5<code>decode()<\/code>\uff0c\u53ef\u4ee5\u7528\u6765\u89e3\u7801\u5b57\u8282\u5bf9\u8c61\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e3b\u8981\u7528\u4e8e\u5c06\u5b57\u8282\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5bf9\u8c61\u3002\u5e38\u7528\u7684\u7f16\u7801\u683c\u5f0f\u6709UTF-8\u3001UTF-16\u3001ASCII\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>byte_data = b&#39;Hello, World!&#39;<\/p>\n<p>str_data = byte_data.decode(&#39;utf-8&#39;)<\/p>\n<p>print(str_data)  # \u8f93\u51fa\uff1aHello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p><strong>\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5<\/strong>\u662f\u6700\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u4e00\u79cd\u89e3\u7801\u65b9\u5f0f\u3002\u5b83\u4e3b\u8981\u7528\u4e8e\u5c06\u5b57\u8282\u5bf9\u8c61\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u7f16\u7801\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7f16\u7801\u683c\u5f0f\u53ca\u5176\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>UTF-8<\/strong>\uff1a\u6700\u5e38\u7528\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u80fd\u591f\u7f16\u7801\u6240\u6709\u7684Unicode\u5b57\u7b26\uff0c\u5e7f\u6cdb\u7528\u4e8e\u4e92\u8054\u7f51\u548c\u6587\u4ef6\u5b58\u50a8\u3002<\/li>\n<li><strong>UTF-16<\/strong>\uff1a\u6bd4UTF-8\u66f4\u4e3a\u5197\u957f\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u8868\u793a\u66f4\u591a\u5b57\u7b26\u96c6\u7684\u573a\u666f\u3002<\/li>\n<li><strong>ASCII<\/strong>\uff1a\u6700\u65e9\u7684\u7f16\u7801\u6807\u51c6\u4e4b\u4e00\uff0c\u4ec5\u5305\u542b128\u4e2a\u5b57\u7b26\uff0c\u9002\u7528\u4e8e\u82f1\u6587\u6587\u672c\u3002<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u4e14\u7f16\u7801\u683c\u5f0f\u5df2\u77e5\u7684\u6570\u636e\u3002\u4e0d\u8fc7\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u7f16\u7801\u8f6c\u6362\u548c\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u66f4\u4e13\u4e1a\u7684\u6a21\u5757\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528base64\u6a21\u5757<\/h3>\n<\/p>\n<p><p>base64\u662f\u4e00\u79cd\u5e38\u7528\u4e8e\u4f20\u8f93\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u901a\u8fc7URL\u3001HTTP\u3001\u7535\u5b50\u90ae\u4ef6\u7b49\u4f20\u8f93\u65f6\u3002Python\u5185\u7f6e\u7684<code>base64<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884cbase64\u7f16\u7801\u548c\u89e3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import base64<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>encoded_data = b&#39;SGVsbG8sIFdvcmxkIQ==&#39;<\/p>\n<p>decoded_data = base64.b64decode(encoded_data)<\/p>\n<p>print(decoded_data)  # \u8f93\u51fa\uff1ab&#39;Hello, World!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><strong>base64\u6a21\u5757<\/strong>\u975e\u5e38\u9002\u5408\u5904\u7406\u9700\u8981\u5728\u6587\u672c\u73af\u5883\u4e2d\u4f20\u8f93\u7684\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u6bd4\u5982\u56fe\u7247\u3001\u6587\u4ef6\u7b49\u3002\u5b83\u80fd\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a\u6587\u672c\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u907f\u514d\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u4e71\u7801\u6216\u6570\u636e\u4e22\u5931\u7684\u95ee\u9898\u3002\u5728\u7f51\u7edc\u901a\u4fe1\u3001\u7535\u5b50\u90ae\u4ef6\u9644\u4ef6\u7b49\u573a\u666f\u4e2d\uff0cbase64\u7f16\u7801\u548c\u89e3\u7801\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528codecs\u6a21\u5757<\/h3>\n<\/p>\n<p><p>codecs\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5b9e\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u7f16\u7801\u683c\u5f0f\u7684\u6587\u4ef6\u548c\u6570\u636e\u6d41\u3002\u5b83\u652f\u6301\u591a\u79cd\u7f16\u7801\u683c\u5f0f\uff0c\u5e76\u4e14\u80fd\u591f\u8fdb\u884c\u66f4\u590d\u6742\u7684\u7f16\u7801\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import codecs<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>byte_data = b&#39;\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd&#39;<\/p>\n<p>str_data = codecs.decode(byte_data, &#39;utf-8&#39;)<\/p>\n<p>print(str_data)  # \u8f93\u51fa\uff1a\u4f60\u597d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p><strong>codecs\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7f16\u7801\u548c\u89e3\u7801\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5904\u7406\u590d\u6742\u7684\u7f16\u7801\u8f6c\u6362\u9700\u6c42\u3002\u5b83\u4e0d\u4ec5\u652f\u6301\u5e38\u89c1\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u8fd8\u652f\u6301\u4e00\u4e9b\u7279\u6b8a\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u6bd4\u5982UTF-7\u3001ISO-8859-1\u7b49\u3002\u6b64\u5916\uff0ccodecs\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u6587\u4ef6\u8bfb\u5199\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u5e26\u6709\u7279\u5b9a\u7f16\u7801\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528codecs\u6a21\u5757\u7684\u4f18\u70b9\u662f\u529f\u80fd\u5f3a\u5927\uff0c\u80fd\u591f\u5904\u7406\u5404\u79cd\u590d\u6742\u7684\u7f16\u7801\u8f6c\u6362\u9700\u6c42\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u5176\u529f\u80fd\u8f83\u4e3a\u590d\u6742\uff0c\u4f7f\u7528\u65f6\u9700\u8981\u66f4\u9ad8\u7684\u5b66\u4e60\u6210\u672c\u548c\u7406\u89e3\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u6587\u4ef6\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u9700\u8981\u8003\u8651\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u7f16\u7801\uff0c\u5305\u62ec\u5185\u7f6e\u7684open\u51fd\u6570\u548ccodecs\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528open\u51fd\u6570\u5904\u7406\u6587\u4ef6\u7f16\u7801<\/p>\n<p>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>    print(content)<\/p>\n<h2><strong>\u4f7f\u7528codecs\u6a21\u5757\u5904\u7406\u6587\u4ef6\u7f16\u7801<\/strong><\/h2>\n<p>import codecs<\/p>\n<p>with codecs.open(&#39;example.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<p><h4>\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><strong>\u5904\u7406\u6587\u4ef6\u7f16\u7801<\/strong>\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c\u6587\u4ef6\u65f6\u3002\u4e0d\u540c\u7684\u6587\u4ef6\u53ef\u80fd\u4f7f\u7528\u4e0d\u540c\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u56e0\u6b64\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u9700\u8981\u660e\u786e\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u4ee5\u907f\u514d\u51fa\u73b0\u4e71\u7801\u6216\u8bfb\u53d6\u5931\u8d25\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528open\u51fd\u6570\u548ccodecs\u6a21\u5757\u90fd\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u6587\u4ef6\u7f16\u7801\uff0c\u524d\u8005\u9002\u7528\u4e8e\u4e00\u822c\u7684\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\uff0c\u540e\u8005\u9002\u7528\u4e8e\u9700\u8981\u66f4\u590d\u6742\u7f16\u7801\u8f6c\u6362\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u7f51\u7edc\u6570\u636e\u89e3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u63a5\u6536\u5230\u7684\u6570\u636e\u901a\u5e38\u662f\u5b57\u8282\u6d41\uff0c\u9700\u8981\u8fdb\u884c\u89e3\u7801\u624d\u80fd\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u5b57\u7b26\u4e32\u3002Python\u7684socket\u6a21\u5757\u548crequests\u6a21\u5757\u90fd\u63d0\u4f9b\u4e86\u5904\u7406\u7f51\u7edc\u6570\u636e\u89e3\u7801\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528socket\u6a21\u5757\u5904\u7406\u7f51\u7edc\u6570\u636e\u89e3\u7801<\/p>\n<p>import socket<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aTCP\/IP\u5957\u63a5\u5b57<\/strong><\/h2>\n<p>sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>server_address = (&#39;localhost&#39;, 10000)<\/p>\n<p>sock.connect(server_address)<\/p>\n<p>try:<\/p>\n<p>    # \u53d1\u9001\u6570\u636e<\/p>\n<p>    message = &#39;Hello, World!&#39;<\/p>\n<p>    sock.sendall(message.encode(&#39;utf-8&#39;))<\/p>\n<p>    # \u63a5\u6536\u6570\u636e<\/p>\n<p>    data = sock.recv(1024)<\/p>\n<p>    print(&#39;Received:&#39;, data.decode(&#39;utf-8&#39;))<\/p>\n<p>finally:<\/p>\n<p>    sock.close()<\/p>\n<h2><strong>\u4f7f\u7528requests\u6a21\u5757\u5904\u7406\u7f51\u7edc\u6570\u636e\u89e3\u7801<\/strong><\/h2>\n<p>import requests<\/p>\n<p>response = requests.get(&#39;https:\/\/www.example.com&#39;)<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><strong>\u5904\u7406\u7f51\u7edc\u6570\u636e\u89e3\u7801<\/strong>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\u975e\u5e38\u5e38\u89c1\u3002\u65e0\u8bba\u662f\u901a\u8fc7socket\u6a21\u5757\u8fdb\u884c\u4f4e\u7ea7\u522b\u7684\u7f51\u7edc\u901a\u4fe1\uff0c\u8fd8\u662f\u901a\u8fc7requests\u6a21\u5757\u8fdb\u884cHTTP\u8bf7\u6c42\uff0c\u90fd\u9700\u8981\u5bf9\u63a5\u6536\u5230\u7684\u6570\u636e\u8fdb\u884c\u89e3\u7801\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7f51\u7edc\u6570\u636e\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u56e0\u6b64\u5728\u89e3\u7801\u65f6\u9700\u8981\u6307\u5b9a\u5bf9\u5e94\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406JSON\u6570\u636e\u89e3\u7801<\/h3>\n<\/p>\n<p><p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e7f\u6cdb\u7528\u4e8eWeb\u5f00\u53d1\u548c\u6570\u636e\u4f20\u8f93\u3002Python\u63d0\u4f9b\u4e86json\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884cJSON\u6570\u636e\u7684\u7f16\u7801\u548c\u89e3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>json_data = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}&#39;<\/p>\n<p>data = json.loads(json_data)<\/p>\n<p>print(data)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u5c06Python\u5bf9\u8c61\u7f16\u7801\u4e3aJSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>python_obj = {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30}<\/p>\n<p>json_str = json.dumps(python_obj)<\/p>\n<p>print(json_str)  # \u8f93\u51fa\uff1a{&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><strong>\u5904\u7406JSON\u6570\u636e\u89e3\u7801<\/strong>\u5728Web\u5f00\u53d1\u548c\u6570\u636e\u4f20\u8f93\u4e2d\u975e\u5e38\u5e38\u89c1\u3002JSON\u683c\u5f0f\u5177\u6709\u8f7b\u91cf\u7ea7\u3001\u6613\u4e8e\u9605\u8bfb\u548c\u7f16\u5199\u7b49\u4f18\u70b9\uff0c\u56e0\u6b64\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8eAPI\u6570\u636e\u4f20\u8f93\u3001\u914d\u7f6e\u6587\u4ef6\u7b49\u573a\u666f\u3002Python\u7684json\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884cJSON\u6570\u636e\u7684\u7f16\u7801\u548c\u89e3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406XML\u6570\u636e\u89e3\u7801<\/h3>\n<\/p>\n<p><p>XML\uff08eXtensible Markup Language\uff09\u662f\u4e00\u79cd\u5e38\u7528\u4e8e\u6570\u636e\u4ea4\u6362\u548c\u5b58\u50a8\u7684\u6807\u8bb0\u8bed\u8a00\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6a21\u5757\u6765\u5904\u7406XML\u6570\u636e\uff0c\u5305\u62ecxml.etree.ElementTree\u3001lxml\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>xml_data = &#39;&#39;&#39;&lt;person&gt;<\/p>\n<p>                &lt;name&gt;Alice&lt;\/name&gt;<\/p>\n<p>                &lt;age&gt;25&lt;\/age&gt;<\/p>\n<p>              &lt;\/person&gt;&#39;&#39;&#39;<\/p>\n<p>root = ET.fromstring(xml_data)<\/p>\n<p>name = root.find(&#39;name&#39;).text<\/p>\n<p>age = root.find(&#39;age&#39;).text<\/p>\n<p>print(f&#39;Name: {name}, Age: {age}&#39;)  # \u8f93\u51fa\uff1aName: Alice, Age: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><strong>\u5904\u7406XML\u6570\u636e\u89e3\u7801<\/strong>\u5728Web\u670d\u52a1\u3001\u6570\u636e\u4ea4\u6362\u548c\u5b58\u50a8\u4e2d\u975e\u5e38\u5e38\u89c1\u3002XML\u683c\u5f0f\u5177\u6709\u826f\u597d\u7684\u53ef\u6269\u5c55\u6027\u548c\u81ea\u63cf\u8ff0\u6027\uff0c\u9002\u7528\u4e8e\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u8868\u793a\u3002Python\u7684xml.etree.ElementTree\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884cXML\u6570\u636e\u7684\u89e3\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406CSV\u6570\u636e\u89e3\u7801<\/h3>\n<\/p>\n<p><p>CSV\uff08Comma-Separated Values\uff09\u662f\u4e00\u79cd\u5e38\u7528\u4e8e\u8868\u683c\u6570\u636e\u5b58\u50a8\u548c\u4ea4\u6362\u7684\u683c\u5f0f\u3002Python\u63d0\u4f9b\u4e86csv\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884cCSV\u6570\u636e\u7684\u8bfb\u53d6\u548c\u5199\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>csv_data = &#39;&#39;&#39;name,age<\/p>\n<p>Alice,25<\/p>\n<p>Bob,30&#39;&#39;&#39;<\/p>\n<p>data = csv.reader(csv_data.splitlines())<\/p>\n<p>for row in data:<\/p>\n<p>    print(row)  # \u8f93\u51fa\uff1a[&#39;name&#39;, &#39;age&#39;]\uff0c[&#39;Alice&#39;, &#39;25&#39;]\uff0c[&#39;Bob&#39;, &#39;30&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><strong>\u5904\u7406CSV\u6570\u636e\u89e3\u7801<\/strong>\u5728\u6570\u636e\u5206\u6790\u3001\u8868\u683c\u6570\u636e\u5b58\u50a8\u548c\u4ea4\u6362\u4e2d\u975e\u5e38\u5e38\u89c1\u3002CSV\u683c\u5f0f\u5177\u6709\u7b80\u5355\u3001\u6613\u4e8e\u7406\u89e3\u548c\u5904\u7406\u7b49\u4f18\u70b9\uff0c\u56e0\u6b64\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u5bfc\u5165\u5bfc\u51fa\u3001\u6570\u636e\u5206\u6790\u7b49\u573a\u666f\u3002Python\u7684csv\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884cCSV\u6570\u636e\u7684\u8bfb\u53d6\u548c\u5199\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406YAML\u6570\u636e\u89e3\u7801<\/h3>\n<\/p>\n<p><p>YAML\uff08YAML <a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n&#39;t Markup Language\uff09\u662f\u4e00\u79cd\u5e38\u7528\u4e8e\u914d\u7f6e\u6587\u4ef6\u548c\u6570\u636e\u5e8f\u5217\u5316\u7684\u683c\u5f0f\u3002Python\u63d0\u4f9b\u4e86\u7b2c\u4e09\u65b9\u5e93PyYAML\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884cYAML\u6570\u636e\u7684\u7f16\u7801\u548c\u89e3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>yaml_data = &#39;&#39;&#39;<\/p>\n<p>name: Alice<\/p>\n<p>age: 25<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>data = yaml.<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_load(yaml_data)<\/p>\n<p>print(data)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u5c06Python\u5bf9\u8c61\u7f16\u7801\u4e3aYAML\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>python_obj = {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30}<\/p>\n<p>yaml_str = yaml.safe_dump(python_obj)<\/p>\n<p>print(yaml_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><strong>\u5904\u7406YAML\u6570\u636e\u89e3\u7801<\/strong>\u5728\u914d\u7f6e\u6587\u4ef6\u548c\u6570\u636e\u5e8f\u5217\u5316\u4e2d\u975e\u5e38\u5e38\u89c1\u3002YAML\u683c\u5f0f\u5177\u6709\u7b80\u6d01\u3001\u6613\u4e8e\u9605\u8bfb\u548c\u7f16\u5199\u7b49\u4f18\u70b9\uff0c\u56e0\u6b64\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u914d\u7f6e\u6587\u4ef6\u3001\u6570\u636e\u5e8f\u5217\u5316\u7b49\u573a\u666f\u3002Python\u7684PyYAML\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884cYAML\u6570\u636e\u7684\u7f16\u7801\u548c\u89e3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u89e3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4f4e\u7ea7\u522b\u7684\u6570\u636e\u5b58\u50a8\u548c\u4f20\u8f93\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u3002Python\u63d0\u4f9b\u4e86struct\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u7f16\u7801\u548c\u89e3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>binary_data = struct.pack(&#39;i&#39;, 42)<\/p>\n<p>decoded_data = struct.unpack(&#39;i&#39;, binary_data)<\/p>\n<p>print(decoded_data)  # \u8f93\u51fa\uff1a(42,)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><strong>\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u89e3\u7801<\/strong>\u5728\u4f4e\u7ea7\u522b\u7684\u6570\u636e\u5b58\u50a8\u548c\u4f20\u8f93\u4e2d\u975e\u5e38\u5e38\u89c1\u3002\u4e8c\u8fdb\u5236\u6570\u636e\u5177\u6709\u9ad8\u6548\u3001\u7d27\u51d1\u7b49\u4f18\u70b9\uff0c\u56e0\u6b64\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6587\u4ef6\u5b58\u50a8\u3001\u7f51\u7edc\u901a\u4fe1\u7b49\u573a\u666f\u3002Python\u7684struct\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u7f16\u7801\u548c\u89e3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u5904\u7406\u56fe\u7247\u6570\u636e\u89e3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u56fe\u7247\u6570\u636e\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u56fe\u7247\u6587\u4ef6\u89e3\u7801\u4e3a\u56fe\u50cf\u5bf9\u8c61\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u5904\u7406\u56fe\u7247\u6570\u636e\uff0c\u5305\u62ecPIL\uff08Pillow\uff09\u3001OpenCV\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<p>image.show()<\/p>\n<h2><strong>\u4f7f\u7528OpenCV\u5904\u7406\u56fe\u7247\u6570\u636e\u89e3\u7801<\/strong><\/h2>\n<p>import cv2<\/p>\n<p>image = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<p>cv2.imshow(&#39;image&#39;, image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><strong>\u5904\u7406\u56fe\u7247\u6570\u636e\u89e3\u7801<\/strong>\u5728\u56fe\u50cf\u5904\u7406\u3001\u8ba1\u7b97\u673a\u89c6\u89c9\u7b49\u9886\u57df\u975e\u5e38\u5e38\u89c1\u3002\u56fe\u7247\u6570\u636e\u901a\u5e38\u4ee5\u6587\u4ef6\u7684\u5f62\u5f0f\u5b58\u50a8\uff0c\u9700\u8981\u5c06\u5176\u89e3\u7801\u4e3a\u56fe\u50cf\u5bf9\u8c61\uff0c\u624d\u80fd\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u548c\u5206\u6790\u3002PIL\uff08Pillow\uff09\u548cOpenCV\u662fPython\u4e2d\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u56fe\u7247\u6570\u636e\u7684\u89e3\u7801\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u5904\u7406\u97f3\u9891\u6570\u636e\u89e3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u97f3\u9891\u6570\u636e\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u97f3\u9891\u6587\u4ef6\u89e3\u7801\u4e3a\u97f3\u9891\u4fe1\u53f7\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u5904\u7406\u97f3\u9891\u6570\u636e\uff0c\u5305\u62ecwave\u3001pydub\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wave<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>with wave.open(&#39;example.wav&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    params = file.getparams()<\/p>\n<p>    frames = file.readframes(params.nframes)<\/p>\n<p>    print(params)<\/p>\n<p>    print(frames[:10])<\/p>\n<h2><strong>\u4f7f\u7528pydub\u5904\u7406\u97f3\u9891\u6570\u636e\u89e3\u7801<\/strong><\/h2>\n<p>from pydub import AudioSegment<\/p>\n<p>audio = AudioSegment.from_file(&#39;example.mp3&#39;)<\/p>\n<p>print(audio.duration_seconds)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><strong>\u5904\u7406\u97f3\u9891\u6570\u636e\u89e3\u7801<\/strong>\u5728\u97f3\u9891\u5904\u7406\u3001\u8bed\u97f3\u8bc6\u522b\u7b49\u9886\u57df\u975e\u5e38\u5e38\u89c1\u3002\u97f3\u9891\u6570\u636e\u901a\u5e38\u4ee5\u6587\u4ef6\u7684\u5f62\u5f0f\u5b58\u50a8\uff0c\u9700\u8981\u5c06\u5176\u89e3\u7801\u4e3a\u97f3\u9891\u4fe1\u53f7\uff0c\u624d\u80fd\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u548c\u5206\u6790\u3002wave\u548cpydub\u662fPython\u4e2d\u5e38\u7528\u7684\u97f3\u9891\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u97f3\u9891\u6570\u636e\u7684\u89e3\u7801\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u5904\u7406\u89c6\u9891\u6570\u636e\u89e3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u89c6\u9891\u6570\u636e\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u89c6\u9891\u6587\u4ef6\u89e3\u7801\u4e3a\u89c6\u9891\u5e27\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u5904\u7406\u89c6\u9891\u6570\u636e\uff0c\u5305\u62eccv2\uff08OpenCV\uff09\u3001moviepy\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(&#39;example.mp4&#39;)<\/p>\n<p>while(cap.isOpened()):<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if ret:<\/p>\n<p>        cv2.imshow(&#39;frame&#39;, frame)<\/p>\n<p>        if cv2.waitKey(1) &amp; 0xFF == ord(&#39;q&#39;):<\/p>\n<p>            break<\/p>\n<p>    else:<\/p>\n<p>        break<\/p>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<h2><strong>\u4f7f\u7528moviepy\u5904\u7406\u89c6\u9891\u6570\u636e\u89e3\u7801<\/strong><\/h2>\n<p>from moviepy.editor import VideoFileClip<\/p>\n<p>clip = VideoFileClip(&#39;example.mp4&#39;)<\/p>\n<p>clip.preview()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><strong>\u5904\u7406\u89c6\u9891\u6570\u636e\u89e3\u7801<\/strong>\u5728\u89c6\u9891\u5904\u7406\u3001\u8ba1\u7b97\u673a\u89c6\u89c9\u7b49\u9886\u57df\u975e\u5e38\u5e38\u89c1\u3002\u89c6\u9891\u6570\u636e\u901a\u5e38\u4ee5\u6587\u4ef6\u7684\u5f62\u5f0f\u5b58\u50a8\uff0c\u9700\u8981\u5c06\u5176\u89e3\u7801\u4e3a\u89c6\u9891\u5e27\uff0c\u624d\u80fd\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u548c\u5206\u6790\u3002cv2\uff08OpenCV\uff09\u548cmoviepy\u662fPython\u4e2d\u5e38\u7528\u7684\u89c6\u9891\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u89c6\u9891\u6570\u636e\u7684\u89e3\u7801\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u5904\u7406\u52a0\u5bc6\u6570\u636e\u89e3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u52a0\u5bc6\u6570\u636e\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u52a0\u5bc6\u6570\u636e\u89e3\u7801\u4e3a\u539f\u59cb\u6570\u636e\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u5904\u7406\u52a0\u5bc6\u6570\u636e\uff0c\u5305\u62eccryptography\u3001pycryptodome\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cryptography.fernet import Fernet<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>key = Fernet.generate_key()<\/p>\n<p>cipher_suite = Fernet(key)<\/p>\n<p>cipher_text = cipher_suite.encrypt(b&quot;Hello, World!&quot;)<\/p>\n<p>plain_text = cipher_suite.decrypt(cipher_text)<\/p>\n<p>print(plain_text)  # \u8f93\u51fa\uff1ab&#39;Hello, World!&#39;<\/p>\n<h2><strong>\u4f7f\u7528pycryptodome\u5904\u7406\u52a0\u5bc6\u6570\u636e\u89e3\u7801<\/strong><\/h2>\n<p>from Crypto.Cipher import AES<\/p>\n<p>from Crypto.Util.Padding import pad, unpad<\/p>\n<p>key = b&#39;Sixteen byte key&#39;<\/p>\n<p>cipher = AES.new(key, AES.MODE_ECB)<\/p>\n<p>cipher_text = cipher.encrypt(pad(b&#39;Hello, World!&#39;, AES.block_size))<\/p>\n<p>plain_text = unpad(cipher.decrypt(cipher_text), AES.block_size)<\/p>\n<p>print(plain_text)  # \u8f93\u51fa\uff1ab&#39;Hello, World!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><strong>\u5904\u7406\u52a0\u5bc6\u6570\u636e\u89e3\u7801<\/strong>\u5728\u6570\u636e\u5b89\u5168\u3001\u9690\u79c1\u4fdd\u62a4\u7b49\u9886\u57df\u975e\u5e38\u5e38\u89c1\u3002\u52a0\u5bc6\u6570\u636e\u9700\u8981\u8fdb\u884c\u89e3\u7801\uff0c\u624d\u80fd\u8fd8\u539f\u4e3a\u539f\u59cb\u6570\u636e\u3002cryptography\u548cpycryptodome\u662fPython\u4e2d\u5e38\u7528\u7684\u52a0\u5bc6\u89e3\u5bc6\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u52a0\u5bc6\u6570\u636e\u7684\u89e3\u7801\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u5904\u7406\u81ea\u5b9a\u4e49\u6570\u636e\u89e3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e00\u4e9b\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u81ea\u5df1\u7f16\u5199\u89e3\u7801\u51fd\u6570\u3002Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u6570\u636e\u5904\u7406\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u81ea\u5b9a\u4e49\u6570\u636e\u7684\u89e3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>custom_data = &#39;name:Alice,age:25&#39;<\/p>\n<p>data = dict(item.split(&#39;:&#39;) for item in custom_data.split(&#39;,&#39;))<\/p>\n<p>print(data)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: &#39;25&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p><strong>\u5904\u7406\u81ea\u5b9a\u4e49\u6570\u636e\u89e3\u7801<\/strong>\u5728\u4e00\u4e9b\u7279\u5b9a\u573a\u666f\u4e2d\u975e\u5e38\u5e38\u89c1\u3002\u4f8b\u5982\uff0c\u67d0\u4e9b\u5e94\u7528\u7a0b\u5e8f\u53ef\u80fd\u4f7f\u7528\u7279\u5b9a\u7684\u683c\u5f0f\u6765\u5b58\u50a8\u548c\u4f20\u8f93\u6570\u636e\uff0c\u9700\u8981\u81ea\u5df1\u7f16\u5199\u89e3\u7801\u51fd\u6570\u6765\u89e3\u6790\u8fd9\u4e9b\u6570\u636e\u3002Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u6570\u636e\u5904\u7406\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u81ea\u5b9a\u4e49\u6570\u636e\u7684\u89e3\u7801\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8fdb\u884c\u89e3\u7801\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u683c\u5f0f\u3002<strong>\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u4f7f\u7528base64\u6a21\u5757\u3001\u4f7f\u7528codecs\u6a21\u5757\u3001\u5904\u7406\u6587\u4ef6\u7f16\u7801\u3001\u5904\u7406\u7f51\u7edc\u6570\u636e\u89e3\u7801\u3001\u5904\u7406JSON\u6570\u636e\u89e3\u7801\u3001\u5904\u7406XML\u6570\u636e\u89e3\u7801\u3001\u5904\u7406CSV\u6570\u636e\u89e3\u7801\u3001\u5904\u7406YAML\u6570\u636e\u89e3\u7801\u3001\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u89e3\u7801\u3001\u5904\u7406\u56fe\u7247\u6570\u636e\u89e3\u7801\u3001\u5904\u7406\u97f3\u9891\u6570\u636e\u89e3\u7801\u3001\u5904\u7406\u89c6\u9891\u6570\u636e\u89e3\u7801\u3001\u5904\u7406\u52a0\u5bc6\u6570\u636e\u89e3\u7801\u3001\u5904\u7406\u81ea\u5b9a\u4e49\u6570\u636e\u89e3\u7801<\/strong>\u7b49\u65b9\u6cd5\u90fd\u5404\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u7406\u89e3\u5e76\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u89e3\u7801\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u89e3\u7801\u65b9\u5f0f\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u89e3\u7801\u65b9\u5f0f\uff0c\u5e38\u89c1\u7684\u5305\u62ec\u5b57\u7b26\u4e32\u89e3\u7801\u548c\u6587\u4ef6\u89e3\u7801\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528<code>bytes.decode()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u8282\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u5bf9\u4e8e\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff08\u5982UTF-8\u3001GBK\u7b49\uff09\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u8fdb\u884c\u89e3\u7801\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:\n    content = f.read()\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u89e3\u7801\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u89e3\u7801\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u4f8b\u5982<code>UnicodeDecodeError<\/code>\u3002\u4e3a\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528<code>errors<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u9519\u8bef\u5904\u7406\u65b9\u6848\uff0c\u5982<code>ignore<\/code>\u3001<code>replace<\/code>\u6216<code>backslashreplace<\/code>\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">byte_string = b&#39;\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd&#39;\ndecoded_string = byte_string.decode(&#39;utf-8&#39;, errors=&#39;ignore&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u786e\u4fdd\u5728\u9047\u5230\u65e0\u6cd5\u89e3\u7801\u7684\u5b57\u8282\u65f6\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u8ba4\u5b57\u8282\u6570\u636e\u7684\u7f16\u7801\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u786e\u8ba4\u5b57\u8282\u6570\u636e\u7684\u7f16\u7801\u683c\u5f0f\u5e76\u4e0d\u603b\u662f\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>chardet<\/code>\u5e93\u6765\u81ea\u52a8\u68c0\u6d4b\u7f16\u7801\u3002\u5b89\u88c5\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4f7f\u7528\uff1a  <\/p>\n<pre><code class=\"language-python\">import chardet\n\nbyte_data = b&#39;\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd&#39;\nresult = chardet.detect(byte_data)\nprint(result[&#39;encoding&#39;])\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u7528\u6237\u627e\u5230\u5408\u9002\u7684\u89e3\u7801\u65b9\u5f0f\uff0c\u51cf\u5c11\u56e0\u7f16\u7801\u4e0d\u4e00\u81f4\u800c\u5bfc\u81f4\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8fdb\u884c\u89e3\u7801\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u4f7f\u7528base64\u6a21\u5757\u3001\u4f7f\u7528codecs\u6a21 [&hellip;]","protected":false},"author":3,"featured_media":1167023,"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\/1167016"}],"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=1167016"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167016\/revisions"}],"predecessor-version":[{"id":1167025,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167016\/revisions\/1167025"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1167023"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1167016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1167016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1167016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}