If you need to exclude all columns that have any zeroes in them, then first you need to read the whole file in memory -- because only after having seen every row will you know which columns have any zeroes! This is a logical need -- whatever language you use the need will remain, it's intrinsic to the problem
So, for example:
allrows = list(reader)
Now, allrows
is a list of dictionaries, whose items are strings, presumably 0
or 1
. Now, you could do:
keepcols = [c for c in allrows[0] if all(r[c] != '0' for r in allrows)]
...not the fastest approach, but hopefully very, very simple to understand!
Once you do know which columns you want to keep, prepare a DictWriter
instance w
with those columns as the headers and the extrasaction='ignore'
argument (so it will ignore "extra" keys in the dicts passed to it, and finally
w.writerows(allrows)
If you mean something different than "exclude all columns which have any zeroes in them", then please clarify exactly what you do mean by "i need exclude those columns that have 0's" because I can't interpret it differently.