[Tutor] writing tuplet to file

Deirdre Saoirse deirdre@deirdre.net
Thu, 27 May 1999 13:34:20 -0400 (EDT)


On Thu, 27 May 1999, Tim Peters wrote:

> [Marek Polanski]
> > I need to write whole tuple (with MANY strings inside) to a file.
> > Problem is, that when I'm trying to do this, python says:
> >
> > >>> f=open('file', 'w')
> > >>> a='blah', 'blahblah', 'blahblahblah'
> > >>> f.write(a)
> > Traceback (innermost last):
> >   File "<stdin>", line 1, in ?
> > TypeError: read-only buffer, list
> > >>>
> 
> Yes, f.write requires a string argument, and you're passing it a tuple
> (which is not a string <wink>).
> 
>     f.write(str(a))

f.write(string.join(a, '\t'))

would probably give something closer to the desired result: creating a
tab-delimited string of the tuple (thus keeping the parts separate).

_Deirdre   *   http://www.linuxcabal.org   *   http://www.deirdre.net
"Perl is just not the answer to all problems. If all you have is a hammer,
the whole world looks like your thumb." George Bonser on Python