[Tutor] get columns from txt file

Emile van Sebille emile at fenx.com
Thu Jul 12 18:26:17 CEST 2012


On 7/12/2012 9:06 AM susana moreno colomer said...
>
> Hi!
> This code is working fine!
> The only one little thing is that the 6 columns appear together in one
> column, what means, in eac cell I get 6 numbers. How can I get tit in 6
> excel columns?


You're creating a list (output) with only one column in it, so you only 
get one column in excel.

Try making these chages (untested):


outfile=open('myfile1', 'w')
cols=[]   #  this will get one list (column) per found file

for infile in files:
     if fnmatch.fnmatch(infile, 'bb_*'):
         thiscol = []  # we're starting a new file so start a new column
         filename= os.path.join(path,infile)
         f=open(filename, 'r')
         for line in f:
             b=line.split('\t')
             thiscol.append(b[5].strip())  # append each col value
         f.close()
     cols.append(thiscol)  # store this cols results

output = zip(cols) # zip transposes the cols to rows


HTH,

Emile




More information about the Tutor mailing list