I have the following strings:
'10000 ABC = 1 DEF'
'1 AM = 0,30$'
'3500 ABC = 1 GTY'
'1000 HUYT=1ABC'
'1 MONET Data = 1 ABC'
I want to find a flexible way to extract numeric and string values from left and right sides of =. I do not know all possible string values. Therefore I cannot pre-define them. The only thing that I know is that left and right sides are divided by =.
The goal is to get this result for the above-given example:
String-pairs:
ABC-DEF
AM-$
ABC-GTY
HUYT-ABC
MONET Data-ABC
Numeric-pairs:
10000-1
1-0.30
3500-1
1000-1
1-1
I was trying to use .lstrip('...') and rstrip("..."), but it does not give me the expected result.
Remove the unwanted characters and replace the = with a -.
import re
str = ['10000 ABC = 1 DEF',
'1 AM = 0,30$',
'3500 ABC = 1 GTY',
'1000 HUYT=1ABC',
'1 MONET Data = 1 ABC']
String_pairs = []
Numeric_pairs = []
for s in str:
String_pairs.append (re.sub(r'\s*=\s*','-', re.sub(r'\s*\d+(,\d+)?\s*','', s)))
Numeric_pairs.append (re.sub(r'\s*=\s*','-', re.sub(r'\s*[^\d,=]+\s*','', s)))
print String_pairs
print Numeric_pairs
Result:
['ABC-DEF', 'AM-$', 'ABC-GTY', 'HUYT-ABC', 'MONET Data-ABC']
['10000-1', '1-0,30', '3500-1', '1000-1', '1-1']
or a more cooler list comprehension (with the same result):
String_pairs = [re.sub(r'\s*=\s*','-', re.sub(r'\s*\d+(,\d+)?\s*','', s)) for s in str]
Numeric_pairs = [re.sub(r'\s*=\s*','-', re.sub(r'\s*[^\d,=]+\s*','', s)) for s in str]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With