[英]Comparing and removing rows of CSV file in python?
I have a csv file that looks something similar to this. 我有一个看起来与此类似的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
The goal is to remove the columns of which do not contain an instance of 1. So in this example, "get-rid" and "rubbish" will be removed - leaving us with something like... 目标是删除不包含实例1的列。因此,在此示例中,将删除“ get-rid”和“ rubbish”-给我们留下类似...
"title","keep", "keep"
"hello_world",1,0
"goodbye_world",0,1
"to_string",1,1
"not_so_smart",1,0
However, I have somehow struggled to perform what seemed to be initially a simple problem. 但是,我一直在努力执行最初看起来很简单的问题。
My failed solution currently looks like this... 我失败的解决方案目前看起来像这样...
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:]:
Which isn't working as expected. 哪个没有按预期工作。 Can anyone point me in the right direction?
谁能指出我正确的方向?
1
应该是str
类型,而不是int
类型。
if '1' not in columns
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.