to extract specific columns from a csv file and copy it to another using python
I have a CSV file which is actually a matrix of 0's and 1's. I need to exclude those columns that have 0's and pick only those that have 1's and copy them to开发者_如何学Go another CSV file.
Here is what I have tried:
reader=csv.DictReader(open("test1.csv","r"),[])
for data in reader:
if data==1:
print data
What am I doing wrong?
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.
reader = csv.DictReader(open("test1.csv", "r"), [])
for data in reader:
if data[column header] != 0:
print data[column header]
精彩评论