{"id":1118028,"date":"2025-01-08T18:31:30","date_gmt":"2025-01-08T10:31:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1118028.html"},"modified":"2025-01-08T18:31:32","modified_gmt":"2025-01-08T10:31:32","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e4%b8%80%e4%b8%aa%e5%a4%a7%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1118028.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u4e00\u4e2a\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\/25081700\/06cdb999-eeb6-473e-8a94-8564dd6c992e.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u4e00\u4e2a\u5927\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6\u5927\u6587\u4ef6\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528\u751f\u6210\u5668\u3001\u9010\u884c\u8bfb\u53d6\u3001\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6280\u672f\uff08mmap\uff09\u3001\u4f7f\u7528Pandas\u5e93\u8bfb\u53d6<\/strong>\u3002\u5176\u4e2d\uff0c\u9010\u884c\u8bfb\u53d6\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u9010\u884c\u8bfb\u53d6\u80fd\u591f\u5728\u4e0d\u5360\u7528\u5927\u91cf\u5185\u5b58\u7684\u524d\u63d0\u4e0b\uff0c\u9010\u6b65\u5904\u7406\u6587\u4ef6\u5185\u5bb9\uff0c\u9002\u5408\u5904\u7406\u8d85\u5927\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u9010\u884c\u8bfb\u53d6\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a\u901a\u8fc7\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u5185\u5b58\u4f7f\u7528\uff0c\u56e0\u4e3a\u6bcf\u6b21\u53ea\u52a0\u8f7d\u4e00\u884c\u6570\u636e\uff0c\u800c\u4e0d\u662f\u6574\u4e2a\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u90a3\u4e9b\u65e0\u6cd5\u4e00\u6b21\u6027\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u7684\u5927\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u9010\u884c\u8bfb\u53d6\u4e00\u4e2a\u5927\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;large_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        process(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684<code>process(line)<\/code>\u51fd\u6570\u4ee3\u8868\u5bf9\u8bfb\u53d6\u5230\u7684\u6bcf\u4e00\u884c\u6570\u636e\u8fdb\u884c\u5904\u7406\u7684\u64cd\u4f5c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u4f46\u5374\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u8ba9\u4f60\u5728\u9700\u8981\u65f6\u751f\u6210\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u5168\u90e8\u751f\u6210\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u5927\u6587\u4ef6\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8282\u7701\u5185\u5b58\u5e76\u63d0\u9ad8\u6548\u7387\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>for line in read_large_file(&#39;large_file.txt&#39;):<\/p>\n<p>    process(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>read_large_file<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\uff0c\u6bcf\u6b21\u8c03\u7528\u90fd\u4f1a\u751f\u6210\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\u5185\u5bb9\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u9010\u884c\u5904\u7406\u5927\u6587\u4ef6\uff0c\u800c\u4e0d\u4f1a\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u9010\u884c\u8bfb\u53d6<\/h3>\n<\/p>\n<p><p>\u9010\u884c\u8bfb\u53d6\u662f\u5904\u7406\u5927\u6587\u4ef6\u7684\u53e6\u4e00\u79cd\u5e38\u7528\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>open<\/code>\u548c<code>readline<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;large_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    while True:<\/p>\n<p>        line = file.readline()<\/p>\n<p>        if not line:<\/p>\n<p>            break<\/p>\n<p>        process(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>file.readline()<\/code>\u51fd\u6570\u6bcf\u6b21\u53ea\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\u5185\u5bb9\uff0c\u5982\u679c\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u884c\u90fd\u5df2\u7ecf\u8bfb\u53d6\u5b8c\u6bd5\uff0c<code>readline<\/code>\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\uff0c\u8fd9\u6837\u53ef\u4ee5\u901a\u8fc7<code>if not line: break<\/code>\u8bed\u53e5\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6280\u672f\uff08mmap\uff09<\/h3>\n<\/p>\n<p><p>\u5185\u5b58\u6620\u5c04\u6280\u672f\uff08mmap\uff09\u662f\u4e00\u79cd\u9ad8\u7ea7\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u4f60\u5c06\u6587\u4ef6\u7684\u5185\u5bb9\u76f4\u63a5\u6620\u5c04\u5230\u5185\u5b58\u4e2d\uff0c\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u50cf\u64cd\u4f5c\u5185\u5b58\u4e00\u6837\u64cd\u4f5c\u6587\u4ef6\u5185\u5bb9\u3002\u4f7f\u7528mmap\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6587\u4ef6\u8bfb\u53d6\u7684\u901f\u5ea6\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u8d85\u5927\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mmap<\/p>\n<p>with open(&#39;large_file.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>        process(line)<\/p>\n<p>    mmapped_file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>mmap.mmap<\/code>\u51fd\u6570\u5c06\u6587\u4ef6\u5185\u5bb9\u6620\u5c04\u5230\u5185\u5b58\u4e2d\uff0c\u4f60\u53ef\u4ee5\u50cf\u64cd\u4f5c\u5b57\u8282\u6570\u7ec4\u4e00\u6837\u64cd\u4f5c\u6587\u4ef6\u5185\u5bb9\u3002<code>iter(mmapped_file.readline, b&quot;&quot;)<\/code>\u4f1a\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u76f4\u5230\u6587\u4ef6\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Pandas\u5e93\u8bfb\u53d6<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8f7b\u677e\u5904\u7406\u5927\u6587\u4ef6\u3002\u867d\u7136Pandas\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5927\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>chunksize = 106<\/p>\n<p>for chunk in pd.read_csv(&#39;large_file.csv&#39;, chunksize=chunksize):<\/p>\n<p>    process(chunk)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>pd.read_csv<\/code>\u51fd\u6570\u4f1a\u5c06\u6587\u4ef6\u6309\u5757\u8bfb\u53d6\uff0c\u6bcf\u6b21\u8bfb\u53d6\u4e00\u4e2a\u5927\u5c0f\u4e3a<code>chunksize<\/code>\u7684\u5757\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6574\u4e2a\u6587\u4ef6\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8d85\u5927\u6587\u4ef6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6280\u672f\u6765\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6587\u4ef6\u5185\u5bb9\u53ef\u4ee5\u5e76\u884c\u5904\u7406\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>\u591a\u7ebf\u7a0b\u5904\u7406<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def process_chunk(chunk):<\/p>\n<p>    for line in chunk:<\/p>\n<p>        process(line)<\/p>\n<p>with open(&#39;large_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    threads = []<\/p>\n<p>    while True:<\/p>\n<p>        chunk = list(file.readline() for _ in range(1000))<\/p>\n<p>        if not chunk:<\/p>\n<p>            break<\/p>\n<p>        thread = threading.Thread(target=process_chunk, args=(chunk,))<\/p>\n<p>        threads.append(thread)<\/p>\n<p>        thread.start()<\/p>\n<p>    for thread in threads:<\/p>\n<p>        thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u591a\u8fdb\u7a0b\u5904\u7406<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def process_chunk(chunk):<\/p>\n<p>    for line in chunk:<\/p>\n<p>        process(line)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    with open(&#39;large_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>        pool = multiprocessing.Pool()<\/p>\n<p>        while True:<\/p>\n<p>            chunk = list(file.readline() for _ in range(1000))<\/p>\n<p>            if not chunk:<\/p>\n<p>                break<\/p>\n<p>            pool.apply_async(process_chunk, args=(chunk,))<\/p>\n<p>        pool.close()<\/p>\n<p>        pool.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5904\u7406\u5927\u6587\u4ef6\u7684\u901f\u5ea6\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u7684\u7ba1\u7406\uff0c\u4ee5\u53ca\u6570\u636e\u7684\u540c\u6b65\u548c\u5171\u4eab\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Dask\u5e93\u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>Dask\u662f\u4e00\u4e2a\u5e76\u884c\u8ba1\u7b97\u5e93\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8f7b\u677e\u5904\u7406\u5927\u6587\u4ef6\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5e76\u884c\u5904\u7406\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dask.dataframe as dd<\/p>\n<p>df = dd.read_csv(&#39;large_file.csv&#39;)<\/p>\n<p>df = df.map_partitions(process)<\/p>\n<p>df.compute()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>dd.read_csv<\/code>\u51fd\u6570\u4f1a\u5c06\u6587\u4ef6\u6309\u5757\u8bfb\u53d6\uff0c\u5e76\u884c\u5904\u7406\u6bcf\u4e2a\u5757\u3002<code>df.map_partitions(process)<\/code>\u4f1a\u5c06\u5904\u7406\u51fd\u6570\u5e94\u7528\u5230\u6bcf\u4e2a\u5757\u4e0a\uff0c<code>df.compute()<\/code>\u4f1a\u89e6\u53d1\u8ba1\u7b97\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528HDF5\u683c\u5f0f\u5b58\u50a8\u548c\u8bfb\u53d6\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>HDF5\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u548c\u7ba1\u7406\u6570\u636e\u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u5904\u7406\u5927\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>h5py<\/code>\u5e93\u6765\u8bfb\u53d6\u548c\u5199\u5165HDF5\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import h5py<\/p>\n<p>with h5py.File(&#39;large_file.h5&#39;, &#39;r&#39;) as file:<\/p>\n<p>    dataset = file[&#39;dataset_name&#39;]<\/p>\n<p>    for data in dataset:<\/p>\n<p>        process(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>h5py.File<\/code>\u51fd\u6570\u4f1a\u6253\u5f00HDF5\u6587\u4ef6\uff0c<code>file[&#39;dataset_name&#39;]<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6570\u636e\u96c6\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u50cf\u64cd\u4f5c\u6570\u7ec4\u4e00\u6837\u64cd\u4f5c\u6570\u636e\u96c6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528Parquet\u683c\u5f0f\u5b58\u50a8\u548c\u8bfb\u53d6\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>Parquet\u662f\u4e00\u79cd\u5217\u5f0f\u5b58\u50a8\u683c\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u5904\u7406\u5927\u6587\u4ef6\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u7ed3\u6784\u5316\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>pyarrow<\/code>\u5e93\u6765\u8bfb\u53d6\u548c\u5199\u5165Parquet\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyarrow.parquet as pq<\/p>\n<p>table = pq.read_table(&#39;large_file.parquet&#39;)<\/p>\n<p>for batch in table.to_batches():<\/p>\n<p>    process(batch)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>pq.read_table<\/code>\u51fd\u6570\u4f1a\u8bfb\u53d6Parquet\u6587\u4ef6\u5e76\u8fd4\u56de\u4e00\u4e2a\u8868\u5bf9\u8c61\uff0c<code>table.to_batches()<\/code>\u4f1a\u5c06\u8868\u5185\u5bb9\u6309\u5757\u8fd4\u56de\uff0c\u4f60\u53ef\u4ee5\u9010\u5757\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528SQLite\u6570\u636e\u5e93\u5b58\u50a8\u548c\u8bfb\u53d6\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>SQLite\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u8bfb\u53d6\u5927\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>sqlite3<\/code>\u5e93\u6765\u64cd\u4f5cSQLite\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;large_file.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>cursor.execute(&#39;SELECT * FROM table_name&#39;)<\/p>\n<p>while True:<\/p>\n<p>    rows = cursor.fetchmany(1000)<\/p>\n<p>    if not rows:<\/p>\n<p>        break<\/p>\n<p>    for row in rows:<\/p>\n<p>        process(row)<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>sqlite3.connect<\/code>\u51fd\u6570\u4f1a\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93\uff0c<code>cursor.execute<\/code>\u51fd\u6570\u4f1a\u6267\u884cSQL\u67e5\u8be2\uff0c<code>cursor.fetchmany<\/code>\u51fd\u6570\u4f1a\u6309\u5757\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\uff0c\u4f60\u53ef\u4ee5\u9010\u5757\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528Apache Spark\u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>Apache Spark\u662f\u4e00\u4e2a\u5206\u5e03\u5f0f\u8ba1\u7b97\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u5904\u7406\u5927\u6587\u4ef6\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5206\u5e03\u5f0f\u5904\u7406\u7684\u60c5\u51b5\u4e0b\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>pyspark<\/code>\u5e93\u6765\u64cd\u4f5cSpark\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pyspark.sql import SparkSession<\/p>\n<p>spark = SparkSession.builder.appName(&#39;large_file_processing&#39;).getOrCreate()<\/p>\n<p>df = spark.read.csv(&#39;large_file.csv&#39;)<\/p>\n<p>df = df.rdd.map(process)<\/p>\n<p>df.collect()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>spark.read.csv<\/code>\u51fd\u6570\u4f1a\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u8fd4\u56de\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c<code>df.rdd.map(process)<\/code>\u4f1a\u5c06\u5904\u7406\u51fd\u6570\u5e94\u7528\u5230\u6bcf\u4e2a\u8bb0\u5f55\u4e0a\uff0c<code>df.collect()<\/code>\u4f1a\u89e6\u53d1\u8ba1\u7b97\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u9ad8\u6548\u5730\u8bfb\u53d6\u548c\u5904\u7406\u5927\u6587\u4ef6\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6587\u4ef6\u7c7b\u578b\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u548c\u6548\u7387\u3002\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u8bb0\u4f4f\u8981\u8003\u8651\u5185\u5b58\u4f7f\u7528\u548c\u5904\u7406\u6548\u7387\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6574\u4e2a\u6587\u4ef6\u5230\u5185\u5b58\u4e2d\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u4f60\u5728Python\u4e2d\u9ad8\u6548\u5730\u8bfb\u53d6\u548c\u5904\u7406\u5927\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9ad8\u6548\u8bfb\u53d6\u5927\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\u53ef\u4ee5\u4f7f\u7528\u9010\u884c\u8bfb\u53d6\u7684\u65b9\u5f0f\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4e00\u6b21\u6027\u5c06\u6574\u4e2a\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528<code>with open(filename, &#39;r&#39;) as file:<\/code>\u8bed\u53e5\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7<code>for line in file:<\/code>\u9010\u884c\u8bfb\u53d6\u5185\u5bb9\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u8282\u7701\u5185\u5b58\uff0c\u8fd8\u80fd\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u5e93\u53ef\u4ee5\u9009\u62e9\uff1f<\/strong><br \/>\u9664\u4e86\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>pandas<\/code>\u548c<code>dask<\/code>\u3002<code>pandas<\/code>\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u573a\u666f\uff0c\u800c<code>dask<\/code>\u5219\u80fd\u591f\u5904\u7406\u66f4\u5927\u89c4\u6a21\u7684\u6570\u636e\u96c6\uff0c\u652f\u6301\u5e76\u884c\u8ba1\u7b97\uff0c\u9002\u5408\u4e8e\u8d85\u51fa\u5185\u5b58\u9650\u5236\u7684\u5927\u6587\u4ef6\u8bfb\u53d6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u6216\u7f16\u7801\u9519\u8bef\u7b49\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u5757\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>try: ... except FileNotFoundError: ...<\/code>\u6765\u5904\u7406\u6587\u4ef6\u672a\u627e\u5230\u7684\u9519\u8bef\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u5728\u8bfb\u53d6\u65f6\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\uff0c\u5982<code>open(filename, &#39;r&#39;, encoding=&#39;utf-8&#39;)<\/code>\uff0c\u4ee5\u907f\u514d\u7f16\u7801\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6\u5927\u6587\u4ef6\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528\u751f\u6210\u5668\u3001\u9010\u884c\u8bfb\u53d6\u3001\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6280\u672f\uff08mmap\uff09\u3001\u4f7f\u7528Pandas\u5e93\u8bfb\u53d6\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1118033,"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\/1118028"}],"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=1118028"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118028\/revisions"}],"predecessor-version":[{"id":1118035,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118028\/revisions\/1118035"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1118033"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1118028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1118028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1118028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}