[Tutor] extracting a column from many files

Bala subramanian bala.biophysics at gmail.com
Thu Feb 19 11:41:40 CET 2009


Dear friends,

I want to extract certain 6 different columns from a many files and write it
to 6 separate output files. I took some help from the following link

http://mail.python.org/pipermail/tutor/2004-November/033475.html

to write one column from many input files to a particular output file. Since
i have to extract 6 such columns, i wanted to loop the output file writing
part. This block of the script is shown in bold below. I see some odd output
file names.  Kindly suggest me i ) how best or should i do this loop part
ii) explanation of the working *row=map(None,*value) *below which i adopted
from the above tutor-mail list link.

Thanks in advance,
Bala

 #!/usr/bin/env python
from sys import argv
lst_files=argv[1:]

sh=[];st=[];sta=[];buc=[];pro=[];ope=[]

def extract(fname)*:*
    A=[];B=[];C=[];D=[];E=[];F=[]
    data=open(fname).readlines()
    for number, line in enumerate(data):
        if "  Duplex" and " Shear" in line:
            number=number+3
        for x in range(0,8):
                new=data[number]
                A.append(new[19:26])
                B.append(new[27:34])
                C.append(new[37:42])
                D.append(new[44:54])
                E.append(new[56:63])
                F.append(new[69:75])
                number = number + 1
    sh.append(A)
    st.append(B)
    sta.append(C)
    buc.append(D)
    pro.append(E)
    ope.append(F)

for x in lst_files:
          extract(x)

*list=[sh,st,sta,buc,pro,ope]*
*for value in list:*
    *row=map(None,*value)*
    *out=open(str(value) + '.txt','w')
    for num in row:
          out.write('\t'.join(num))
          out.write('\n')
    out.close()*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090219/1b4bd982/attachment.htm>


More information about the Tutor mailing list