[Tutor] Tab delim file

Liam Clarke cyresse at gmail.com
Wed Mar 2 01:27:19 CET 2005


Hi Srinivas,

You want to use the csv module, it's designed for this kind of stuff.

http://docs.python.org/lib/module-csv.html

So, I think for your thing you'd want = 


import csv

f1 = open('my_file.txt','r')
reader =  csv.reader(f1)
reader.delimiter = '\t'
fileA = []
fileB = []
for row in reader:
     toA = []
     toB = []
     for i in range(len(row)):
          if i <= 12:
             toA.append(row[i])
          else:
             toB.append(row[i])
     fileA.append(toA)
     fileB.append(toB)

You may want to play with this, I'm not sure if I set the reader's
delimiter right. But \t is tab.

Good luck, 

Liam Clarke


On Tue, 1 Mar 2005 14:21:58 -0800 (PST), Srinivas Iyyer
<srini_iyyer_bio at yahoo.com> wrote:
> Hello:
> 
> I have a nasty file where I have 165 column and 140K
> lines.
> 
> For every 12 columns, a new dataset is written.
> 
> 0-12 - File A's data is there
> 13-24 - File B's data is there.
> 
> My task is to write data in each 12 columns to a file.
> 
> I have a rough idea, but when I try I am unable to
> proceed further.
> 
> f1 = open('my_file.txt','r')
> aml = f1.read().split('\n')
> 
> fLine = aml[0]
> 
> k = len(fLine)/12  # k = 162/12
> 
> #So I want to jump k blocks and get the text.
> 
> for line in aml:
>    cols = line.split('\t')
>    while i > 13:
>    ......
> I am lost from here...
> 
> Every 4 line(row) contains the file name on which I
> have to write the file.
> 
> can any one plese help, i am really stuck in a
> problem.
> 
> thanks
> srini
> 
> 
> __________________________________________________
> 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
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list