比较和删除python中的CSV文件行?

时间:2016-01-11 10:45:49

标签: python list csv

我有一个类似于此的csv文件。

"title","keep","get_rid","keep","rubbish"
"hello_world",1,0,0,0
"goodbye_world",0,0,1,0
"to_string",1,0,1,0
"not_so_smart",1,0,0,0

目标是删除不包含1的实例的列。因此,在此示例中,“get-rid”和“rubbish”将被删除 - 给我们留下类似的东西......

"title","keep", "keep"
"hello_world",1,0
"goodbye_world",0,1
"to_string",1,1
"not_so_smart",1,0

然而,我在某种程度上努力表现出最初似乎是一个简单的问题。

我失败的解决方案目前看起来像这样......

with open("filename.csv", "rb") as file:
reader = csv.reader(file)
header = next(reader)
for i, columns in enumerate(reader):
    for j, rows in enumerate(columns):
        if "1" not in rows[1:]:

哪个没有按预期工作。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

1应该是str类型,而不是int类型。

if '1' not in columns