将excel列提取到python数组中

时间:2014-03-20 18:49:46

标签: python arrays excel

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

excel文件如下所示:

     A    B    C
1   123  534  576
2   456  745  345
3   234  765  285

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

[[123,534,576],[456,745,345],[234,765,285]]

我该怎么做?谢谢

6 个答案:

答案 0 :(得分: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)

答案 1 :(得分:2)

如果您正在按照上述注释查看xlrd软件包,可以尝试一下,看看它是否有效吗?

(基于我在此处找到的内容:http://www.youlikeprogramming.com/2012/03/examples-reading-excel-xls-documents-using-pythons-xlrd/

import xlrd
workbook = xlrd.open_workbook('my_workbook.xls')
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows - 1
curr_row = 0

#creates an array to store all the rows
row_array = []

while curr_row < num_rows:
    row = worksheet.row(curr_row)
    row_array += row
    curr_row += 1

print(row_array)

答案 2 :(得分:1)

使用xlrd加载数据 row-wise ,然后使用zip转置它。

>>> 
>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> zip(*a)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>> 

使用xlrd加载数据 row-wise ,用它来创建一个numpy数组,然后转置它。

>>> import numpy
>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> z = numpy.array(a)
>>> z
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> z.transpose()
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])
>>>

答案 3 :(得分:1)

我明白了。

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

答案 4 :(得分:0)

import csv
array = []
with open(* insert file directory here*) as fin:
     reader = csv.reader(fin)
     rows = [row for row in reader]
     for row in rows:
        j = 0
        arr = []
        for i = 0 < 3:
          arr[i] = row[i]
        array[j] = arr
        j = j + 1

答案 5 :(得分:0)

import csv

csv_rows = csv.reader(open("temp.csv","r"))
result_array = []
for row_index, row in enumerate(csv_rows):   
    if row_index != 0: #to neglect column names row
        result_array.append(row)
print(result_array)