Re: Converting List of Tuples to List of Strings

Jim Vickroy <[email protected]>
Newsgroups gmane.comp.python.db.pysqlite.user
Message-ID <[email protected]>
Rich Shepard wrote:
>    I'm stuck in a rut and have not found a way out.
>
>    Records are retrieved from a sqlite table and stored in a list. In another
> module (and function) I retrieve these records and want to use their
> contents with other data in a different table.
>
>    When I access the data, e.g., 'print appData.polEco' I see this:
>
> [(u'Jobs',), (u'Infrastructure',), (u'Housing',), (u'Tax Base',), (u'Traffic
> Volume',), (u'Roads',), (u'Medical care',), (u'Sustainability',)]
>   
Hi Rich,
This should get you started again ...

 >>> x = [(u'Jobs',), (u'Infrastructure',), (u'Housing',), (u'Tax 
Base',), (u'Traffic Volume',), (u'Roads',), (u'Medical care',), 
(u'Sustainability',)]
 >>> x
[(u'Jobs',), (u'Infrastructure',), (u'Housing',), (u'Tax Base',), 
(u'Traffic Volume',), (u'Roads',), (u'Medical care',), (u'Sustainability',)]
 >>> y = [this[0] for this in x] # retain only the first element of each 
tuple in *x*
 >>> y
[u'Jobs', u'Infrastructure', u'Housing', u'Tax Base', u'Traffic Volume', 
u'Roads', u'Medical care', u'Sustainability']
 >>>

-- jv
>    I can extract each tuple, for i in range(len(appData.polEco)), and this
> gives me a list of individual tuples:
>
> (u'Jobs',)
> (u'Infrastructure',)
> (u'Housing',)
> (u'Tax Base',)
> (u'Traffic Volume',)
> (u'Roads',)
> (u'Medical care',)
> (u'Sustainability',)
>
>    Then, when I want to use these with other data:
>
> values = ('eco', appData.polEco[i], ecoVec[i])
> appData.cur.execute("insert or replace into importance (cat, comp, imp_wt)
> values (?,?,?)", values)
>
>    python responds:
>
> pysqlite2.dbapi2.InterfaceError: Error binding parameter 1 - probably
> unsupported type.
>
>    Printing values gives:
>
> ('eco', (u'Roads',), 0.073969887301348305)
>
>    so I assume it's the extra parentheses and unicode mark causing the
> problem. But, I've not been able to remove them when I've tried to slice
> them off.
>
>    How do I handle this?
>
> Rich
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.