I am currently trying to write an algorithm that reads in a CSV file, gets a list of names from column 0, a list of hours worked from column 6, then iterate through a list of staff names, if a name is equal to the current name, grab the relevant hours, add them to a total, once the name is fully checked in the whole file, the name is removed from the list and the next name gets checked.
main.py
from modules import gui_module as gui
from modules import csv_operations as csvcalc
if __name__ == "__main__":
programWindow = gui.programGUI("CSV Calculator", 200, 150)
programWindow.run()
finalStaffHours = csvcalc.calculate_csv_data(gui.csv_file_path, 'staff', 'shift time ex. break (hr)') # Send the CSV file path to the iteration module.
print (finalStaffHours)
csv_operations.py
import pandas as pd
import csv
def calculate_csv_data(csv_file_path, name_column, hours_column):
""" Take CSV File, remove header row and find the length of said file."""
dataFrame = pd.read_csv(csv_file_path)
file_length = len(dataFrame) # Find length of file
uncheckedNames = get_name_column(dataFrame, 'staff') # Get list of staff names - contains duplicates
allHoursWorked = get_hours_column(dataFrame, 'shift time ex. break (hr)')
finalStaffHours = calculate_staff_hours(allHoursWorked, uncheckedNames, int(file_length))
return finalStaffHours
def calculate_staff_hours(all_hours_worked, unchecked_names, file_length):
""" Calculates each staff members hours then returns the list to main."""
staffHoursAndName = []
currentHourTotal = 0.0
uncheckedNamesRemovedDupe = list(set(unchecked_names))
while uncheckedNamesRemovedDupe:
currentNameToCheck = uncheckedNamesRemovedDupe[0]
for i in range(file_length):
if i == (file_length + 1):
hourAndNameCombined = currentNameToCheck + str(currentHourTotal)
staffHoursAndName.append(hourAndNameCombined)
currentHourTotal = 0.0
del uncheckedNamesRemovedDupe[0]
if currentNameToCheck != unchecked_names[i]:
continue
if currentNameToCheck == unchecked_names[i]:
currentHourTotal += all_hours_worked[i]
print(currentHourTotal)
if not uncheckedNamesRemovedDupe:
return staffHoursAndName
def get_hours_column(csv_file_path, hours_column):
""" Pull all of the hours worked from column 6 and return that. """
if hours_column in csv_file_path.columns:
return csv_file_path[hours_column].tolist()
def get_name_column(csv_file_path, name_column):
""" Pull all of the staff names from column 0 CSV and return them. """
if name_column in csv_file_path.columns:
return csv_file_path[name_column].tolist()
type here
I can read in the CSV, get the list from column 0 and column 6 - the problem lies in the calculate_staff_hours function in csv_operations.py
It should add the numbers together for one person, once done then remove that person from the list, move on to the next and so on.
It adds the numbers from the CSV file in a never ending loop.
print()(andprint(type(...)),print(len(...)), etc.) to see which part of code is executed and what you really have in variables. It is called"print debugging"and it helps to see what code is really doing.get_name_columnandget_hours_columnto get column. They do the same. Besides if column doesn't exists then it will returnNonebut you never check if you get column or None. So you could at once runcsv_file_path[staff'].tolist()without functions. It could make code shorter and more readable.