如何从大型文本文件(> 60GB)中删除所有包含Python中特定字母的行?

问题描述 投票:0回答:1

我有一个大文本文件(> 60GB),我想从其中删除某些行。

文本文件包含:

352_0M, 352_1M,  0.913
500_1F, 452_0M,  0.500
870_0M, 400_1F,  0.980
601_1F, 470_0M,  0.630
845_0M, 900_1M,  0.456
100_1F, 250_0F,  0.123
...

我想删除第一列或第二列或两者中所有包含“ F”字母的行。预期的输出是:

352_0M, 352_1M,  0.913
845_0M, 900_1M,  0.456

如何在python中做到这一点?

python
1个回答
2
投票
with open('input_file','r') as inf:
    with open('output_file','w') as outf:
        for line in inf:
            if not any('F' in x for x in line.split(',', 2)[:2]):
                outf.write(line)
© www.soinside.com 2019 - 2024. All rights reserved.