{"id":931400,"date":"2024-12-26T17:35:39","date_gmt":"2024-12-26T09:35:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/931400.html"},"modified":"2024-12-26T17:35:42","modified_gmt":"2024-12-26T09:35:42","slug":"python-%e5%a6%82%e4%bd%95%e5%ad%98%e5%82%a8%e5%ad%97%e8%8a%82","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/931400.html","title":{"rendered":"python \u5982\u4f55\u5b58\u50a8\u5b57\u8282"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065717\/34485911-9d45-43f5-bb0e-c8079f1db4e9.webp\" alt=\"python \u5982\u4f55\u5b58\u50a8\u5b57\u8282\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d:  <\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5b58\u50a8\u5b57\u8282\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec<strong>\u4f7f\u7528\u5b57\u8282\u5bf9\u8c61\u3001\u6587\u4ef6\u64cd\u4f5c\u3001\u6570\u636e\u5e93\u5b58\u50a8<\/strong>\u7b49\u3002\u5b57\u8282\u5bf9\u8c61\u662fPython\u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u80fd\u591f\u76f4\u63a5\u5b58\u50a8\u548c\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\uff1b\u6587\u4ef6\u64cd\u4f5c\u5219\u5141\u8bb8\u5c06\u5b57\u8282\u6570\u636e\u5199\u5165\u5230\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u8bbf\u95ee\u548c\u7ba1\u7406\uff1b\u800c\u6570\u636e\u5e93\u5b58\u50a8\u9002\u7528\u4e8e\u9700\u8981\u957f\u671f\u4fdd\u5b58\u5e76\u9ad8\u6548\u7ba1\u7406\u5927\u91cf\u5b57\u8282\u6570\u636e\u7684\u573a\u666f\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u5b57\u8282\u5bf9\u8c61\u662f\u6700\u4e3a\u76f4\u63a5\u548c\u7b80\u5355\u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u5185\u5b58\u4e2d\u5b58\u50a8\u548c\u64cd\u4f5c\u5b57\u8282\u6570\u636e\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u77ed\u671f\u6216\u7b80\u5355\u7684\u5b57\u8282\u5b58\u50a8\u9700\u6c42\u3002  <\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b57\u8282\u5bf9\u8c61\u7684\u4f7f\u7528  <\/p>\n<\/p>\n<p><p>\u5b57\u8282\u5bf9\u8c61\u662fPython\u4e2d\u4e00\u79cd\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u5185\u7f6e\u7c7b\u578b\u3002\u5b57\u8282\u5bf9\u8c61\u7684\u5b9a\u4e49\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u6570\u636e\u7528b&#39;&#39;\u5305\u56f4\u5373\u53ef\u3002\u4f8b\u5982\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">byte_data = b&#39;Hello World&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u8282\u5bf9\u8c61\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u5b58\u50a8\u548c\u5904\u7406\u5c0f\u89c4\u6a21\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u573a\u5408\u3002\u5b57\u8282\u5bf9\u8c61\u4e0d\u4ec5\u652f\u6301\u5e38\u89c4\u7684\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u65b9\u6cd5\u7528\u4e8e\u6570\u636e\u7684\u8f6c\u6362\u548c\u5904\u7406\uff0c\u5982<code>hex()<\/code>\u3001<code>decode()<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u5b57\u8282\u5bf9\u8c61\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u6027\u662f\u5b83\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u80fd\u518d\u4fee\u6539\u3002\u8fd9\u79cd\u4e0d\u53ef\u53d8\u6027\u4fdd\u8bc1\u4e86\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u4e00\u81f4\u6027\uff0c\u540c\u65f6\u4e5f\u4f7f\u5f97\u5b57\u8282\u5bf9\u8c61\u53ef\u4ee5\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\u4f7f\u7528\u3002\u5bf9\u4e8e\u9700\u8981\u9891\u7e41\u66f4\u65b0\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5b57\u8282\u6570\u7ec4\uff08<code>bytearray<\/code>\uff09\uff0c\u5b83\u662f\u5b57\u8282\u5bf9\u8c61\u7684\u53ef\u53d8\u7248\u672c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u6587\u4ef6\u64cd\u4f5c\u5b58\u50a8\u5b57\u8282  <\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u957f\u671f\u4fdd\u5b58\u7684\u5b57\u8282\u6570\u636e\uff0c\u6587\u4ef6\u5b58\u50a8\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u9009\u62e9\u3002\u5728Python\u4e2d\uff0c\u6587\u4ef6\u64cd\u4f5c\u975e\u5e38\u7b80\u5355\uff0c\u901a\u8fc7\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u5373\u53ef\u5b9e\u73b0\u3002  <\/p>\n<\/p>\n<ol>\n<li>\u5199\u5165\u5b57\u8282\u6570\u636e\u5230\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5c06\u6587\u4ef6\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\uff0c\u7136\u540e\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u5b57\u8282\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u4f8b\u5982\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;data.bin&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    file.write(byte_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>&#39;wb&#39;<\/code>\u8868\u793a\u4ee5\u4e8c\u8fdb\u5236\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5b57\u8282\u6570\u636e\u88ab\u6b63\u786e\u5199\u5165\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u8bfb\u53d6\u5b57\u8282\u6570\u636e<\/li>\n<\/ol>\n<p><p>\u540c\u6837\uff0c\u901a\u8fc7\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>read()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u5b57\u8282\u6570\u636e\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;data.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    byte_data = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>&#39;rb&#39;<\/code>\u6a21\u5f0f\u8868\u793a\u4ee5\u4e8c\u8fdb\u5236\u8bfb\u53d6\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u8fd9\u6837\u8bfb\u53d6\u7684\u6570\u636e\u5c06\u4ee5\u5b57\u8282\u5bf9\u8c61\u7684\u5f62\u5f0f\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6570\u636e\u5e93\u5b58\u50a8\u5b57\u8282  <\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u9ad8\u6548\u5b58\u50a8\u548c\u7ba1\u7406\u5927\u91cf\u5b57\u8282\u6570\u636e\u7684\u5e94\u7528\uff0c\u6570\u636e\u5e93\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u8bb8\u591a\u6570\u636e\u5e93\u7cfb\u7edf\u90fd\u652f\u6301\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61\uff08BLOB\uff09\u7c7b\u578b\u7684\u6570\u636e\u5b58\u50a8\u3002  <\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528SQLite\u5b58\u50a8\u5b57\u8282\u6570\u636e<\/li>\n<\/ol>\n<p><p>SQLite\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u9002\u5408\u5c0f\u578b\u5e94\u7528\u548c\u539f\u578b\u5f00\u53d1\u3002\u53ef\u4ee5\u901a\u8fc7<code>sqlite3<\/code>\u6a21\u5757\u6765\u64cd\u4f5cSQLite\u6570\u636e\u5e93\uff0c\u5e76\u5b58\u50a8\u5b57\u8282\u6570\u636e\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>c = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS files (id INTEGER PRIMARY KEY, data BLOB)&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>c.execute(&#39;INSERT INTO files (data) VALUES (?)&#39;, (byte_data,))<\/p>\n<p>conn.commit()<\/p>\n<h2><strong>\u8bfb\u53d6\u5b57\u8282\u6570\u636e<\/strong><\/h2>\n<p>c.execute(&#39;SELECT data FROM files WHERE id=?&#39;, (1,))<\/p>\n<p>data = c.fetchone()[0]<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u8282\u6570\u636e\u88ab\u63d2\u5165\u5230\u540d\u4e3a<code>files<\/code>\u7684\u8868\u4e2d\uff0c\u5e76\u53ef\u4ee5\u901a\u8fc7\u4e3b\u952eID\u8fdb\u884c\u68c0\u7d22\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u5176\u4ed6\u6570\u636e\u5e93<\/li>\n<\/ol>\n<p><p>\u5176\u4ed6\u6570\u636e\u5e93\u5982MySQL\u3001PostgreSQL\u7b49\u4e5f\u652f\u6301BLOB\u6570\u636e\u7c7b\u578b\u3002\u4f7f\u7528\u8fd9\u4e9b\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u5229\u7528\u76f8\u5e94\u7684Python\u5e93\uff08\u5982<code>mysql-connector-python<\/code>\u3001<code>psycopg2<\/code>\u7b49\uff09\u6765\u5b9e\u73b0\u5b57\u8282\u6570\u636e\u7684\u5b58\u50a8\u548c\u68c0\u7d22\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b57\u8282\u6570\u636e\u7684\u7f16\u7801\u4e0e\u89e3\u7801  <\/p>\n<\/p>\n<p><p>\u5728\u5b58\u50a8\u548c\u4f20\u8f93\u5b57\u8282\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u6b63\u786e\u6027\u548c\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\u7f16\u7801\u5b57\u8282\u6570\u636e<\/li>\n<\/ol>\n<p><p>\u7f16\u7801\u662f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u636e\u7684\u8fc7\u7a0b\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u7f16\u7801\u65b9\u5f0f\uff0c\u5982UTF-8\u3001ASCII\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>encode()<\/code>\u65b9\u6cd5\u8fdb\u884c\u7f16\u7801\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_data = &#39;Hello World&#39;<\/p>\n<p>byte_data = string_data.encode(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u89e3\u7801\u5b57\u8282\u6570\u636e<\/li>\n<\/ol>\n<p><p>\u89e3\u7801\u662f\u5c06\u5b57\u8282\u6570\u636e\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u8282\u5bf9\u8c61\u7684<code>decode()<\/code>\u65b9\u6cd5\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">decoded_string = byte_data.decode(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u5728\u89e3\u7801\u65f6\u4f7f\u7528\u4e0e\u7f16\u7801\u65f6\u76f8\u540c\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4ee5\u907f\u514d\u6570\u636e\u635f\u574f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5b57\u8282\u6570\u636e\u7684\u8f6c\u6362\u4e0e\u64cd\u4f5c  <\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u8282\u6570\u636e\u8fdb\u884c\u590d\u6742\u7684\u8f6c\u6362\u548c\u64cd\u4f5c\u3002Python\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u5b8c\u6210\u8fd9\u4e9b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528struct\u6a21\u5757<\/li>\n<\/ol>\n<p><p><code>struct<\/code>\u6a21\u5757\u53ef\u4ee5\u5c06Python\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0e\u5b57\u8282\u6570\u636e\u8fdb\u884c\u76f8\u4e92\u8f6c\u6362\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\u548c\u7f51\u7edc\u6570\u636e\u3002  <\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u636e\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<p>byte_data = struct.pack(&#39;i&#39;, 12345)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u89e3\u5305\u5b57\u8282\u6570\u636e\u4e3a\u6574\u6570\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">unpacked_data = struct.unpack(&#39;i&#39;, byte_data)[0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528binascii\u6a21\u5757<\/li>\n<\/ol>\n<p><p><code>binascii<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u7528\u4e8e\u8f6c\u6362\u4e8c\u8fdb\u5236\u6570\u636e\u548cASCII\u8868\u793a\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u5982\u5341\u516d\u8fdb\u5236\u7f16\u7801\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import binascii<\/p>\n<p>hex_data = binascii.hexlify(byte_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53cd\u5411\u8f6c\u6362\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">byte_data = binascii.unhexlify(hex_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5b57\u8282\u6570\u636e\u7684\u5e94\u7528\u573a\u666f  <\/p>\n<\/p>\n<p><p>\u5b57\u8282\u6570\u636e\u5728\u8bb8\u591a\u5e94\u7528\u573a\u666f\u4e2d\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u7f51\u7edc\u7f16\u7a0b<\/li>\n<\/ol>\n<p><p>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u4ee5\u5b57\u8282\u5f62\u5f0f\u5728\u7f51\u7edc\u4e2d\u4f20\u8f93\u3002\u4f7f\u7528\u5b57\u8282\u6570\u636e\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\u548c\u54cd\u5e94\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u56fe\u50cf\u548c\u89c6\u9891\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u56fe\u50cf\u548c\u89c6\u9891\u6587\u4ef6\u901a\u5e38\u4ee5\u4e8c\u8fdb\u5236\u683c\u5f0f\u5b58\u50a8\u548c\u4f20\u8f93\u3002\u901a\u8fc7\u5b57\u8282\u6570\u636e\uff0c\u53ef\u4ee5\u8bfb\u53d6\u3001\u4fee\u6539\u548c\u4fdd\u5b58\u8fd9\u4e9b\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u6587\u4ef6\u538b\u7f29\u4e0e\u89e3\u538b<\/li>\n<\/ol>\n<p><p>\u5b57\u8282\u6570\u636e\u53ef\u4ee5\u7528\u4e8e\u538b\u7f29\u548c\u89e3\u538b\u7f29\u6587\u4ef6\uff0c\u4ee5\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\u548c\u5e26\u5bbd\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\uff08\u5982<code>zlib<\/code>\u3001<code>gzip<\/code>\u7b49\uff09\u53ef\u4ee5\u5b9e\u73b0\u6587\u4ef6\u7684\u538b\u7f29\u548c\u89e3\u538b\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3  <\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5b58\u50a8\u548c\u64cd\u4f5c\u5b57\u8282\u6570\u636e\uff0c\u5305\u62ec\u5b57\u8282\u5bf9\u8c61\u3001\u6587\u4ef6\u64cd\u4f5c\u548c\u6570\u636e\u5e93\u5b58\u50a8\u7b49\u3002\u901a\u8fc7\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u7684\u7075\u6d3b\u8fd0\u7528\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u5f62\u5f0f\u7684\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u5e94\u7528\u9700\u6c42\u3002\u5728\u5904\u7406\u5b57\u8282\u6570\u636e\u65f6\uff0c\u7f16\u7801\u4e0e\u89e3\u7801\u3001\u8f6c\u6362\u4e0e\u64cd\u4f5c\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u6b65\u9aa4\uff0c\u786e\u4fdd\u6570\u636e\u7684\u6b63\u786e\u6027\u548c\u517c\u5bb9\u6027\u3002\u968f\u7740\u6280\u672f\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u5b57\u8282\u6570\u636e\u7684\u5e94\u7528\u573a\u666f\u4e5f\u5c06\u8d8a\u6765\u8d8a\u5e7f\u6cdb\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u66f4\u591a\u7684\u521b\u65b0\u53ef\u80fd\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5b58\u50a8\u5b57\u8282\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>bytes<\/code>\u7c7b\u578b\u6765\u5b58\u50a8\u5b57\u8282\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b57\u9762\u91cf\uff08\u4f8b\u5982<code>b&#39;example&#39;<\/code>\uff09\u6216\u4f7f\u7528<code>bytes()<\/code>\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u5b57\u8282\u5bf9\u8c61\u3002\u6b64\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5982<code>open()<\/code>\u51fd\u6570\uff0c\u5141\u8bb8\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u8bfb\u5199\u6587\u4ef6\uff0c\u4ece\u800c\u6709\u6548\u5b58\u50a8\u5b57\u8282\u6570\u636e\u3002<\/p>\n<p><strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u5c06\u5b57\u8282\u5199\u5165\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u6765\u5c06\u5b57\u8282\u5199\u5165\u6587\u4ef6\u3002\u4f7f\u7528<code>with open(&#39;filename&#39;, &#39;wb&#39;) as file:<\/code>\u8bed\u53e5\u53ef\u4ee5\u5b89\u5168\u5730\u6253\u5f00\u6587\u4ef6\u5e76\u5199\u5165\u5b57\u8282\u6570\u636e\u3002\u4f7f\u7528<code>file.write(b&#39;your_bytes_data&#39;)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u8282\u76f4\u63a5\u5199\u5165\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u5b8c\u6210\u64cd\u4f5c\u540e\u5173\u95ed\u6587\u4ef6\uff0c\u867d\u7136\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u4f1a\u81ea\u52a8\u5904\u7406\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b58\u50a8\u7684\u5b57\u8282\u6570\u636e\uff1f<\/strong><br \/>\u8981\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u8282\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u4f8b\u5982<code>with open(&#39;filename&#39;, &#39;rb&#39;) as file:<\/code>\u3002\u4f7f\u7528<code>file.read()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u7684\u5b57\u8282\u5185\u5bb9\u3002\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5927\u5c0f\u53c2\u6570\u6765\u9010\u5757\u8bfb\u53d6\u5b57\u8282\u6570\u636e\uff0c\u786e\u4fdd\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\u4e0d\u4f1a\u6d88\u8017\u8fc7\u591a\u5185\u5b58\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d: \u5728Python\u4e2d\u5b58\u50a8\u5b57\u8282\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5b57\u8282\u5bf9\u8c61\u3001\u6587\u4ef6\u64cd\u4f5c\u3001\u6570\u636e\u5e93\u5b58\u50a8\u7b49\u3002\u5b57\u8282 [&hellip;]","protected":false},"author":3,"featured_media":931403,"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\/931400"}],"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=931400"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931400\/revisions"}],"predecessor-version":[{"id":931404,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931400\/revisions\/931404"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/931403"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=931400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=931400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=931400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}