-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck
56 lines (44 loc) · 2.09 KB
/
check
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import re
def process_data(input_data):
pattern1 = r'(\d+)Q (\d{2})' # Matches 2Q 23
pattern2 = r'(\d+)Q(\d{2})' # Matches 2Q23
pattern3 = r'Q (\d{2})' # Matches Q followed by a space and 2 digits
pattern4 = r'Q(\d{2})' # Matches Q followed by 2 digits without space
pattern5 = r'Q(\d+) Results:? ' # Matches Q followed by a number, space, and "Results"
pattern6 = r'Q(\d+) Results' # Matches Q followed by a number and "Results"
pattern7 = r'[.,]' # Matches dots or commas
processed_data = []
for row in input_data:
processed_row = row
for pattern in [pattern1, pattern2, pattern3, pattern4, pattern5, pattern6]:
processed_row = re.sub(pattern, r'YearQuarter', processed_row)
processed_row = re.sub(pattern7, '', processed_row) # Remove dots and commas
processed_row = re.sub(r'(\d+)Q', r'Quarter', processed_row)
processed_row = re.sub(r'YearQuarter YearQuarter', r'YearQuarter', processed_row) # Remove duplicate YearQuarter
processed_data.append(processed_row)
return processed_data
input_data = [' ', 'Inv.', '2Q23 A',
' ', 'Inv. Banking', '2Q 23 Vs Expectations FMR',
' ', 'Inv. Bank', 'V. Prior Year/Quarter 2Q22',
'Q3 Results:', 'Q3 Results', 'Q3 Results',
' ', 'Inv. Bank', 'V. Prior Year/Quarter 2Q22', '2Q22A',
' ', 'Inv. Bank', 'V. Prior Year/Quarter 2Q 22', '2Q 22 A',
'1Q1.5', '2Q2,5']
processed_data = process_data(input_data)
for item in processed_data:
print(item)
import re
def extract_pattern(input_data):
output_data = []
pattern = r'% [a-zA-Z]+ [a-zA-Z]\/[a-zA-Z]'
for item in input_data:
match = re.search(pattern, item)
if match:
output_data.append(match.group())
else:
output_data.append(item)
return output_data
# Example usage:
input_data = ['YearQuater:', 'Revenue', '% chg g/g % chg g/g', 'Profit % ab/cd']
output_data = extract_pattern(input_data)
print(output_data)