Re: [cx-oracle-users] cx_Oracle cursor.var Example
Brought to you by:
atuining
From: Doug H. <djn...@gm...> - 2014-10-23 22:18:56
|
On 23 October 2014 14:31, Solomon, Scott \ wrote: > Ok, getting closer. Causes another error. I think I still need to convert > variable 'line' to string somehow: > > > File "C:\Users\{redacted}\gensim\models\lsimodel.py", line 513, in > show_topics > if i < len(self.projection.s): > Your problem is more a python one than a cx_Oracle problem, but i will try to set you on the right path. Open a python console, and follow along. I am using python3, python2 with "from __future__ import print_function" also works, or just translate the print function calls on the fly. >>> result = [ ( 'proj 1 title' , ) , ( 'proj 2 title' , ) , ( 'proj 3 title' , ) , ] >>> result [('proj 1 title',),('proj 2 title',),('proj 3 title',),] This is sort of what the result set of the cursor looks like, if you used a statement like >>> result = cur.fetchall ( ) after executing your cursor. Here, you get back a list of rows, where each row is a tuple of column values. when you use the cursor in the for statement, you are getting back a generator that will give you the next row tuple on each iteration. The actual type of the column values may be cx_Oracle defined types that are automatically converted to regular python strings and number when you use them. The result of a list comprehension is a generator object. It is almost the same as the list object, in practice. (Not PEP8, to make brackets and parenthesis easier to see.) >>> print ( row for row in result ) <generator object <genexpr> at 0x0000000002319DC8> >>> print ( [ row for row in result ] ) [('a',), ('b',), ('c',)] Notice how I get the column values: >>> print ( [ row [ 0 ] for row in result ] ) ['a', 'b', 'c'] >>> print ( [ row [ 0 ] . upper ( ) for row in result ] ) ['A', 'B', 'C'] >>> print ( row [ 0 ] . upper ( ) for row in result ) <generator object <genexpr> at 0x0000000002319D80> So your corpora.Dictionary() constructor is getting passed a generator object. If it expects a list() type object it may do the wrong thing. Just add the square brackets to pass a list object instead of the generator object. Likewise, if down in the the guts of that code, something is expecting a string and chokes on the actual type passed back from cx_Oracle, you can convert the value using something like: >>> print ( [ str ( row [ 0 ] ) . upper ( ) for row in result ] ) ['A', 'B', 'C'] Doug -- Doug Henderson, Calgary, Alberta, Canada |