{"id":1074329,"date":"2025-01-08T11:33:37","date_gmt":"2025-01-08T03:33:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1074329.html"},"modified":"2025-01-08T11:33:39","modified_gmt":"2025-01-08T03:33:39","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96100g%e5%a4%a7%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1074329.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6100g\u5927\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104015\/eb89686d-c72f-4997-94fe-b311daa74309.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6100g\u5927\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8bfb\u53d6100G\u5927\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u751f\u6210\u5668\u3001\u5185\u5b58\u6620\u5c04\u3001\u5206\u5757\u8bfb\u53d6\u7b49\u3002<\/strong> \u63a8\u8350\u7684\u65b9\u6cd5\u662f<strong>\u4f7f\u7528\u751f\u6210\u5668<\/strong>\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u5728\u4e0d\u5360\u7528\u5927\u91cf\u5185\u5b58\u7684\u524d\u63d0\u4e0b\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u6587\u4ef6\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u751f\u6210\u5668\u8bfb\u53d6<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u5728\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u5185\u5b58\u4e2d\u9010\u884c\u5904\u7406\u6587\u4ef6\u5185\u5bb9\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u52a0\u8f7d\u6574\u4e2a\u6587\u4ef6\u3002\u8fd9\u6837\uff0c\u5373\u4f7f\u662f100G\u7684\u5927\u6587\u4ef6\uff0c\u4e5f\u4e0d\u4f1a\u5360\u7528\u8fc7\u591a\u7684\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_large_file(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        for line in file:<\/p>\n<p>            yield line<\/p>\n<p>file_path = &#39;large_file.txt&#39;<\/p>\n<p>for line in read_large_file(file_path):<\/p>\n<p>    # Process the line<\/p>\n<p>    print(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u5229\u7528\u4e86Python\u7684\u751f\u6210\u5668\u7279\u6027\uff0c\u901a\u8fc7<code>yield<\/code>\u5173\u952e\u5b57\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u5728\u4e8e\u5185\u5b58\u5360\u7528\u975e\u5e38\u4f4e\uff0c\u5373\u4f7f\u6587\u4ef6\u975e\u5e38\u5927\uff0c\u4e5f\u80fd\u987a\u5229\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5206\u5757\u8bfb\u53d6<\/h3>\n<\/p>\n<p><p>\u5206\u5757\u8bfb\u53d6\u4e5f\u662f\u5904\u7406\u5927\u6587\u4ef6\u7684\u5e38\u7528\u65b9\u6cd5\u3002\u5b83\u901a\u8fc7\u5206\u5757\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6574\u4e2a\u6587\u4ef6\uff0c\u4ece\u800c\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_file_in_chunks(file_path, chunk_size=1024):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        while True:<\/p>\n<p>            chunk = file.read(chunk_size)<\/p>\n<p>            if not chunk:<\/p>\n<p>                break<\/p>\n<p>            yield chunk<\/p>\n<p>file_path = &#39;large_file.txt&#39;<\/p>\n<p>for chunk in read_file_in_chunks(file_path):<\/p>\n<p>    # Process the chunk<\/p>\n<p>    print(chunk)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>read_file_in_chunks<\/code>\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u6587\u4ef6\u8def\u5f84\u548c\u5757\u5927\u5c0f\u4f5c\u4e3a\u53c2\u6570\u3002\u901a\u8fc7<code>yield<\/code>\u5173\u952e\u5b57\u9010\u5757\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u4ece\u800c\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5185\u5b58\u6620\u5c04<\/h3>\n<\/p>\n<p><p>\u5185\u5b58\u6620\u5c04\uff08Memory Mapping\uff09\u662f\u4e00\u79cd\u5c06\u6587\u4ef6\u5185\u5bb9\u6620\u5c04\u5230\u5185\u5b58\u7684\u6280\u672f\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u6587\u4ef6\u8bfb\u53d6\u901f\u5ea6\uff0c\u5e76\u4e14\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mmap<\/p>\n<p>def read_large_file_with_mmap(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        with mmap.mmap(file.fileno(), length=0, access=mmap.ACCESS_READ) as mmap_obj:<\/p>\n<p>            for line in iter(mmap_obj.readline, b&quot;&quot;):<\/p>\n<p>                yield line.decode(&#39;utf-8&#39;)<\/p>\n<p>file_path = &#39;large_file.txt&#39;<\/p>\n<p>for line in read_large_file_with_mmap(file_path):<\/p>\n<p>    # Process the line<\/p>\n<p>    print(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>mmap<\/code>\u6a21\u5757\u5c06\u6587\u4ef6\u5185\u5bb9\u6620\u5c04\u5230\u5185\u5b58\uff0c\u5e76\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u8fd9\u6837\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u8bfb\u53d6\u901f\u5ea6\uff0c\u8fd8\u51cf\u5c11\u4e86\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5de5\u5177\uff0c\u867d\u7136\u5b83\u901a\u5e38\u7528\u4e8e\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8bfb\u53d6\u5927\u6587\u4ef6\u3002Pandas\u7684<code>read_csv<\/code>\u51fd\u6570\u652f\u6301\u5206\u5757\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def read_large_file_with_pandas(file_path, chunk_size=100000):<\/p>\n<p>    for chunk in pd.read_csv(file_path, chunksize=chunk_size):<\/p>\n<p>        # Process the chunk<\/p>\n<p>        print(chunk)<\/p>\n<p>file_path = &#39;large_file.csv&#39;<\/p>\n<p>read_large_file_with_pandas(file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Pandas\u7684<code>read_csv<\/code>\u51fd\u6570\uff0c\u5e76\u8bbe\u7f6e\u4e86<code>chunksize<\/code>\u53c2\u6570\u6765\u5206\u5757\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u5728\u4e8e\u53ef\u4ee5\u5229\u7528Pandas\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u6765\u5904\u7406\u5927\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001Dask\u5e93<\/h3>\n<\/p>\n<p><p>Dask\u5e93\u662f\u4e00\u4e2a\u5e76\u884c\u8ba1\u7b97\u5e93\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u6570\u636e\u3002\u5b83\u53ef\u4ee5\u5c06\u5927\u6587\u4ef6\u5206\u6210\u591a\u4e2a\u5c0f\u5757\uff0c\u5e76\u5e76\u884c\u5904\u7406\u8fd9\u4e9b\u5c0f\u5757\uff0c\u4ece\u800c\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dask.dataframe as dd<\/p>\n<p>def read_large_file_with_dask(file_path):<\/p>\n<p>    df = dd.read_csv(file_path)<\/p>\n<p>    # Process the dataframe<\/p>\n<p>    df.compute()<\/p>\n<p>file_path = &#39;large_file.csv&#39;<\/p>\n<p>read_large_file_with_dask(file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Dask\u7684<code>read_csv<\/code>\u51fd\u6570\u8bfb\u53d6\u5927\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aDask DataFrame\u3002\u901a\u8fc7<code>compute<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5e76\u884c\u5904\u7406\u8fd9\u4e9b\u6570\u636e\uff0c\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5f02\u6b65\u8bfb\u53d6<\/h3>\n<\/p>\n<p><p>\u5f02\u6b65\u8bfb\u53d6\u662f\u5904\u7406\u5927\u6587\u4ef6\u7684\u53e6\u4e00\u79cd\u9ad8\u6548\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5728\u4e0d\u963b\u585e\u4e3b\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u4ece\u800c\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import <a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ofiles<\/p>\n<p>import asyncio<\/p>\n<p>async def read_large_file(file_path):<\/p>\n<p>    async with aiofiles.open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        async for line in file:<\/p>\n<p>            # Process the line<\/p>\n<p>            print(line)<\/p>\n<p>file_path = &#39;large_file.txt&#39;<\/p>\n<p>asyncio.run(read_large_file(file_path))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>aiofiles<\/code>\u5e93\u8fdb\u884c\u5f02\u6b65\u6587\u4ef6\u8bfb\u53d6\u3002\u901a\u8fc7<code>async<\/code>\u548c<code>await<\/code>\u5173\u952e\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u963b\u585e\u4e3b\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5904\u7406100G\u5927\u6587\u4ef6\u5728Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u751f\u6210\u5668\u3001\u5206\u5757\u8bfb\u53d6\u3001\u5185\u5b58\u6620\u5c04\u3001Pandas\u5e93\u3001Dask\u5e93\u548c\u5f02\u6b65\u8bfb\u53d6\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u63a8\u8350\u4f7f\u7528\u751f\u6210\u5668<\/strong>\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u5728\u4e0d\u5360\u7528\u5927\u91cf\u5185\u5b58\u7684\u524d\u63d0\u4e0b\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u6587\u4ef6\u7684\u573a\u666f\u3002\u5f53\u7136\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5927\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6709\u6548\u8bfb\u53d6\u5927\u578b\u6587\u4ef6\uff1f<\/strong><br \/>\u5728\u5904\u7406100GB\u7684\u5927\u6587\u4ef6\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u9010\u884c\u8bfb\u53d6\u6216\u5206\u5757\u8bfb\u53d6\u7684\u65b9\u6cd5\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u7ed3\u5408\u5faa\u73af\uff0c\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u4ece\u800c\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;large_file.txt&#39;, &#39;r&#39;) as file:\n    for line in file:\n        # \u5904\u7406\u6bcf\u4e00\u884c\n<\/code><\/pre>\n<p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>read_csv()<\/code>\u51fd\u6570\uff0c\u5e76\u8bbe\u7f6e<code>chunksize<\/code>\u53c2\u6570\uff0c\u8fd9\u6837\u53ef\u4ee5\u5206\u5757\u8bfb\u53d6\u6570\u636e\uff0c\u9002\u5408\u5904\u7406\u8f83\u5927\u7684\u6570\u636e\u96c6\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\u5e94\u8be5\u6ce8\u610f\u54ea\u4e9b\u6027\u80fd\u4f18\u5316\uff1f<\/strong><br \/>\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\uff0cI\/O\u64cd\u4f5c\u5f80\u5f80\u662f\u74f6\u9888\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u6765\u4f18\u5316\u6027\u80fd\uff1a  <\/p>\n<ol>\n<li>\u4f7f\u7528<code>mmap<\/code>\u6a21\u5757\u5c06\u6587\u4ef6\u6620\u5c04\u5230\u5185\u5b58\u4e2d\uff0c\u8fd9\u6837\u53ef\u4ee5\u52a0\u5feb\u6587\u4ef6\u7684\u8bfb\u53d6\u901f\u5ea6\u3002  <\/li>\n<li>\u8c03\u6574\u6587\u4ef6\u7684\u8bfb\u53d6\u7f13\u51b2\u533a\u5927\u5c0f\uff0c\u589e\u5927\u7f13\u51b2\u533a\u53ef\u4ee5\u51cf\u5c11\u8bfb\u53d6\u6b21\u6570\uff0c\u63d0\u9ad8\u6548\u7387\u3002  <\/li>\n<li>\u5982\u679c\u6587\u4ef6\u683c\u5f0f\u652f\u6301\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u901a\u5e38\u8bfb\u53d6\u901f\u5ea6\u4f1a\u66f4\u5feb\u3002<\/li>\n<\/ol>\n<p><strong>\u5728\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5185\u5b58\u4e0d\u8db3\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u5f53\u5185\u5b58\u4e0d\u8db3\u65f6\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u7b56\u7565\uff1a  <\/p>\n<ol>\n<li>\u5206\u6279\u6b21\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\uff0c\u5904\u7406\u5b8c\u4e00\u6279\u540e\u518d\u8bfb\u53d6\u4e0b\u4e00\u6279\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6574\u4e2a\u6587\u4ef6\u3002  <\/li>\n<li>\u4f7f\u7528<code>dask<\/code>\u5e93\uff0c\u5b83\u80fd\u591f\u5904\u7406\u8d85\u51fa\u5185\u5b58\u9650\u5236\u7684\u6570\u636e\u96c6\uff0c\u63d0\u4f9b\u7c7b\u4f3c\u4e8e<code>pandas<\/code>\u7684API\u3002  <\/li>\n<li>\u5c06\u6570\u636e\u5199\u5165\u6570\u636e\u5e93\uff0c\u800c\u4e0d\u662f\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff0c\u5904\u7406\u5b8c\u6210\u540e\u518d\u8fdb\u884c\u67e5\u8be2\u548c\u5206\u6790\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bfb\u53d6100G\u5927\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u751f\u6210\u5668\u3001\u5185\u5b58\u6620\u5c04\u3001\u5206\u5757\u8bfb\u53d6\u7b49\u3002 \u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u751f\u6210\u5668\uff0c\u56e0\u4e3a\u5b83 [&hellip;]","protected":false},"author":3,"featured_media":1074338,"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\/1074329"}],"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=1074329"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074329\/revisions"}],"predecessor-version":[{"id":1074340,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074329\/revisions\/1074340"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1074338"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1074329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1074329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1074329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}