我正在使用xlrd进行数据提取,并为我的项目提取了8列输入。每列数据大约有100行。我的代码如下:
wb = xlrd.open_workbook('/Users/Documents/Sample_data/AI_sample.xlsx')
sh = wb.sheet_by_name('Sample')
x1 = sh.col_values( + 0)[1:]
x2 = sh.col_values( + 1)[1:]
x3 = sh.col_values( + 2)[1:]
x4 = sh.col_values( + 3)[1:]
x5 = sh.col_values( + 4)[1:]
x6 = sh.col_values( + 5)[1:]
x7 = sh.col_values( + 6)[1:]
x8 = sh.col_values( + 7)[1:]现在,我想要创建一个输入数组,它给出8列的每一行。如果这是我的8列数据
x1 x2 x3 x4 x5 x6 x7 x8
1 2 3 4 5 6 7 8
7 8 6 5 2 4 8 8
9 5 6 4 5 1 7 5
7 5 6 3 1 4 5 6我想要的东西是: x1,x2,x3,x4,x5,x6 (1,2,3,4,5,6,7,8)。
我本可以进行逐行提取,但是,对100+行这样做实际上非常困难。那我该怎么做呢。我还理解可以使用np.array完成这一任务。但我不知道怎么做。
发布于 2015-10-08 10:36:24
我发现这段代码非常有用
X = np.array([x1, x2, x3, x4, x5, x6, x7, x8])
return X.T发布于 2015-10-07 09:07:37
您还可以尝试openpyxl,类似于xlrd
from openpyxl import load_workbook,Workbook
book = load_workbook(filename=file_name)
sheet = book['sheet name']
for row in sheet.rows:
col_0 = row[0].value
col_1 = row[1].value我以前更喜欢openpyxl而不是xlrd
https://stackoverflow.com/questions/32987918
复制相似问题