get-a-cup-of-coffee slow (fwd)

Mark Rowe mark21rowe at yahoo.com
Fri Aug 10 00:40:30 EDT 2001


Hi,

The first example should be written:

>>> l=[0,1,2,3,4]
>>> f=open('e', 'w')
>>> f.write(''.join(l))

and the second should  be something like:

>>> l=[0,1,2,3,4]
>>> f=open('e', 'w')
>>> l2 = map(str, l)
>>> f.writelines(l2)

This is because f.write() takes a expects a string argument, not a list, and
f.writelines() expects a list of strings, not a list of integers.

Mark

"Grant Griffin" <not.this at seebelow.org> wrote in message
news:3B735049.4547EC3E at seebelow.org...
> Quinn Dunkan wrote:
> >
> > On Thu, 09 Aug 2001 18:12:31 -0400, Lulu of the Lotus-Eaters
<mertz at gnosis.cx>
> > wrote:
> > >Grant Griffin wrote:
> > >|Also, I tried writing something like this with a list (which are
presumably more
> > >|suited to being appended), but then I realized that I didn't know how
to write
> > >|the contents of a list to a file as a stream of bytes.  There's
probably a
> > >|simple way to do that: what is it?
> > >
> > >''.join(lst)         # Python 2.0+
> > >string.join(lst,'')  # Python 1.4+ (or something)
> >
> > There is also file.writelines(lst).  It's not documented in my version
of
> > python, so maybe you're not supposed to use it, but why put it there
then?
> > It's been there for quite a while now.
>
> The other day I had tried that with "write".  It went something like
> this:
>
>    Python 2.0.1 (#18, Jun 30 2001, 10:36:44) [MSC 32 bit (Intel)] on
> win32
>    Type "copyright", "credits" or "license" for more information.
>    >>> l=[0,1,2,3,4]
>    >>> f=open('e', 'w')
>    >>> f.write(l)
>    Traceback (most recent call last):
>      File "<stdin>", line 1, in ?
>    TypeError: read-only character buffer, list
>
> but maybe it should have been "writelines" instead:
>
>    >>> f.writelines(l)
>    Traceback (most recent call last):
>      File "<stdin>", line 1, in ?
>    TypeError: writelines() requires sequences of strings
>    >>>
>
> what-part-of-'Traceback'-don't-I-understand?-<wink>-ly y'rs,
>
> =g2
> --
> _____________________________________________________________________
>
> Grant R. Griffin                                       g2 at dspguru.com
> Publisher of dspGuru                           http://www.dspguru.com
> Iowegian International Corporation            http://www.iowegian.com





More information about the Python-list mailing list