[Tutor] Tab delimited file

Lloyd Kvam pythonTutor at venix.com
Tue Nov 2 19:25:15 CET 2004


Assuming the input file is small enough to manipulate in memory:

rows = [ line.split('\t') for line in f1 ]	# nested lists ~= 2d array
columns = zip(*rows)	# transposes rows and columns (thanks Peter Norvig)

Now your data is organized the way you want it.  The hard part is
knowing how to do the transpose.  I learned it from Peter Norvig's web
site.
http://www.norvig.com/python-iaq.html

Let us know if you have questions getting the data written out.

On Tue, 2004-11-02 at 12:29, kumar s wrote:
> Dear Group, 
>  I have a tab-delimited text file with 100 columns. 
> 
> I wanted to write every first column and every
> alternative column starting from 2-100 in to a file. 
> 
> For example:
> Column 1 is names of my genes.
> Column 2 - 100 are experiments done to check those
> genes in various samples. 
> 
> Now I want to write column 1 and column 2 as file1.
> Column1 and column3 as file2. 
> Col. 1 and col. 4 as file 3 ..so on and so forth. 
> 
> 
> import string
> from string import strip
> 
> f1 = open('file1.txt','r')
> f2 = open('sub_file2.txt','w')
> list = f1.read()
> list1 = split(list,'\n')
> for all lines in range(len(list1)):
>          columns = split(list1[i],'\t')
> 
> 
> >From now on It proved difficult for me to progress
> ahead. How can I ask column 1 and column 2 to be
> written to sub_file2.txt. 
> 
> Can any one please help. 
> 
> What I do not know is:
> 1.how can I ask python to choose specific data only in
> column2, 3 4 ....like that. 
> 
> 2. I do not know I can ask python to iterate over each
> row and asking it to write only column 1 and column 2
> and nothing else. 
> 
> Please help me and this is a stumblick block for me. 
> 
> 
> Thanks
> 
> Kumar
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp



More information about the Tutor mailing list