In my project I am webscrapping UFC website to gather to the total wins, total losses, and total draws of each UFC athlete.
This is part of my code, as I wish to strip the total wins, total losses, and total draws separately:
import re
record = "10-7-3 (W-L-D)" #W = wins, L= Loss, D= Draws
char = "-"
record ="-"
totalwins = ""
totalloss = ""
totaldraws = ""
correctRecord = re.findall('\[[^\]]*\]|\([^\)]*\)|\"[^\"]*\"|\S ',record)[0]
print("The correct record per fighter is:", correctRecord)
totaldash = 0
for i in range(len(correctRecord)):
if(record[i] == char):
totaldash =1
if totaldash == 1:
print("The total wins", totalwins)
totalwins =""
elif totaldash ==2:
print("The total losses ", totalwins)
totalwins=""
elif totaldash ==3:
print("The total draws ", totalwins)
elif (correctRecord[i] !=char):
totalwins =correctRecord[i]
The result is the following:
The correct record per fighter is: 10-7-3
The total wins 10
The total losses 7
The problem is, I am unable to spew out the total draws. I also tried using the strip method with no avail:
correctRecord= str(record.split(separator, 1)[0])
CodePudding user response:
Try:
import re
record = "10-7-3 (W-L-D)"
wins, loss, draw = map(int, re.findall(r"\d ", record))
print(f"{wins=} {loss=} {draw=}")
Prints:
wins=10 loss=7 draw=3
CodePudding user response:
Something a little more complicated which will work in the event the data is in a different order:
import re
record = "10-3-7 (W-D-L)"
values = re.findall(r'\d |[WDL]', record)
vdict = dict(zip(values[3:], values[:3]))
print(f'wins={vdict["W"]}, losses={vdict["L"]}, draws={vdict["D"]}')
Output:
wins=10, losses=7, draws=3