{"id":1135338,"date":"2025-01-08T21:24:21","date_gmt":"2025-01-08T13:24:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1135338.html"},"modified":"2025-01-08T21:24:24","modified_gmt":"2025-01-08T13:24:24","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e6%8d%ae%e8%bd%ac%e5%88%b0%e7%bc%93%e5%86%b2%e5%8c%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1135338.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u6570\u636e\u8f6c\u5230\u7f13\u51b2\u533a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104015\/33f8dfc2-78cc-4116-b2d7-f17fb6686eb9.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u6570\u636e\u8f6c\u5230\u7f13\u51b2\u533a\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528I\/O\u5e93\u5c06\u6570\u636e\u8f6c\u5230\u7f13\u51b2\u533a\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>io<\/code>\u6a21\u5757\u3001<code>BytesIO<\/code>\u3001<code>StringIO<\/code>\u7b49\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u6570\u636e\u53ef\u4ee5\u88ab\u5199\u5165\u5185\u5b58\u4e2d\u7684\u7f13\u51b2\u533a\uff0c\u4ece\u800c\u907f\u514d\u9891\u7e41\u7684\u78c1\u76d8I\/O\u64cd\u4f5c\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>BytesIO<\/code>\u548c<code>StringIO<\/code>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002<code>BytesIO<\/code>\u7528\u4e8e\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u800c<code>StringIO<\/code>\u7528\u4e8e\u6587\u672c\u6570\u636e\u3002<strong>\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u6570\u636e\u5199\u5165<code>BytesIO<\/code>\u5bf9\u8c61\u6765\u6a21\u62df\u6587\u4ef6\u64cd\u4f5c\uff0c\u800c\u65e0\u9700\u5b9e\u9645\u521b\u5efa\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u975e\u5e38\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u51cf\u5c11\u4e86\u78c1\u76d8I\/O\u7684\u5f00\u9500<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001PYTHON \u4e2d\u7684 I\/O \u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684 I\/O \u6a21\u5757\u6765\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u548c\u6587\u4ef6\u64cd\u4f5c\u3002\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5185\u7f6e\u7684 open \u51fd\u6570<\/strong>\uff1a\u7528\u4e8e\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002<\/li>\n<li><strong>io \u6a21\u5757<\/strong>\uff1a\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684 I\/O \u64cd\u4f5c\u529f\u80fd\u3002<\/li>\n<\/ul>\n<p><h4>1\u3001\u5185\u7f6e\u7684 open \u51fd\u6570<\/h4>\n<\/p>\n<p><p>Python \u7684\u5185\u7f6e open \u51fd\u6570\u662f\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u7684\u57fa\u672c\u5de5\u5177\u3002\u901a\u8fc7 open \u51fd\u6570\u53ef\u4ee5\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>open<\/code> \u51fd\u6570\u6253\u5f00\u4e86\u4e00\u4e2a\u540d\u4e3a <code>example.txt<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u4ee5\u5199\u6a21\u5f0f (<code>&#39;w&#39;<\/code>) \u6253\u5f00\u5b83\u3002\u7136\u540e\uff0c<code>write<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001io \u6a21\u5757<\/h4>\n<\/p>\n<p><p>Python \u7684 <code>io<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd I\/O \u64cd\u4f5c\u529f\u80fd\uff0c\u5305\u62ec\u8bfb\u5199\u6587\u672c\u548c\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u5b83\u5305\u542b\u4e86\u51e0\u4e2a\u91cd\u8981\u7684\u7c7b\uff0c\u4f8b\u5982 <code>TextIOWrapper<\/code>\u3001<code>BufferedReader<\/code>\u3001<code>BufferedWriter<\/code> \u7b49\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 <code>io<\/code> \u6a21\u5757\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import io<\/p>\n<p>buffer = io.StringIO()<\/p>\n<p>buffer.write(&#39;Hello, World!&#39;)<\/p>\n<p>print(buffer.getvalue())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>io.StringIO<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u5185\u5b58\u4e2d\u7684\u6587\u672c\u7f13\u51b2\u533a\uff0c\u53ef\u4ee5\u50cf\u6587\u4ef6\u4e00\u6837\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001BYTESIO \u548c STRINGIO \u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>BytesIO<\/code> \u548c <code>StringIO<\/code> \u662f Python <code>io<\/code> \u6a21\u5757\u4e2d\u7684\u4e24\u4e2a\u91cd\u8981\u7c7b\uff0c\u7528\u4e8e\u5904\u7406\u5185\u5b58\u4e2d\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u548c\u6587\u672c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001BytesIO<\/h4>\n<\/p>\n<p><p><code>BytesIO<\/code> \u7c7b\u7528\u4e8e\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u5b83\u521b\u5efa\u4e00\u4e2a\u5185\u5b58\u4e2d\u7684\u4e8c\u8fdb\u5236\u7f13\u51b2\u533a\uff0c\u53ef\u4ee5\u50cf\u6587\u4ef6\u4e00\u6837\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 <code>BytesIO<\/code> \u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import io<\/p>\n<p>buffer = io.BytesIO()<\/p>\n<p>buffer.write(b&#39;Hello, World!&#39;)<\/p>\n<p>print(buffer.getvalue())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>io.BytesIO<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u5185\u5b58\u4e2d\u7684\u4e8c\u8fdb\u5236\u7f13\u51b2\u533a\uff0c\u5e76\u5c06\u5b57\u8282\u5b57\u7b26\u4e32\u5199\u5165\u7f13\u51b2\u533a\u3002\u901a\u8fc7 <code>getvalue<\/code> \u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u7f13\u51b2\u533a\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2\u3001StringIO<\/h4>\n<\/p>\n<p><p><code>StringIO<\/code> \u7c7b\u7528\u4e8e\u5904\u7406\u6587\u672c\u6570\u636e\u3002\u5b83\u521b\u5efa\u4e00\u4e2a\u5185\u5b58\u4e2d\u7684\u6587\u672c\u7f13\u51b2\u533a\uff0c\u53ef\u4ee5\u50cf\u6587\u4ef6\u4e00\u6837\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 <code>StringIO<\/code> \u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import io<\/p>\n<p>buffer = io.StringIO()<\/p>\n<p>buffer.write(&#39;Hello, World!&#39;)<\/p>\n<p>print(buffer.getvalue())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>io.StringIO<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u5185\u5b58\u4e2d\u7684\u6587\u672c\u7f13\u51b2\u533a\uff0c\u5e76\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u7f13\u51b2\u533a\u3002\u901a\u8fc7 <code>getvalue<\/code> \u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u7f13\u51b2\u533a\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7f13\u51b2\u533a\u8fdb\u884c\u6570\u636e\u5904\u7406\u7684\u4f18\u52bf<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u7f13\u51b2\u533a\u8fdb\u884c\u6570\u636e\u5904\u7406\u6709\u4ee5\u4e0b\u51e0\u4e2a\u4e3b\u8981\u4f18\u52bf\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u63d0\u9ad8\u6027\u80fd<\/strong>\uff1a\u51cf\u5c11\u9891\u7e41\u7684\u78c1\u76d8 I\/O \u64cd\u4f5c\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u3002<\/li>\n<li><strong>\u7b80\u5316\u6570\u636e\u5904\u7406<\/strong>\uff1a\u7f13\u51b2\u533a\u53ef\u4ee5\u50cf\u6587\u4ef6\u4e00\u6837\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\uff0c\u4f7f\u5f97\u6570\u636e\u5904\u7406\u66f4\u52a0\u7b80\u4fbf\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong>\uff1a\u4f7f\u7528\u7f13\u51b2\u533a\u8fdb\u884c\u6570\u636e\u5904\u7406\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u7f13\u51b2\u533a\u7684\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u7f13\u51b2\u533a\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u5f88\u591a\u573a\u666f\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u7f51\u7edc\u6570\u636e\u4f20\u8f93<\/h4>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u7f13\u51b2\u533a\u7ecf\u5e38\u7528\u4e8e\u6682\u5b58\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5728\u63a5\u6536\u548c\u53d1\u9001\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u51b2\u533a\u6682\u5b58\u6570\u636e\uff0c\u907f\u514d\u9891\u7e41\u7684\u7f51\u7edc I\/O \u64cd\u4f5c\uff0c\u63d0\u9ad8\u6570\u636e\u4f20\u8f93\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 <code>BytesIO<\/code> \u8fdb\u884c\u7f51\u7edc\u6570\u636e\u4f20\u8f93\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import io<\/p>\n<p>import socket<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a socket \u5bf9\u8c61<\/strong><\/h2>\n<p>sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u670d\u52a1\u5668<\/strong><\/h2>\n<p>sock.connect((&#39;localhost&#39;, 8080))<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a BytesIO \u7f13\u51b2\u533a<\/strong><\/h2>\n<p>buffer = io.BytesIO()<\/p>\n<h2><strong>\u63a5\u6536\u6570\u636e\u5e76\u5199\u5165\u7f13\u51b2\u533a<\/strong><\/h2>\n<p>data = sock.recv(1024)<\/p>\n<p>buffer.write(data)<\/p>\n<h2><strong>\u8bfb\u53d6\u7f13\u51b2\u533a\u4e2d\u7684\u6570\u636e<\/strong><\/h2>\n<p>print(buffer.getvalue())<\/p>\n<h2><strong>\u5173\u95ed socket<\/strong><\/h2>\n<p>sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5ba2\u6237\u7aef\u8fde\u63a5\u5230\u670d\u52a1\u5668\u540e\uff0c\u63a5\u6536\u5230\u7684\u6570\u636e\u88ab\u5199\u5165 <code>BytesIO<\/code> \u7f13\u51b2\u533a\uff0c\u7136\u540e\u4ece\u7f13\u51b2\u533a\u4e2d\u8bfb\u53d6\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u5904\u7406\u548c\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u7f13\u51b2\u533a\u53ef\u4ee5\u7528\u4e8e\u6682\u5b58\u6570\u636e\uff0c\u8fdb\u884c\u5404\u79cd\u8f6c\u6362\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>StringIO<\/code> \u7f13\u51b2\u533a\u5c06 CSV \u6570\u636e\u8f6c\u6362\u4e3a JSON \u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06 CSV \u6570\u636e\u8f6c\u6362\u4e3a JSON \u6570\u636e\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import io<\/p>\n<p>import csv<\/p>\n<p>import json<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a StringIO \u7f13\u51b2\u533a<\/strong><\/h2>\n<p>buffer = io.StringIO()<\/p>\n<h2><strong>\u5199\u5165 CSV \u6570\u636e<\/strong><\/h2>\n<p>csv_writer = csv.writer(buffer)<\/p>\n<p>csv_writer.writerow([&#39;name&#39;, &#39;age&#39;])<\/p>\n<p>csv_writer.writerow([&#39;Alice&#39;, 30])<\/p>\n<p>csv_writer.writerow([&#39;Bob&#39;, 25])<\/p>\n<h2><strong>\u8bfb\u53d6\u7f13\u51b2\u533a\u4e2d\u7684 CSV \u6570\u636e<\/strong><\/h2>\n<p>buffer.seek(0)<\/p>\n<p>csv_reader = csv.DictReader(buffer)<\/p>\n<h2><strong>\u5c06 CSV \u6570\u636e\u8f6c\u6362\u4e3a JSON \u6570\u636e<\/strong><\/h2>\n<p>json_data = json.dumps([row for row in csv_reader])<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cCSV \u6570\u636e\u88ab\u5199\u5165 <code>StringIO<\/code> \u7f13\u51b2\u533a\uff0c\u7136\u540e\u4ece\u7f13\u51b2\u533a\u4e2d\u8bfb\u53d6 CSV \u6570\u636e\u5e76\u8f6c\u6362\u4e3a JSON \u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u6570\u636e\u538b\u7f29\u548c\u89e3\u538b\u7f29<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u538b\u7f29\u548c\u89e3\u538b\u7f29\u8fc7\u7a0b\u4e2d\uff0c\u7f13\u51b2\u533a\u53ef\u4ee5\u7528\u4e8e\u6682\u5b58\u6570\u636e\uff0c\u907f\u514d\u9891\u7e41\u7684\u78c1\u76d8 I\/O \u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>BytesIO<\/code> \u7f13\u51b2\u533a\u8fdb\u884c\u6570\u636e\u538b\u7f29\u548c\u89e3\u538b\u7f29\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 <code>BytesIO<\/code> \u8fdb\u884c\u6570\u636e\u538b\u7f29\u548c\u89e3\u538b\u7f29\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import io<\/p>\n<p>import gzip<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a BytesIO \u7f13\u51b2\u533a<\/strong><\/h2>\n<p>buffer = io.BytesIO()<\/p>\n<h2><strong>\u5c06\u6570\u636e\u5199\u5165\u7f13\u51b2\u533a\u5e76\u8fdb\u884c\u538b\u7f29<\/strong><\/h2>\n<p>with gzip.GzipFile(fileobj=buffer, mode=&#39;w&#39;) as gz:<\/p>\n<p>    gz.write(b&#39;Hello, World!&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u538b\u7f29\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>compressed_data = buffer.getvalue()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 BytesIO \u7f13\u51b2\u533a<\/strong><\/h2>\n<p>buffer = io.BytesIO(compressed_data)<\/p>\n<h2><strong>\u89e3\u538b\u7f29\u6570\u636e\u5e76\u4ece\u7f13\u51b2\u533a\u4e2d\u8bfb\u53d6<\/strong><\/h2>\n<p>with gzip.GzipFile(fileobj=buffer, mode=&#39;r&#39;) as gz:<\/p>\n<p>    decompressed_data = gz.read()<\/p>\n<p>print(decompressed_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6570\u636e\u88ab\u5199\u5165 <code>BytesIO<\/code> \u7f13\u51b2\u533a\u5e76\u8fdb\u884c\u538b\u7f29\uff0c\u7136\u540e\u4ece\u7f13\u51b2\u533a\u4e2d\u8bfb\u53d6\u538b\u7f29\u6570\u636e\u5e76\u8fdb\u884c\u89e3\u538b\u7f29\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u7f13\u51b2\u533a<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u7f13\u51b2\u533a\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u7c7b\u578b\u6765\u9009\u62e9\u5408\u9002\u7684\u7f13\u51b2\u533a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9009\u62e9\u7f13\u51b2\u533a\u7684\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5904\u7406\u6587\u672c\u6570\u636e\u65f6<\/strong>\uff1a\u4f7f\u7528 <code>StringIO<\/code> \u7f13\u51b2\u533a\u3002<\/li>\n<li><strong>\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u65f6<\/strong>\uff1a\u4f7f\u7528 <code>BytesIO<\/code> \u7f13\u51b2\u533a\u3002<\/li>\n<li><strong>\u9700\u8981\u9ad8\u7ea7 I\/O \u64cd\u4f5c\u65f6<\/strong>\uff1a\u4f7f\u7528 <code>io<\/code> \u6a21\u5757\u4e2d\u7684\u5176\u4ed6\u7c7b\uff0c\u4f8b\u5982 <code>BufferedReader<\/code>\u3001<code>BufferedWriter<\/code> \u7b49\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5728 Python \u4e2d\u5c06\u6570\u636e\u8f6c\u5230\u7f13\u51b2\u533a\u7684\u4e3b\u8981\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528 <code>io<\/code> \u6a21\u5757\u3001<code>BytesIO<\/code> \u548c <code>StringIO<\/code> \u7b49\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\uff0c\u51cf\u5c11\u78c1\u76d8 I\/O \u64cd\u4f5c\u3002\u6b64\u5916\uff0c\u8fd8\u4ecb\u7ecd\u4e86\u7f13\u51b2\u533a\u7684\u5b9e\u9645\u5e94\u7528\u573a\u666f\u548c\u9009\u62e9\u5408\u9002\u7f13\u51b2\u533a\u7684\u5efa\u8bae\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5408\u7406\u4f7f\u7528\u7f13\u51b2\u533a\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\uff0c\u4f7f\u6570\u636e\u5904\u7406\u66f4\u52a0\u7b80\u4fbf\u548c\u9ad8\u6548\u3002\u65e0\u8bba\u662f\u8fdb\u884c\u7f51\u7edc\u6570\u636e\u4f20\u8f93\u3001\u6570\u636e\u5904\u7406\u548c\u8f6c\u6362\uff0c\u8fd8\u662f\u6570\u636e\u538b\u7f29\u548c\u89e3\u538b\u7f29\uff0c\u7f13\u51b2\u533a\u90fd\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528 Python \u4e2d\u7684\u7f13\u51b2\u533a\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u8bf7\u968f\u65f6\u4e0e\u6211\u8054\u7cfb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u7f13\u51b2\u533a\u7684\u6982\u5ff5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u7f13\u51b2\u533a\u662f\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u4e00\u5757\u533a\u57df\uff0c\u7528\u4e8e\u4e34\u65f6\u5b58\u50a8\u6570\u636e\uff0c\u4ee5\u4fbf\u4e8e\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u4f20\u8f93\u3002\u5728Python\u4e2d\uff0c\u7f13\u51b2\u533a\u901a\u5e38\u6d89\u53ca\u5230\u6587\u4ef6\u64cd\u4f5c\u3001\u7f51\u7edc\u4f20\u8f93\u548c\u6570\u636e\u6d41\u7b49\u573a\u666f\u3002\u901a\u8fc7\u4f7f\u7528\u7f13\u51b2\u533a\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u51cf\u5c11\u5bf9\u786c\u4ef6\u7684\u9891\u7e41\u8bbf\u95ee\uff0c\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u901f\u5ea6\u548c\u6548\u7387\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5185\u5b58\u7f13\u51b2\u533a\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u521b\u5efa\u548c\u4f7f\u7528\u5185\u5b58\u7f13\u51b2\u533a\uff0c\u4f8b\u5982\u4f7f\u7528<code>io<\/code>\u6a21\u5757\u4e2d\u7684<code>BytesIO<\/code>\u548c<code>StringIO<\/code>\u7c7b\u3002<code>BytesIO<\/code>\u7528\u4e8e\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u800c<code>StringIO<\/code>\u5219\u7528\u4e8e\u5904\u7406\u6587\u672c\u6570\u636e\u3002\u901a\u8fc7\u8fd9\u4e9b\u7c7b\uff0c\u60a8\u53ef\u4ee5\u5c06\u6570\u636e\u5199\u5165\u7f13\u51b2\u533a\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8bfb\u53d6\u51fa\u6765\uff0c\u907f\u514d\u76f4\u63a5\u5bf9\u6587\u4ef6\u6216\u7f51\u7edc\u8fdb\u884c\u64cd\u4f5c\uff0c\u63d0\u9ad8\u4e86\u6548\u7387\u3002<\/p>\n<p><strong>\u5728\u6570\u636e\u4f20\u8f93\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u7f13\u51b2\u533a\u7684\u6709\u6548\u6027\u548c\u5b89\u5168\u6027\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5408\u7406\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u6765\u786e\u4fdd\u6570\u636e\u7684\u6709\u6548\u6027\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u9002\u5f53\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u4ee5\u9632\u6b62\u5728\u6570\u636e\u8bfb\u5199\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef\u3002\u5bf9\u4e8e\u7f51\u7edc\u4f20\u8f93\uff0c\u4f7f\u7528\u52a0\u5bc6\u534f\u8bae\uff08\u5982SSL\/TLS\uff09\u53ef\u4ee5\u8fdb\u4e00\u6b65\u589e\u5f3a\u6570\u636e\u5b89\u5168\u6027\u3002\u5b9a\u671f\u6e05\u7406\u548c\u7ba1\u7406\u7f13\u51b2\u533a\u4e2d\u7684\u6570\u636e\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u548c\u6570\u636e\u635f\u574f\u4e5f\u662f\u5fc5\u8981\u7684\u6b65\u9aa4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528I\/O\u5e93\u5c06\u6570\u636e\u8f6c\u5230\u7f13\u51b2\u533a\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528io\u6a21\u5757\u3001BytesIO\u3001String [&hellip;]","protected":false},"author":3,"featured_media":1135346,"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\/1135338"}],"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=1135338"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135338\/revisions"}],"predecessor-version":[{"id":1135350,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135338\/revisions\/1135350"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1135346"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1135338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1135338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1135338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}