Extract String From Enclosing Tuple

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Wed Feb 28 16:33:56 EST 2007


On Feb 28, 12:40 pm, rshep... at nospam.appl-ecosys.com wrote:
>   I'm a bit embarrassed to have to ask for help on this, but I'm not finding
> the solution in the docs I have here.
>
>   Data are assembled for writing to a database table. A representative tuple
> looks like this:
>
> ('eco', "(u'Roads',)", 0.073969887301348305)
>
> Pysqlite doesn't like the format of the middle term:
> pysqlite2.dbapi2.InterfaceError: Error binding parameter 1 - probably
> unsupported type.
>
>   I want to extract the 'Roads', part from the double-quoted enclosing
> tuple.

(snipped)



Perhaps something like:

>>> t = ('eco', "(u'Roads',)", 0.073969887301348305)
>>> t2 = eval(t[1], { '__builtins__' : {} }, {} )
>>> t2
(u'Roads',)
>>> t2[0].encode('ascii')
'Roads'


>>> import itertools
>>> tuple(itertools.chain((t[0], t2[0].encode('ascii')), t[2:]))
('eco', 'Roads', 0.073969887301348305)

>>> tuple(itertools.chain((t[0], (t2[0].encode('ascii'),)), t[2:]))
('eco', ('Roads',), 0.073969887301348305)


--
Hope this helps,
Steven




More information about the Python-list mailing list