我有一个类似于此的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:]:
哪个没有按预期工作。有人能指出我正确的方向吗?
答案 0 :(得分:1)
1
应该是str
类型,而不是int
类型。
if '1' not in columns