Библиотека xlrd идеально подходит для работы с файлами Excel.
import xlrd
def main():
# Path to excel file
file_path = ('PATH_TO_FILE')
# Import complete excel workbook
excel_workbook = xlrd.open_workbook(file_path)
# Import specific sheet by index
excel_sheet = excel_workbook.sheet_by_index(0)
# Create array for each row
relevantData = []
# Loop through each row of excel sheet
for row in range(excel_sheet.nrows): #nrows returns number of rows
# If even
if row % 2 != 0:
# Convert row to array and append to relevantData array
relevantData.append(rowToArray(row))
print(relevantData)
def rowToArray(row):
"""
excel_sheet.cell_value(row,0) -> Get the data in the row defined
.split() -> returns list of string, spilt at the white spaces,
map(int, <>) -> map all values in list to integers
lits(map(<>)) -> reconverts result into a list
"""
return list(map(int, excel_sheet.cell_value(row,0).split()))
main()
Вывод:
[[1, 35, 62, 93, 116, 167, 173, 176, 182], [2, 11, 29, 128, 130], [8, 19, 20, 25, 26, 58, 67, 132, 150, 153, 185, 187, 188]]