如何从Excel文件中提取行

时间:2018-09-14 15:51:02

标签: python excel xlrd

我有an excel file。如何提取数据,以便在python中看起来像这样:

list = [['Igor', '20', 'SSU]'], ['Sergay', '19', 'SSTU'], ['Nadya', '21', 'SSAU']] 

使用导入xlrd

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容构建列表:

# Import:
import xlrd 

# Setting Path of File and Initializing List:
location = ("path of file") 
list = []

# Opening the Workbook and Defining Which Sheet to Use:
wb = xlrd.open_workbook(location) 
sheet = wb.sheet_by_index(0) 

# Starting at Row 0 and Column 0:
sheet.cell_value(0, 0)

# Iterating Over the Number of Rows and Appending to List:
for i in range(1, sheet.nrows + 1):
   list.append(sheet.row_values(i))

您还可以通过将wb.sheet_by_index包装在带有图纸数量的for循环中来遍历工作簿中的每个图纸。您可能还需要运行一些检查,以确保该行不为空。

请原谅任何错误,我处理Excel的Python有点生锈。