[Tutor] get columns from txt file

Don Jennings dfjennings at gmail.com
Thu Jul 12 14:53:49 CEST 2012


(Please use reply all so the message gets posted for all to see :>)

On Jul 12, 2012, at 7:14 AM, susana moreno colomer wrote:

> Hi!
> Many thanks for your help.
> Now I am trying this, but I get a blank excel document

Not surprising as there are several errors in your code. In fact, I'm surprised it ran without reporting an error. Be sure to copy and paste the code exactly as you are running it.

> (I have tried everything, but I don't get it!)

That's why we're here :>)

As I understand it, you have a file structure like this:

dir/
    bb_csvfile1.txt
    bb_csvfile2.txt
    bb_csvfilen.txt
    someotherfile.txt

You want to read each of the csv files which start with 'bb_', extracting the data in a certain column, and then output that data to a single excel document. Yes?


>  
> import os
> import fnmatch
> import csv
> 
> path = '...'

First of all, this path variable looks odd. Do you mean '.' for the current directory or '..' for the parent directory?

> csv_out=csv.writer(open('out11.csv', 'wb'), delimiter='  ')

Since you said you want an excel document, you should specify dialect='excel'  (or 'excel-tab') instead of a delimiter.

> files=os.listdir(path)
>  
> for infile in files:
>  output=[] 

The variable output should be moved outside the for loop (i.e. put it before the "for infile in files:")

>  if fnmatch.fnmatch(infile, 'bb_*'):
>   global filename

Drop the global statement. Out of curiousity, why did you think you needed that? (There was no use of global in the example code from Kent Johnson.)

>   filename= path+infile

Won't work (even if the path variable was correct)—where's the file path separator? Use the join function of os.path:

filename = os.path.join(path, infile)

>         
>   global f

Again, no need for the global statement.

>   f=open(filename, 'r')
>        
>   for line in f:
>  
>    b=line.split('\t')
>    output.append(b[5].strip())
>   
>    def csvwriter():

No need for a function here. In fact, you don't ever actually call it, so it never gets run! That's a major reason why you aren't getting any output.

>     csv_out.append(output)
>     csv_out.append('\n')

These should be outside of the for loops as well (i.e. remove all indentation).
>    
>   f.close


You need the parentheses to call the function and it should be at the same level as the call where you open it (i.e. the same amount of indentation, before the calls to csv_out.append):

f.close()

Take care,
Don

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


More information about the Tutor mailing list