[Tutor] list and tuple question
Alan Gauld
alan.gauld at btinternet.com
Mon May 5 01:42:28 CEST 2008
"quantrum75" <quantrum75 at yahoo.com> wrote in
> Hi everybody
Hi,
I think the key to your question is here:
> a=((1,),(2,),(3,),(4,),(5,))
>
> I need the comma since it is going into an excel sheet.
I think you are confused between tuples and comma separated
strings - which is what you normally use to load data into Excel
I think you probably want to look at the csv module which makes
it easier to create properly foermatted comma separated values
> a=[]
> for i in range(5):
> a.append('['+str(i)+','+']')
This appends a string that looks like a list to a list.
To create a list of lists you only need:
a.append([i])
But that doesmn't show any commas - and neither should it
since the comma is not part of the list/tuple for a single item,
its only a syntactic trick to force Python to produce a tuple.
>>> a = (1,)
>>> a
(1,)
>>> print a[0]
1
So the comma is only part of the representation to show
its actually a tuple not just a number in parentheses.
> ['[0,]', '[1,]', '[2,]', '[3,]', '[4,]', '[5,]']
Which is what you told it to store.
> Which is no good since excel reads the whole thing inside the '
> '
> instead of the actual number.
How are you feeding the data to excel?
Or to rephtrase that how is Excel 'reading' the data?
That is fairly critical to solving your core problem.
> I know th e solution is probably simple and I appreciate any
> help.
I think the solution lies in a different place to where you
are looking.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list