Basically I have a simple employee list file and I am trying to load it in python and manipulate some data.
My code at the moment looks like this:-
from typing import Any
import numpy as np
from openpyxl import load_workbook
from openpyxl.utils import get_column_letter
wb = load_workbook('Employees.xlsx')
ws = wb.active
row_count = ws.max_row
column_count = ws.max_column
headings_PlayersSheet = ['Name'] + ['Age'] + ['Nationality']
def get_employee_list():
employee_list = []
# get an array of players from the sheet provided
for row in range(2, row_count + 1):
# print(ws.cell(row, 1).value)
employee_list.append(ws.cell(row, 1).value)
# playersList.append(ws.cell(row, 1).value)
for col in range(2, column_count + 1):
char = get_column_letter(col)
employee_details = [ws[char + str(1)].value, ws[char + str(row)].value]
employee_list.append(employee_details)
# print(employee_list)
employee_list_arr = np.array(employee_list, dtype=object)
# print(employee_list_arr)
np.save('employeeList.npy', employee_list_arr)
return
get_employee_list()
employees_list = np.load('employeeList.npy', allow_pickle=True)
# print(employees_list)
# print(employees_list[0])
# print(employees_list[37][1])
# print(employees_list[38][1])
# print(employees_list[39][1])
The problem I am facing is that when I do the last print(employees_list), I am only getting the last record, instead of the whole arraylist. I am fairly new to Python so I am trying to figure out how I can persist this arraylist so that I can use it outside of the function.
Any help would be very much appreciated! Thanks!
I have added an extra array to get all the records in an array collection
from typing import Any
import numpy as np
from openpyxl import load_workbook
from openpyxl.utils import get_column_letter
wb = load_workbook('Employees.xlsx')
ws = wb.active
row_count = ws.max_row
column_count = ws.max_column
def get_employee_list():
employee_list = []
# get an array of players from the sheet provided
for row in range(2, row_count + 1):
employee_list_single: list[Any] = [ws.cell(row, 1).value]
for col in range(2, column_count + 1):
char = get_column_letter(col)
employee_details: list(Any) = [ws[char + str(1)].value, ws[char + str(row)].value]
employee_list_single.append(employee_details)
employee_list.append(employee_list_single)
# print(employee_list)
employee_list_arr = np.array(employee_list, dtype=object)
# print(employee_list_arr)
np.save('employeeList.npy', employee_list_arr)
return
get_employee_list()
employees_list = np.load('employeeList.npy', allow_pickle=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.