2016-07-13 27 views
0

我一直在尝试使用pyodbc从SQL数据库中提取数据,并希望将其放入numpy.array。但是,我发现难以输入np.fromiter()参数的多个数据类型。pyodbc SQL查询到Numpy数组typeerror:需要类似字节的对象

import pyodbc as od 
import numpy as np 

con = od.connect('DSN=dBASE; UID=user; PWD=pass') 
cursor = con.cursor() 
SQLCommand = (
    """ 
    SELECT 

     [Item No_] 
     ,sum ([Quantity]) as TotQty 
     ,sum ([Discount Amount]) as DiscAmount 
     ,sum ([Cost Amount]) as CostAmount 
     ,[Date] 
     ,sum ([Net Amount]) as NetAmount 
     ,sum ([VAT Amount]) as VATAmount 
     ,sum ([Refund Qty_]) as RefundQty 


    FROM database 
    where [DATE] between ('2015-12-01 00:00:00.000') and ('2015-12-31 00:00:00.000') and [area No_] = '123' 
    group by ROLLUP([DATE],[Item No_]); 

    """) 

cursor.execute(SQLCommand) 
results = cursor.fetchall() 
results_as_list = [i[0] for i in results] 
array = np.fromiter(results_as_list, dtype="str, float, float, datetime64,float,float,float") 
print(array[:5,:]) 

而且我得到这个错误

TypeError: a bytes-like object is required, not 'str' 

回答

0

正在试图通过一个迭代,具体查询的第一列,与多个dtypes。 Numpy.iter()只接受一个对象和类型。考虑将查询结果集的每一列作为一维迭代进行传递。对于字符串(可变长度),您还需要指定长度,并且如果日期仅包含后缀[D]或仅包含时间[s]

cursor.execute(SQLCommand) 
results = cursor.fetchall() 

array = [np.fromiter([i[0] for i in results], dtype="|S50"), 
     np.fromiter([i[1] for i in results], dtype="float"), 
     np.fromiter([i[2] for i in results], dtype="float"), 
     np.fromiter([i[3] for i in results], dtype="float"), 
     np.fromiter([i[4] for i in results], dtype="datetime64[s]"), 
     np.fromiter([i[5] for i in results], dtype="float"), 
     np.fromiter([i[6] for i in results], dtype="float"), 
     np.fromiter([i[7] for i in results], dtype="float")] 

或者,创建矩阵数组:

array = np.array([np.matrix([i[0] for i in results], dtype="str"), 
        np.matrix([i[1] for i in results], dtype="float"), 
        np.matrix([i[2] for i in results], dtype="float"), 
        np.matrix([i[3] for i in results], dtype="float"), 
        np.matrix([i[4] for i in results], dtype="str"), 
        np.matrix([i[5] for i in results], dtype="float"), 
        np.matrix([i[6] for i in results], dtype="float"), 
        np.matrix([i[7] for i in results], dtype="float")]) 
+0

谢谢,它的工作原理!我是否也可以知道“| S50”的含义,我试着谷歌它,但没有找到任何参考。 – Windalfin

+0

太棒了!这是设置[字符限制字符串](http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html),因为它是默认的可变长度dtype。这里使用了50个字符。根据需要调整。 – Parfait

相关问题