如何将 excel 文件数据的元素分配给 Python 中的二维矩阵?

how to assign elements of an excel file data to a 2D matrix in Python?

我想将 excel sheet 中的数据用作二维矩阵,但是尽管迭代提取的数据我遇到了一些错误。

我想加载从 excel 到 python 代码的数据集。我使用了 xlrd 库,认为在遍历元素之后,它会产生一个数组。虽然我希望有一个二维矩阵。

import xlrd


workbook = xlrd.open_workbook('test1.xlsx')
sheet = workbook.sheet_by_index(0)

N =sheet.nrows
M =sheet.ncols

mat_d =  [ [0] * N for _ in range(M)]

mat=[]
for i in range(N):
    for j in range(M):
        mat.append(sheet.cell(i, j).value)

k = 0
for i in range(N):
    for j in range(M):
        mat_d[i][j]= mat[k]
        k += 1

代码在这里给出了我的正确答案,mat_d[1][1] = mat[79],但是当我想在 for 循环中迭代它时,它以错误告终: IndexError: 列表索引超出范围

import os
import csv

data = []

with open(os.path.join(sys.path[0], file), newline='') as dataset:
    reader = csv.reader(dataset)
    for row in reader:
        rowlist = []
        for numeric_string in row:
            value = int(numeric_string)
            rowlist.append(value)

        data.append(rowlist)