我有一个numpy数组需要插入到PostgreSQL中。包含子数组的numpy数组已在元组中。 元组看起来像这样:
((1,0.,-0.5,[0,0],1,5))-我缩短了阵列以显示子阵列
元组包含一个要插入单行的项。如果我想插入多行,元组将包含多个项
我已经为numpy.uint32、numpy.float32和numpy.ndarray注册了PostgreSQL适配器:
from psycopg2.extensions import register_adapter, AsIs
def numpy2pyUInt32(self,npUInt32):
return AsIs(npUInt32.item())
def numpy2pyFloat32(self,npFloat32):
return AsIs(npFloat32.item())
def numpy2pyndarray(self,npndarray):
return AsIs(npndarray.tolist())
register_adapter(numpy.uint32, self.numpy2pyUInt32)
register_adapter(numpy.float32, self.numpy2pyFloat32)
register_adapter(numpy.ndarray, self.numpy2pyndarray)
要插入数据,我使用execute_values()
和以下sql命令:
INSERT INTO mytable VALUES %s
。但当我执行此操作时,我从psycopg2得到一个错误,它说:
Traceback (most recent call last):
File "...", line 132, in <module>
...
File "...", line 113, in ...
psycopg2.extras.execute_values (cur, sql_command, col_values)
File "/.../lib/python3.8/site-packages/psycopg2/extras.py", line 1292, in execute_values
cur.execute(b''.join(parts))
psycopg2.errors.SyntaxError: syntax error at or near "["
LINE 1: ...06346473842859,0.0,0.0,0.0,14.284889221191406,4,0,[0, 0],540...
^
其他信息:
data = ((1,0., -0.5, ..., [0, 0], 1, 5))
print(type(data)) # <class 'tuple'>
print(type(data[0])) # <class 'numpy.void'>
print(data[0].dtype) #
[('..', '<u4'), ('..', '<f4'), ('..', '<f4'), ('..', '<f4'), ('..', '<f4'),
('..', '<f4'), ('..', '<f4'), ('..', '<f4'), ('..', '<f8'), ('..', '<f4'),
('..', '<f4'), ('..', '<f4'), ('..', 'u1'), ('..', 'u1'),
('..', 'u1',(2,)), # sub-array
('..', '<u4'), ('..', '<u4'), ('..', '<u4'), ('..', '<u4'), ('..', '<u4'),
('..', '<u4')]
print (data[0].shape) # ()
print (type(data[0][0])) # <class 'numpy.uint32'>
...
print (type(data[0][14])) # <class 'numpy.ndarray'>
定义数据类型:
和结构化阵列:
查看一个记录:
将其包装在元组中不会转换该内部数组:
关注这一领域:
使用字段名列表减去问题字段名对数组进行索引:
相关问题 更多 >
编程相关推荐