[Tutor] Data conversion

Andre Engels andreengels at gmail.com
Thu May 19 11:26:00 CEST 2011


On Thu, May 19, 2011 at 8:15 AM, Joe Aquilina <joe at chem.com.au> wrote:

> I realised after I read your response that I probably hadn't included enough
> information, partly due to my  inexperience in Python and partly due to
> haste on my part.
>
> AFter my original post, I had a little play in Python and was able to create
> this tuple:
>
> [1, 2, 3, 4, 5]
>
> from which I was able to extract any item I wanted as an integer and work
> with as I wanted. I am guessing that this is a 1-tuple.

No, this is an array. However, an array and a tuple work similarly in
many cases. A 1-tuple is a tuple with one element, so this is
definitely not a 1-tuple.

> It is when I do the fetchall() from the table, that I get the following:
>
> [(1,), (2,), (3,)]
>
> I don't know enough to know whether this is a 1-tuple or not. It is from
> this tuple that I want to extract the 3 as an integer so that I can
> increment it and save as an integer into the next row in the table.

This again is an array, but this time the elements are tuples (indeed
1-tuples). To show you how to get the value 3 from this:

>>> A = [(1,), (2,), (3,)]
>>> A[2]
(3,)
>>> A[-1]
(3,)
>>> B = A[-1]
>>> B
(3,)
>>> B[0]
3
>>> A[-1][0]
3


-- 
André Engels, andreengels at gmail.com


More information about the Tutor mailing list