我有一个看起来像这样的mySQL数据(SequelPro的输出),该表被调用
mytable
:
该SQL db的整体结构如下所示:
我想要做的是将上面的表加载到numpy 2d数组中。 这是我的代码:
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
然而它出现了这个错误:
D = np.fromiter(results, count=num_rows, dtype=('c8,str,f,f,f,f,f,f'))
TypeError: a float is required
做正确的方法是什么?我的主要问题似乎是我不确定
如何正确构建dtype
。什么是SQL的等价物(char, mediumtext, float
)?请指教。
最后我想得到如下所示的np.array:
array([['101D','NONRBP', 2.829e-07,....,0.00030],
['115L','NONRBP', 2.5e-5,....,0.01772]])