[Tutor] extracting a column from many files
Kent Johnson
kent37 at tds.net
Thu Feb 19 19:46:29 CET 2009
On Thu, Feb 19, 2009 at 5:41 AM, Bala subramanian
<bala.biophysics at gmail.com> wrote:
> Dear friends,
>
> I want to extract certain 6 different columns from a many files and write it
> to 6 separate output files.
>
> #!/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)
I think you want to use extend() rather than append() here so you end
up with flat lists of values. Using append() creates lists of lists
which I don't think you want.
>
> for x in lst_files:
> extract(x)
>
> list=[sh,st,sta,buc,pro,ope]
Don't use the names of built-ins (e.g. 'list') as variable names, it
shadows the built-in name.
> for value in list:
> row=map(None,*value)
Don't do this. You just want one column of data. The program you took
this from was writing one column *per input file* which I don't think
you want.
> out=open(str(value) + '.txt','w')
Value is a list, not a file name. I think you want something like this:
# List of pairs of (value list, name)
lists = [ (sh, 'sh'), (st, 'st'), ...]
for values, name in lists:
out = open(name + '.txt', 'w')
for value in values:
out.write(str(value))
out.write('\n')
out.close()
Kent
> for num in row:
> out.write('\t'.join(num))
> out.write('\n')
> out.close()
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list