[Tutor] get columns from txt file

susana moreno colomer susana_87 at hotmail.com
Thu Jul 12 18:06:23 CEST 2012


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?
Many thanks, 
Susana



Subject: Re: [Tutor] get columns from txt file
From: dfjennings at gmail.com
Date: Thu, 12 Jul 2012 11:43:02 -0400
CC: tutor at python.org
To: susana_87 at hotmail.com




On Jul 12, 2012, at 11:10 AM, susana moreno colomer wrote:


Hi!
It is attached on the email, called myprogram.txt

and, here are the contents of that file:





#! /usr/bin/env python




import os
import fnmatch
import csv




path = '//../my_working_folder/'
csv_out=csv.writer(open('out14.csv', 'wb'), delimiter=' ')
files=os.listdir(path)


outfile=open('myfile1', 'w')

Here you've opened a file called "outfile" which you never use! So, of course it's blank. Just remove this line.



output=[]




for infile in files:          columnData = []


if fnmatch.fnmatch(infile, 'bb_*'):
filename= os.path.join(path,infile)
f=open(filename, 'r')

for line in f:

b=line.split('\t')                          # remove the next line


output.append(b[5].strip())                          # instead, append to columnData list
                          columnData.append(b[5].strip())


f.close()

                  # now append all of that data as a list to the output
                  output.append(columnData)


  # now, as Kent said, create "a list of row lists from the list of column lists
  # if any of the column lists are too short they will be padded with None"


  rows = map(None, *output)


  # now, write those rows to your file
  csv_out.writerows(rows)






########This gives me a single column (I want 6, since I have 6 bb_files:


csv_out.writerows(output)

One of the things you missed in Kent's code is that the output is a list of lists. So, for each file you need a list which you then append to the output list. I've inserted the appropriate code above and you should have better luck if you copy and paste carefully.








########with this I get excel whitesheet




def csvwriter(): 
excelfile=csv_out
a=excelfile.append(output)
c=excelfile.write(a)
return c


Right! As I explained in an earlier email, you never call the function csvwriter; you merely define it. The file is created when you opened it earlier for writing on line 10 of your program, but you never write anything to it.


If you have the time and inclination, I recommend you work through one of the fine tutorials for python. It really is a great language and this is a **great** community of folks for people like me who are learning the language still.


Take care,
Don





 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120712/b82d4edf/attachment-0001.html>


More information about the Tutor mailing list