将混合数据类型的MySQLdb数据加载到Numpy的ndarray中

2024-09-30 04:39:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个mySQL数据,看起来像这样(SequelPro的输出),表名为 mytable

enter image description here

SQL数据库的总体结构如下所示: enter image description here

我要做的是将上面的表加载到numpy2d数组中。 这是我的代码:

import MySQLdb as mdb
import numpy as np

db = conn.connect(host = "somehost.ac.jp",
                 user = "gandalf",
                 passwd = "abcxxx",
                 db = "mygreatdb",
                 )
cursor = conn.cursor()
sql = """
      SELECT * from %s
      """ %mdb.escape_string(tablename)
cursor.execute(sql)
results = cursor.fetchall()
num_rows = int(cursor.rowcount)



D = np.fromiter(results, count=num_rows, dtype=('c8,str,f,f,f,f,f,f'))
print D

但是它给出了以下错误:

^{pr2}$

正确的方法是什么?对我来说主要的问题似乎是我不确定 如何正确构造dtype。SQLs(char, mediumtext, float)的numpy等价物是什么?请告知。在

最后我想得到np.数组看起来像这样:

array([['101D','NONRBP', 2.829e-07,....,0.00030],
       ['115L','NONRBP', 2.5e-5,....,0.01772]])

Tags: importnumpydbsqlasnp数组conn

热门问题