如何使用python读取excel列作为列表?

2024-09-29 21:34:54 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用python读取excel列作为列表?你知道吗

Excel数据:输入文件(输入.xlsx)你知道吗

Column1      Column2  Column3  Column4 
one          two      three    four    
22/03/1997   six      7        eight   

代码

book = xlrd.open_workbook("input.xlsx")
sheet = book.sheet_by_index(0)

col = []

for i in range(1,sheet.nrows):
  col.append(str(sheet.row_values(i)))

但我的代码将按行打印,但我希望按列读取数据

预期产量:

[[Column1,one,22/03/1997],[Column2,two,six],[Column3,three,7,],[ Column4,four,eight]]

Tags: 代码colxlsxonesheetthreefourtwo
2条回答

您可以使用pandas

import pandas as pd

df = pd.read_excel('file.xlsx', header=None)
result = [list(df[x].values) for x in df.columns.values]

输出:

[['Column1', 'one', '22/03/1997'], ['Column2', 'two', 'six'], ['Column3', 'three', 7], ['Column4', 'four', 'eight']]
import xlrd
book = xlrd.open_workbook("input.xlsx")
sheet = book.sheet_by_index(0)
col = []
for i in range(0,sheet.ncols):
    col.append(str(sheet.col_values(i)))
print col

相关问题 更多 >

    热门问题