read-only character buffer, list

Chris Liechti cliechti at gmx.net
Mon Jul 22 19:07:10 EDT 2002


akuip at yahoo.com (aaron) wrote in
news:85cee405.0207221446.750c36e3 at posting.google.com: 

> Ths following message is from a previous question.....my question is
> what if l = [0,1,2,3,4] is not a sequence...for example
> l=[.2,.009,.008,.05] how do I handle this?

this is still called a "sequence" in python. lists and tuples as well as 
strings are sequences. the order of the elements in the does not matter at 
all.

>>>> 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, not list <<< inserted "not" here

write() expects a string
writelines() expects a list of strings (lines or not does not matter, name 
symmetry because of readlines)

(a read-only buffer can be created with "buffer":
>>> b = buffer("hello")
>>> sys.stdout.write(b)
hello>>>

and now just forget it - you won't need "buffer"....)

> One could, of course, do the below: 
>>>> l = [0,1,2,3,4] 
>>>> f = open('e','w') 
>>>> f.write(''.join(map(str,l)))

yes, thats the way to go if the list contains non-strings 

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list