{"id":1119316,"date":"2025-01-08T18:43:35","date_gmt":"2025-01-08T10:43:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1119316.html"},"modified":"2025-01-08T18:43:37","modified_gmt":"2025-01-08T10:43:37","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%88%97%e8%a1%a8%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6%e5%a4%a7%e5%b0%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1119316.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5217\u8868\u5199\u5165\u6587\u4ef6\u5927\u5c0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082444\/b6e21d4f-73cb-42a6-b7af-1ea1f8272b01.webp\" alt=\"python\u5982\u4f55\u5c06\u5217\u8868\u5199\u5165\u6587\u4ef6\u5927\u5c0f\" \/><\/p>\n<p><p> <strong>Python \u5c06\u5217\u8868\u5199\u5165\u6587\u4ef6\u7684\u51e0\u79cd\u65b9\u6cd5\u3001\u4f7f\u7528\u4e0d\u540c\u7684\u6587\u4ef6\u6a21\u5f0f\u3001\u5904\u7406\u5927\u6587\u4ef6\u7684\u6280\u5de7<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u5217\u8868\u5199\u5165\u6587\u4ef6\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u5c24\u5176\u5728\u6570\u636e\u5904\u7406\u548c\u5b58\u50a8\u65f6\u3002<strong>\u4f7f\u7528write()\u65b9\u6cd5\u3001\u4f7f\u7528writelines()\u65b9\u6cd5\u3001\u4f7f\u7528pickle\u6a21\u5757\u3001\u4f7f\u7528json\u6a21\u5757<\/strong>\u90fd\u662f\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u6df1\u5165\u63a2\u8ba8\u5904\u7406\u5927\u6587\u4ef6\u7684\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528write()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>write()\u65b9\u6cd5\u9002\u7528\u4e8e\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u9010\u884c\u5199\u5165\u6587\u4ef6\u3002\u9996\u5148\u6211\u4eec\u9700\u8981\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u7136\u540e\u9010\u884c\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for item in my_list:<\/p>\n<p>        file.write(f&quot;{item}\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u5927\u6587\u4ef6\u64cd\u4f5c\uff0c\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528writelines()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>writelines()\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u5c06\u5217\u8868\u5199\u5165\u6587\u4ef6\uff0c\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u5faa\u73af\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.writelines(f&quot;{item}\\n&quot; for item in my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u76f8\u5bf9write()\u65b9\u6cd5\u66f4\u7b80\u6d01\uff0c\u4f46\u540c\u6837\u9002\u7528\u4e8e\u8f83\u5c0f\u7684\u6570\u636e\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528pickle\u6a21\u5757<\/p>\n<\/p>\n<p><p>pickle\u6a21\u5757\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\u5e8f\u5217\u5316\u540e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u6301\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>with open(&#39;output.pkl&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    pickle.dump(my_list, file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u4fdd\u5b58\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u573a\u666f\uff0c\u4f46\u751f\u6210\u7684\u6587\u4ef6\u662f\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u4e0d\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528json\u6a21\u5757<\/p>\n<\/p>\n<p><p>json\u6a21\u5757\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u5e76\u5199\u5165\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u4fdd\u5b58\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5e76\u4e14\u751f\u6210\u7684\u6587\u4ef6\u53ef\u8bfb\u6027\u5f3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>with open(&#39;output.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(my_list, file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u751f\u6210\u7684\u6587\u4ef6\u6613\u4e8e\u9605\u8bfb\u548c\u4ea4\u6362\uff0c\u9002\u7528\u4e8e\u4fdd\u5b58\u7ed3\u6784\u5316\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u5927\u6587\u4ef6\u7684\u6280\u5de7<\/p>\n<\/p>\n<p><p>1\u3001\u5206\u6279\u5199\u5165<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6587\u4ef6\u64cd\u4f5c\uff0c\u5206\u6279\u5199\u5165\u53ef\u4ee5\u6709\u6548\u964d\u4f4e\u5185\u5b58\u4f7f\u7528\u91cf\u3002\u53ef\u4ee5\u5c06\u5217\u8868\u5206\u5272\u4e3a\u591a\u4e2a\u5b50\u5217\u8868\uff0c\u9010\u4e2a\u5b50\u5217\u8868\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;apple&#39;] * 1000000  # \u793a\u4f8b\u5927\u5217\u8868<\/p>\n<p>chunk_size = 10000<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for i in range(0, len(my_list), chunk_size):<\/p>\n<p>        chunk = my_list[i:i + chunk_size]<\/p>\n<p>        file.writelines(f&quot;{item}\\n&quot; for item in chunk)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528\u751f\u6210\u5668<\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u53ef\u4ee5\u52a8\u6001\u751f\u6210\u6570\u636e\uff0c\u907f\u514d\u5c06\u6574\u4e2a\u5217\u8868\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u9002\u7528\u4e8e\u6570\u636e\u91cf\u975e\u5e38\u5927\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_data():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        yield f&quot;item_{i}\\n&quot;<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.writelines(generate_data())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u4f7f\u7528\u5185\u5b58\u6620\u5c04<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8d85\u5927\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6280\u672f\uff0c\u5c06\u6587\u4ef6\u7684\u4e00\u90e8\u5206\u6620\u5c04\u5230\u5185\u5b58\u4e2d\uff0c\u8fdb\u884c\u5206\u5757\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mmap<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;r+b&#39;) as file:<\/p>\n<p>    mmapped_file = mmap.mmap(file.fileno(), 0)<\/p>\n<p>    for line in iter(mmapped_file.readline, b&quot;&quot;):<\/p>\n<p>        print(line.decode(&#39;utf-8&#39;))<\/p>\n<p>    mmapped_file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u4e0d\u540c\u6587\u4ef6\u6a21\u5f0f\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u6a21\u5f0f\u975e\u5e38\u91cd\u8981\u3002\u5e38\u89c1\u7684\u6587\u4ef6\u6a21\u5f0f\u6709\uff1a<\/p>\n<\/p>\n<ul>\n<li>&#39;w&#39;\uff1a\u5199\u5165\u6a21\u5f0f\uff0c\u5982\u679c\u6587\u4ef6\u5b58\u5728\u5219\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9<\/li>\n<li>&#39;a&#39;\uff1a\u8ffd\u52a0\u6a21\u5f0f\uff0c\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9<\/li>\n<li>&#39;r&#39;\uff1a\u8bfb\u53d6\u6a21\u5f0f\uff0c\u53ea\u8bfb\u6587\u4ef6\u5185\u5bb9<\/li>\n<li>&#39;wb&#39;\uff1a\u4e8c\u8fdb\u5236\u5199\u5165\u6a21\u5f0f<\/li>\n<li>&#39;rb&#39;\uff1a\u4e8c\u8fdb\u5236\u8bfb\u53d6\u6a21\u5f0f<\/li>\n<\/ul>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u6a21\u5f0f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6587\u4ef6\u64cd\u4f5c\u7684\u6548\u7387\u548c\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u9519\u8bef\u5904\u7406\u548c<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a><\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u9519\u8bef\u5904\u7406\u548c\u8d44\u6e90\u7ba1\u7406\u81f3\u5173\u91cd\u8981\u3002\u4f7f\u7528try-except\u8bed\u53e5\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u4f7f\u7528with\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>try:<\/p>\n<p>    with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>        for item in my_list:<\/p>\n<p>            file.write(f&quot;{item}\\n&quot;)<\/p>\n<p>except IOError as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5c06\u5217\u8868\u5199\u5165\u6587\u4ef6\uff0c\u6bcf\u79cd\u65b9\u6cd5\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u6839\u636e\u6570\u636e\u89c4\u6a21\u548c\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u5206\u6279\u5199\u5165\u3001\u751f\u6210\u5668\u548c\u5185\u5b58\u6620\u5c04\u7b49\u6280\u5de7\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u6587\u4ef6\u64cd\u4f5c\u7684\u6548\u7387\u548c\u53ef\u9760\u6027\u3002\u540c\u65f6\uff0c\u6b63\u786e\u9009\u62e9\u6587\u4ef6\u6a21\u5f0f\u548c\u8fdb\u884c\u9519\u8bef\u5904\u7406\u4e0e\u8d44\u6e90\u7ba1\u7406\uff0c\u80fd\u591f\u786e\u4fdd\u6587\u4ef6\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u4e2d\u7684\u5217\u8868\u4fdd\u5b58\u4e3a\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u7ed3\u5408<code>write()<\/code>\u65b9\u6cd5\u5c06\u5217\u8868\u5199\u5165\u6587\u4ef6\u3002\u9996\u5148\uff0c\u9700\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u4f8b\u5982\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\uff0c\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u7528\u6362\u884c\u7b26\u6216\u5176\u4ed6\u5206\u9694\u7b26\u5206\u9694\u3002\u7136\u540e\uff0c\u5c06\u5904\u7406\u540e\u7684\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]\nwith open(&#39;output.txt&#39;, &#39;w&#39;) as f:\n    f.write(&#39;\\n&#39;.join(my_list))\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5c06\u5305\u542b\u590d\u6742\u5bf9\u8c61\u7684\u5217\u8868\u5199\u5165\u6587\u4ef6\uff1f<\/strong><br \/>\u82e5\u5217\u8868\u4e2d\u5305\u542b\u590d\u6742\u5bf9\u8c61\uff0c\u5982\u5b57\u5178\u6216\u81ea\u5b9a\u4e49\u7c7b\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u5c06\u5176\u5e8f\u5217\u5316\u4e3aJSON\u683c\u5f0f\u3002\u4f7f\u7528<code>json.dump()<\/code>\u53ef\u4ee5\u76f4\u63a5\u5c06\u5217\u8868\u5199\u5165\u6587\u4ef6\uff0c\u800c\u65e0\u9700\u624b\u52a8\u8f6c\u6362\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\nmy_list = [{&#39;name&#39;: &#39;apple&#39;, &#39;color&#39;: &#39;red&#39;}, {&#39;name&#39;: &#39;banana&#39;, &#39;color&#39;: &#39;yellow&#39;}]\nwith open(&#39;output.json&#39;, &#39;w&#39;) as f:\n    json.dump(my_list, f)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u5217\u8868\u6570\u636e\uff1f<\/strong><br \/>\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u5217\u8868\u6570\u636e\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u548c<code>readlines()<\/code>\u65b9\u6cd5\u3002\u5c06\u6bcf\u4e00\u884c\u8bfb\u53d6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strip()<\/code>\u65b9\u6cd5\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u767d\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u5b58\u5165\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;r&#39;) as f:\n    my_list = [line.strip() for line in f.readlines()]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6bcf\u884c\u5305\u542b\u4e00\u4e2a\u5217\u8868\u5143\u7d20\u7684\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5c06\u5217\u8868\u5199\u5165\u6587\u4ef6\u7684\u51e0\u79cd\u65b9\u6cd5\u3001\u4f7f\u7528\u4e0d\u540c\u7684\u6587\u4ef6\u6a21\u5f0f\u3001\u5904\u7406\u5927\u6587\u4ef6\u7684\u6280\u5de7 \u5728Python\u4e2d\uff0c\u5c06\u5217\u8868\u5199\u5165\u6587\u4ef6 [&hellip;]","protected":false},"author":3,"featured_media":1119327,"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\/1119316"}],"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=1119316"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119316\/revisions"}],"predecessor-version":[{"id":1119328,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119316\/revisions\/1119328"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1119327"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1119316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1119316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1119316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}