使用 Python 删除文件中的行

Deleting rows in a file using Python

我有输入文件 "input.dat" 包含一些像这样的值:

41611   2014    12  18  0   0
41615   2014    12  18  0   0
41625   2014    12  18  0   0
41640   2014    6   14  3   3
42248   2014    12  18  0   0
42323   2014    12  18  0   0
42330   2014    8   13  7   7
42334   2014    12  18  0   0
42335   2014    12  18  0   0
...

我有很多数据集文件,但似乎有很多不需要的数据 如何立即删除此案例 41640 和 42330 的许多行及其整个行值。现在我使用了这个脚本:

with open(path+fname,"r") as input:
    with open("00-new.dat","wb") as output: 
        for line in input:
            if line!="41640"+"\n":
                output.write(line)

结果:数据41640仍然存在于输出中。有什么想法吗??

您需要更改您的条件 - 现在它检查整行是否等于 41640。每个 line 等于您正在读取的整行数据后跟一个 \n。您的程序的固定版本如下所示:

with open("00-old.dat","r") as input:
with open("00-new.dat","wb") as output:
    for line in input:
        if "41640" not in line:
            output.write(line)

要删除多行,您可以使用 all() 结合列表理解,如 this post

中所述
if all(nb not in line for nb in del_list):
    output.write(line)

其中 del_list 是您要删除的值列表,

del_list = ["41615", "41640", "42334"]

此外,由于 Python 的 operator precedence,您的原始条件将始终评估为 True。这是因为即使 41640!=line 为假,\n 也会添加到其中并解释(转换后)为 True。基本上,首先评估 !=,而不是后跟 != 的字符串连接。