2

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...

3
  • any help with code will be greatly appreciated... Commented Jul 9, 2010 at 10:35
  • 2
    what version of python are you using? all was added in python-2.5. You need to update at least to that version, or even better to the latest stable 2.7 Commented Jul 9, 2010 at 10:53
  • ok i just figured out that i am using python 2.3... i wil change my version immediately Commented Jul 9, 2010 at 10:59

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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...
+1 for suggesting old Python version. The all() built-in function was introduced in 2.5, according to Python docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.