只是想快速了解使用 psycopg2 将大量数据重新排列到 postgres 中的最佳方法,即最少的编码方法。我看过一些使用 cast 的东西,但我真的认为这会很困难,我可以在网上找到一些好东西。
示例有人口普查数据,其中包含 200 个变量,在 recarray 中读取了许多列的不同数据类型。我只想浏览使用列名和数据类型并输入到 postgres。
另外,如果有比 psycopy2 更好的东西,我愿意接受建议。
这就是我发现的,尽管它进入了 sqlight 和错误的方式。
elif driver=='sqlite3':
tups=cur.fetchall()
if len(tups)>0:
_cast = {types.BooleanType: numpy.bool,
types.IntType: numpy.int32,
types.LongType: numpy.int64,
types.FloatType: numpy.float64,
types.StringType: numpy.str,
types.UnicodeType: numpy.str}
try:
typelist=[_cast[type(tmp)] for tmp in tups[0]]
except KeyError:
raise Exception("Unknown datatype")
res = numpy.core.records.array(tups)
else:
return None
res=[res[tmp] for tmp in res.dtype.names]
except BaseException: