[Tutor] get columns from txt file

Don Jennings dfjennings at gmail.com
Thu Jul 12 16:26:30 CEST 2012


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

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


More information about the Tutor mailing list