Skip to main content
 首页 » 编程设计

python之使用 pandas 加载过滤后的 .tda 文件的最简单方法是什么

2025年05月04日8dflying

Pandas 有优秀的.read_table() 函数,但是大文件会导致MemoryError。
因为我只需要加载满足特定条件的行,所以我正在寻找一种方法来只加载那些。

这可以使用临时文件来完成:

with open(hugeTdaFile) as huge: 
    with open(hugeTdaFile + ".partial.tmp", "w") as tmp: 
        tmp.write(huge.readline())  # the header line 
        for line in huge: 
            if SomeCondition(line): 
                tmp.write(line) 
 
t = pandas.read_table(tmp.name) 

有没有办法避免这样使用临时文件?

请您参考如下方法:

您可以使用 chunksize 参数返回一个迭代器

看到这个:http://pandas.pydata.org/pandas-docs/stable/io.html#iterating-through-files-chunk-by-chunk

  • 根据需要过滤 block 帧
  • 将过滤后的内容附加到列表中
  • 最后连接

(或者你可以将它们写到新的 csvs 或 HDFStores 或其他什么)