将excel列提取到python数组中

use*_*664 2 python arrays excel

我想将excel列(NOT行)提取到数组的python数组中.它必须是数组,而不是字典.

excel文件如下所示:

     A    B    C
1   123  534  576
2   456  745  345
3   234  765  285
Run Code Online (Sandbox Code Playgroud)

我想以下列格式将它带入python:

[[123,534,576],[456,745,345],[234,765,285]]
Run Code Online (Sandbox Code Playgroud)

我该怎么做?谢谢

小智 8

这是一个更简单的方法:

import xlrd
book = xlrd.open_workbook('your.xlsx')
sheet = book.sheet_by_name('example')
data = [[sheet.cell_value(r, c) for c in range(sheet.ncols)] for r in range(sheet.nrows)]
# Profit !
print(data)
Run Code Online (Sandbox Code Playgroud)


use*_*664 0

我想到了。

import csv
cr = csv.reader(open("temp.csv","rb"))
arr = range(100)  # adjust to needed
x = 0
for row in cr:    
    arr[x] = row
    x += 1

print(arr[:22])  # adjust to needed
Run Code Online (Sandbox Code Playgroud)