2016-07-13 20 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

相關問題