[Numpy-discussion] Issue with converting from numpy record to list/tuple

Sean Davis seandavi@gmail....
Mon Aug 27 12:23:34 CDT 2007


I have a numpy recarray that I want to load into a database using insert
statements.  To do so, I need to convert each record to a tuple.  Here is
what I get (using psycopg2)

In [1]: a[1]
Out[1]: ('5151_0023_0001', 'FORWARD', 'interval rank', 'target_tm:76.00
;probe_tm:70.90;freq:27.93;count:01;rules:0000;score:0658',
'chr3:1-199501827',
'AAAGGAATTCCATTCATCTCTGGATATTTTGAAATCATTAGGGCAAACAATAAATAA', 0L, 171449529L,
171449529L, 1L, 23L, 'experimental', 'CHR03P006149104', 6149104L, 5151L,
23L, 1L)

In [2]: type(a[1])
Out[2]: <class 'numpy.core.records.record'>

In [3]: sqlcommand
Out[3]: 'insert into nbl_tmp values
(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'

In [4]: cur.execute(sqlcommand,tuple(a[1]))
---------------------------------------------------------------------------
<class 'psycopg2.ProgrammingError'>       Traceback (most recent call last)

/sherlock/sdavis/Documents/workspace/svn/watson/Sean/PythonCode/<ipython
console> in <module>()

<class 'psycopg2.ProgrammingError'>: can't adapt

In [5]: b=('5151_0023_0001', 'FORWARD', 'interval rank', 'target_tm:76.00
;probe_tm:70.90;freq:27.93;count:01;rules:0000;score:0658',
'chr3:1-199501827',
'AAAGGAATTCCATTCATCTCTGGATATTTTGAAATCATTAGGGCAAACAATAAATAA', 0L, 171449529L,
171449529L, 1L, 23L, 'experimental', 'CHR03P006149104', 6149104L, 5151L,
23L, 1L)

In [6]: cur.execute(sqlcommand,b)

In [7]: a[1].dtype
Out[7]: dtype([('PROBE_DESIGN_ID', '|S40'), ('CONTAINER', '|S40'),
('DESIGN_NOTE', '|S80'), ('SELECTION_CRITERIA', '|S80'), ('SEQ_ID', '|S40'),
('PROBE_SEQUENCE', '|S100'), ('MISMATCH', '<u8'), ('MATCH_INDEX', '<u8'),
('FEATURE_ID', '<u8'), ('ROW_NUM', '<u8'), ('COL_NUM', '<u8'),
('PROBE_CLASS', '|S40'), ('PROBE_ID', '|S40'), ('POSITION', '<u8'),
('DESIGN_ID', '<u8'), ('X', '<u8'), ('Y', '<u8')])

Why does the casting using tuple() not work while cut-and-paste of the a[1]
record into a new variable works just fine?

Thanks,
Sean
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070827/a5c46573/attachment.html 


More information about the Numpy-discussion mailing list