tags:

views:

78

answers:

2

hi, I'll explain my whole problem:
I have 2 csv files:

  • project-table.csv (has about 50 columns)
  • interaction-matrix.csv (has about 45 columns)

I want to append the string in col[43] from project-table.csv with string in col[1] of interaction-matrix.csv with a dot(.) in between both the strings

next,

  • interaction-matrix.csv has a set of headers..
  • its 1st col will now have the appended string after doing what I've mentioned above
  • all other remaining columns have only 0's and 1's
  • I'm supposed to extract only those columns with 1's from this interaction-matrix.csv and copy it to a new csv file... (with the first column intact)

this is the code i ve come up with...

I'm getting an error with the keepcols line...

import csv
reader=csv.reader(open("project-table.csv","r"))
writer=csv.writer(open("output.csv","w"),delimiter=" ")
for data in reader:
        name1=data[1].strip()+'.'+data[43].strip()
        writer.writerow((name1, None))


reader=csv.DictReader(open("interaction-matrix.csv","r"),[])
allrows = list(reader)
keepcols = [c for c in allrows[0] if all(r[c] != '0' for r in allrows)]

print keepcols
writer=csv.DictWriter(open("output1.csv","w"),fieldnames='keepcols',extrasaction='ignore')
writer.writerows(allrows)

this is the error i get:

Traceback (most recent call last):
  File "prg1.py", line 23, in ?
    keepcols = [c for c in allrows[0] if all([r[c] != '0' for r in allrows])]
NameError: name 'all' is not defined

project table and interaction-matrix both have the same data in their respective 1st columns .. so i just appended col[43] of prj-table to col[1] of the same table itself...

+1  A: 

Edit your question to show what error message are you getting. Update: NameError probably means you are using an (older) version of Python (which one?) without all() or (you have used all as a variable name AND are not showing the exact code that you ran)

Note: open both files in binary mode ("rb" and "wb") respectively.

You say "I want to append the string in col[43] from project-table.csv with string in col[1] of interaction-matrix.csv with a dot(.) in between both the strings" HOWEVER you are using col[2] (not col[1]) of project-table.csv (not interaction-matrix.csv, which you haven't opened at that stage).

John Machin
ok here s the thing...project table and interaction-matrix both have the same data in their respective 1st columns .. so i just appended col[43] of prj-table to col[1] of the same table itself...
totallyignorant
not ok -- edit your question with those facts.
John Machin
i ve done the editing
totallyignorant
+1 for suggesting old Python version. The `all()` built-in function was introduced in 2.5, according to Python docs.
Denilson Sá
A: 

is there a better way to do the whole thing??

totallyignorant