使用Python读取Excel中特定列

1 投票
1 回答
940 浏览
提问于 2025-04-18 18:29

我需要用Python输出某一列下的所有内容。

到目前为止,我已经可以读取整个Excel文件了。在我的工作簿里有很多数据表,所以我指定了要打开哪一个,现在我只需要输出指定列下的所有内容。以下是我的代码:

import xlrd

file_Location = "/home/jerad/Desktop/Register.xlsx"
workbook = xlrd.open_workbook(file_Location)
sheet = workbook.sheet_by_name('CM4000 Register View')
num_rows = sheet.nrows - 1
curr_row = -1
while curr_row < num_rows:
   curr_row += 1
   row = sheet.row(curr_row)   #this is print everything in the file.
   print row

1 个回答

0

更新的方式如下:

while curr_row < num_rows:
    curr_row += 1
    row = sheet.cell(curr_row,1)   #this is print only the cells selected (Index Start from 0).
    print (row.value)

如果你只打印row,你会得到类似这样的内容:

number:1.0

当你使用.value时,你只会得到值:

1.0

撰写回答