I have a csv like so:
Category,Position,Name,Time
A,1,Tom Smith,00:45:01.23
there are multiple rows in the same format.
I am getting the time of the first place rider in category 'A', and calculating the time which is 15% above, i.e. if they take 1 minute 40 seconds then time to calculate is 1 minute 55 seconds. It will then give anybody in cat A above this time 0 points in a new csv.
I have this code:
def convert(seconds): # function to convert amount of seconds to a time format
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%d:%02d:%02d" % (hour, minutes, seconds)
with open("results.csv", 'rt', encoding='UTF-8', errors='ignore') as file: # opening the full results file
reader = csv.reader(file, skipinitialspace=True, escapechar='\\') # skipping headers
MaleCategoryList = [] # setting category as blank so a change is recognised
for row in reader:
if row[0] not in MaleCategoryList:
if row[0] == "A":
firstPlaceTime = datetime.strptime(row[3], "%H:%M:%S.%f")
timeInSecs = firstPlaceTime.second + firstPlaceTime.minute * 60 + firstPlaceTime.hour * 3600
timeDifference = timeInSecs * 1.15
MaxTime = datetime.strptime(convert(timeDifference), "%H:%M:%S")
# some code here which is not relevant i.e calculate points
if cat == "A" and datetime.strptime(row[3], "%H:%M:%S.%f") > MaxTime:
points = int(0)
position_for_file = "DQ Time-Cut"
cat = "Time Cut"
data = {'Position': position_for_file, 'Category': cat, 'Name': name, 'Club': club,
'Points': points, 'Time': time} # dictionary of data to write to CSV
I feel it is very messy and inefficient as there are lots of if loops and it relies on lots of calculations which do seem unnecessary. Do you have any ideas of how I could re-write this/improve it?