如何从 excel 文件中提取行
how to extract rows from excel file
我有an excel file。我怎样才能提取数据,使 python 看起来像:
list = [['Igor', '20', 'SSU]'], ['Sergay', '19', 'SSTU'], ['Nadya', '21', 'SSAU']]
使用 导入 xlrd
您可以使用以下方法构建列表:
# 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 循环中来遍历工作簿中的每张工作表。您可能还想 运行 进行一些检查以确保该行不为空。
请原谅任何错误,我的 Python 处理 Excel 有点生疏。
我有an excel file。我怎样才能提取数据,使 python 看起来像:
list = [['Igor', '20', 'SSU]'], ['Sergay', '19', 'SSTU'], ['Nadya', '21', 'SSAU']]
使用 导入 xlrd
您可以使用以下方法构建列表:
# 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 循环中来遍历工作簿中的每张工作表。您可能还想 运行 进行一些检查以确保该行不为空。
请原谅任何错误,我的 Python 处理 Excel 有点生疏。