首页 > 解决方案 > 如何将 Excel 工作表中的 numpy 浮点数组乘以常数?

问题描述

我正在使用xlrd包从 Excel 文件中读取值并将它们存储到数组中。我打印了数组,它从 Excel 中获取了值。现在我想将数组乘以一个常数值:

def getExcel():    
    book = xlrd.open_workbook(filedialog.askopenfilename())
    sheet = book.sheet_by_index(5)
    sheet.cell_value(0, 0)    
    y = []
    ydata = np.array(y, dtype = np.float32)
    x = []
    angle = np.array(x, dtype = np.float32)    
    i=0
    while (i<sheet.nrows):
        if (i%2)>0:            
            angle = np.append(angle, sheet.cell_value(i, 0))            
        else:
            ydata = np.append(ydata, sheet.cell_value(i, 0))
        i+=1
    constant = sheet.cell_value(1, 4)
    rad = np.multiply(angle, 0.0174)    
    print(rad)

我希望数组的所有元素都angle乘以0.0174,但出现如下错误:

line 27, in getExcel rad = np.multiply(angle, 0.0174)
numpy.core._exceptions.UFuncTypeError: ufunc 'multiply' did not contain a 
loop with signature matching types (dtype('<U32'), dtype('<U32')) -> 
dtype('<U32')

标签: python-3.xnumpy

解决方案


推荐阅读