I have a CSV like the following.
Category,Position,Name,Time,Team,avg_power,20minWKG,Male?,20minpower
A,1,Tom Smith ,00:41:58.95,7605,295,4.4,1,299.2
A,2,James Johnson,00:41:58.99,2740,281,4.5,1,283.95
A,3,Tom Lamb,00:41:59.25,1634,311,4.2,1,315
B,1,Elliot Farmer,00:45:06.23,7562,306,3.9,1,312
B,2,Matt Jones,00:45:10.10,4400,292,4.0,1,300
B,3,Patrick James,00:45:39.83,6508,299,4.1,1,311.6
I get the time of the rider in first place of category 'A'. I then give any rider that is 15% slower than this 0 points in a new CSV. For example if the first rider take 1 minute 40 seconds, then anyone slower than 1 minute 55 seconds will get 0 points.
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 that my code is very messy and inefficient. This is as there are lots of if in my loop and I believe a lot of the calculations seem unnecessary. Do you have any ideas of how I could improve my code?
I would prefer to not use Pandas if possible. This is as I have never used. However if there is a fast and easily maintainable solution to this with Pandas then I would be interested.