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:
JavaScript
x
37
37
1
import re
2
record = "10-7-3 (W-L-D)" #W = wins, L= Loss, D= Draws
3
char = "-"
4
record+="-"
5
totalwins = ""
6
totalloss = ""
7
totaldraws = ""
8
9
10
correctRecord = re.findall('[[^]]*]|([^)]*)|"[^"]*"|S+',record)[0]
11
12
print("The correct record per fighter is:", correctRecord)
13
14
totaldash = 0
15
for i in range(len(correctRecord)):
16
if(record[i] == char):
17
18
totaldash+=1
19
20
21
if totaldash == 1:
22
print("The total wins", totalwins)
23
totalwins =""
24
elif totaldash ==2:
25
26
print("The total losses ", totalwins)
27
28
totalwins=""
29
30
elif totaldash ==3:
31
32
print("The total draws ", totalwins)
33
34
35
elif (correctRecord[i] !=char):
36
totalwins +=correctRecord[i]
37
The result is the following:
JavaScript
1
5
1
2
The correct record per fighter is: 10-7-3
3
The total wins 10
4
The total losses 7
5
The problem is, I am unable to spew out the total draws. I also tried using the strip method with no avail:
JavaScript
1
2
1
correctRecord= str(record.split(separator, 1)[0])
2
Advertisement
Answer
Try:
JavaScript
1
8
1
import re
2
3
record = "10-7-3 (W-L-D)"
4
5
wins, loss, draw = map(int, re.findall(r"d+", record))
6
7
print(f"{wins=} {loss=} {draw=}")
8
Prints:
JavaScript
1
2
1
wins=10 loss=7 draw=3
2