I would like to ask you how I can extract substrings related to some keywords.
For example I have the following text:
JavaScript
x
2
1
mystring = "Commission 0,0000 Packaging 0,0426 Discount 0,0120 Transport 0,0690 F YEB 0,0000 Commission 0,0000 Payment discount 0,0000 % Other discount 0,0000 YEB 4,0700 % Industrial 0,3856"
2
I would like to extract the numeric value after some keywords, for example: “Discount” and “Other discount”. I was trying with the following code:
JavaScript
1
7
1
test = re.compile(r"""(
2
(Discountsd*)
3
(Othersdiscountsd*)
4
)""", re.VERBOSE)
5
6
pr = test.findall(mystring)
7
I would like to obtain (in this case) a pair –> Discount : 0,0120 and Other discount : 0,0000 But it could be also enough obtain a list like the following one:
JavaScript
1
2
1
["Discount 0,0120", "Other discount 0,0000"]
2
I really thanks in advance for any help.
Advertisement
Answer
I had better luck with re.search. Also you were missing d,d to capture numbers before and after the comma.
JavaScript
1
13
13
1
import re
2
3
mystring = "Commission 0,0000 Packaging 0,0426 Discount 0,0120 Transport 0,0690 F YEB 0,0000 Commission 0,0000 Payment discount 0,0000 % Other discount 0,0000 YEB 4,0700 % Industrial 0,3856"
4
5
pattern = "(Discountsd+,d+)(.*)(Othersdiscountsd+,d+)"
6
7
p = re.search(pattern, mystring)
8
9
p.groups()
10
>> ('Discount 0,0120',
11
' Transport 0,0690 F YEB 0,0000 Commission 0,0000 Payment discount 0,0000 % ',
12
'Other discount 0,0000')
13