-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathregex.py
37 lines (30 loc) · 1.29 KB
/
regex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Python 3.7
# appendix to sherdog-parser.py
# Created by - Montanaz0r (https://github.com/Montanaz0r)
# Some of records scrapped from sherdog did have tricky event names, for instance there was ',' inside double quote,
# it messed with reading these lines properly in Pandas so i have decided to help myself with regular expressions.
# Code below will adjust problematic lines.
import csv
import re
new_csv = []
with open('sherdog.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
new_row = []
if len(row) == 1:
subbed = re.sub(r'(?!(([^"]*"){2})*[^"]*$),', '', row[0])
subbed = subbed.replace('"', '')
subbed = subbed.replace('-', '')
after_split = subbed.split(',')
new_csv.append(after_split)
else:
for word in row:
subbed = re.sub(r'(?!(([^"]*"){2})*[^"]*$),', '', word)
subbed = subbed.replace('"', '')
subbed = subbed.replace('-', '')
new_row.append(subbed)
new_csv.append(new_row)
with open('sherdog-subbed.csv', 'w', newline='') as f:
writer = csv.writer(f, delimiter=';', skipinitialspace=True)
for line in new_csv:
writer.writerow(line)