[portland] Extracting strings from a tuple

Matt McCredie mccredie at gmail.com
Sat Nov 3 00:01:59 CET 2007


>    While this should be simple, I cannot get it working and would appreciate
> guidance on solving my problem.
>
>    There is a database table with a single record. When I retrieve that
> record I have a list consisting of a single tuple. However, that tuple has 7
> strings; when printed it looks like this:
>
> [(u'string 1', u'string 2', u'string 3', u'string 4', u'string 5', u'string
> 6', u'string 7')]
>
>    What I want to do is walk through that tuple and extract each string.
> However, I cannot successfully slice it, iterate through it, or use an index
> to extract each string. According to "DIP," "Byte of Python," and other
> books I should be able to get each string as a separate item.
>
>    What have I missed?

Well, without some sample code showing what you are trying, it is
difficult to say what you have missed. How would I do it?

[code]
returned_data = [(u'string 1', u'string 2', u'string 3', u'string 4',
u'string 5', u'string6', u'string 7')]
strings_tuple = returned_data[0]
individual_string = strings_tuple[0]
for txt in strings_tuple:
    print txt
[/code]

My guess is that you are dereferencing from the list incorrectly. If
you don't want to use intermediate variables this should work:

[code]
returned_data = [(u'string 1', u'string 2', u'string 3', u'string 4',
u'string 5', u'string6', u'string 7')]

for txt in returned_data[0]:
    print txt
[/code]

Matt


More information about the Portland mailing list