开发者

append a particular column from a csv file to another using python

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()
        w开发者_运维技巧riter.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...


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜