[Tutor] get columns from txt file

susana moreno colomer susana_87 at hotmail.com
Wed Jul 11 16:20:05 CEST 2012


Hi!
 
I have a group of files in a directory.
I want to extract from  files whose names start with  bb_   column number 5 to an excel file. I have 6  bb_  files, therefore I want to get 6 columns (5th column from each file)
This code is working,with the following errors:

I get the data in only one column, instead of six
I get white cell after every extracted cell
 
I've got  help from http://mail.python.org/pipermail/tutor/2004-November/033474.html, though it is not working for me
 
This is my code:
 
 
import os
import fnmatch
import csv

path = '//......'
files=os.listdir(path)
csv_out=csv.writer(open('out.csv', 'w'), delimiter='  ')

for infile in files:
   
        if fnmatch.fnmatch(infile, 'bb_*'):
                print infile
       
                filename= path+infile
                print filename
        
                f=open(filename)
                for line in f.readlines():
                       b=line.split('\t')
                       csv_out.writerow(b[5])
               f.close
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120711/236bb065/attachment-0001.html>


More information about the Tutor mailing list