Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def parse_numeric_string(s):
- if isinstance(s, int): s = str(s)
- amount = None
- currency = ''
- multiplier = 1.0
- for token in s.split(' '):
- token = token.lower()
- # Extract multipliers from their string names/abbrevs
- if token in ['million','m','mm']:
- multiplier = 1e6
- # ... or you could use a dict:
- # multiplier = {'million': 1e6, 'm': 1e6...}.get(token, 1.0)
- # Assume anything else is some string format of number/int/float/scientific
- try:
- token = token.replace(',', '')
- amount = float(token)
- except:
- pass # Process your parse failures...
- # Return a tuple, or whatever you prefer
- return (currency, amount * multiplier)
- parse_numeric_string("$ 1,350,000")
- parse_numeric_string("1.35 MM $")
- parse_numeric_string("$ 1.35 M")
- parse_numeric_string(1350000)
- (?<=$ )([d,]+)
- ([d.]+)(?= MM $)
- (?<=$ )([d.]+)(?= M)
- ([d]+)
Add Comment
Please, Sign In to add comment