Hi!
 
I have a group of files in a directory:
bb_1.txt
bb_2.txt
bb_3.txt
bb_4.txt
bb_5.txt
bb_6.txt
ss_1.txt

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
 
Many thanks!

 



Subject: Re: [Tutor] get columns from txt file
From: dfjenni...@gmail.com
Date: Thu, 12 Jul 2012 10:26:30 -0400
CC: tutor@python.org
To: susana...@hotmail.com

Oops! Still you forgot to cc: the tutor list. It's really important because if 
someone (like me, for instance) steers you in the wrong direction, others will 
jump in with corrections.





On Jul 12, 2012, at 9:48 AM, susana moreno colomer wrote:


Hi!
Many thanks!


You're welcome. I see that your code is improving :>)




Still I get an error: AttributeError: '_cvs.writer' object has no atribute 
'append'. 

If you would like a response to that error, please send the exact code you 
tried which didn't work and the error message by copying/pasting. (See, I'm 
pretty sure that you typed in the error above since you misspelled attribute as 
'atribute'.)




I wanted to solve it with the Def function.

And that's why you should send the original code and error. Now, you have a 
different problem, but it's a good time to clear up your confusion. Def is not 
a function. In fact, I don't know what "Def" is. Instead, the def 
statement—notice that it's all lower case—defines a function which you can call 
elsewhere in code.



>>> def call_me():
...     print "inside the call_me function"
... 
>>> call_me()
inside the call_me function


Note, that there is no output from our defining the call_me function; it's only 
when we call it with the parentheses that the code inside of it is executed. 
Make sense? However, again, it's not necessary for this script. Once you have 
the csv_out variable, you should be able to 




Now I am trying this, though I get
 
 
>import os
>import fnmatch
>import csv

>path = '/This is my current directory/'
>csv_out=csv.writer(open('out13.csv', 'wb'), delimiter=' ')
>files=os.listdir(path)
>outfile=open('myfile1', 'w')
>output=[]

>for infile in files:
>      if fnmatch.fnmatch(infile, 'bb_*'):
>      filename= os.path.join(path,infile)
>      f=open(filename, 'r')
  
>      for line in f:
>          b=line.split('\t')
>          output.append(b[5].strip())
>     f.close()
 
>Def csvwriter():       ---->gives me SyntaxError: invalid syntax
>        excelfile=csv_out
>        a=excelfile.append(output)
>        c=excelfile.write(a)
>        return c
 
 
I am trying with this  because the atribute 'writerows' didn't work.


A different error. There are lots of reasons it might not have worked. Wish you 
had sent us the code for that one :<( We can't solve it without all the info.



Is there another way to write in csv files?


Absolutely. At the interactive prompt, dir() shows all the attributes of an 
object to find out your options:


>>> csv_out=csv.writer(open('out13.csv', 'wb'), dialect='excel')
>>> dir(csv_out)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', 
'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'dialect', 
'writerow', 'writerows']


See the writerow and writerows? Those look promising. Alternatively, while it 
takes a while to learn to read the python documentation, it really is your 
friend:


http://docs.python.org/library/csv.html


If you'd like further examples, Doug Hellman often provides a great resource 
with his Python Module of the Week (PYMTOW) series:


http://www.doughellmann.com/PyMOTW/csv/





I don't know what you mean with specify dialect


Something like this:


csv_out = csv.writer(fileobject, dialect='excel')


Take care,
Don
                                          
#! /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')
output=[]


for infile in files:

        if fnmatch.fnmatch(infile, 'bb_*'):
                filename= os.path.join(path,infile)
                f=open(filename, 'r')
                
                for line in f:
        
                        b=line.split('\t')
                        output.append(b[5].strip())
                f.close()

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

csv_out.writerows(output)


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


def csvwriter():        
        excelfile=csv_out
        a=excelfile.append(output)
        c=excelfile.write(a)
        return c
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to