fork(1) download
  1. import re
  2. def xNumber(arg):
  3. switcher = {
  4. "mln": 1000000,
  5. "million": 1000000,
  6. "bln": 1000000000,
  7. "billion": 1000000000,
  8. "thousand": 1000,
  9. "hundred": 100
  10. }
  11. return switcher.get(arg, 1)
  12.  
  13. rx = re.compile(r'\$(?P<number>\d+(?:,\d{3})?(?:\.\d+)?(?:-\d+(?:,\d{3})?(?:\.\d+)?)?)(?:\s*(?P<suffix>mln|million|bln|billion|thousand|hundred))?')
  14. s = "$3 million, $910,000,$16.5-18 million"
  15. result = ""
  16. for match in rx.finditer(s):
  17. if match.group("suffix") and match.group("number").find("-") == -1: # We have no range and have a suffix
  18. result = str(float(match.group("number"))*xNumber(match.group("suffix")))
  19. elif match.group("number").find("-") > -1: # Range
  20. lst = [float(x) for x in match.group("number").split("-")]
  21. result = str(float(sum(lst))/len(lst)) + (" {}".format(match.group("suffix")) if match.group("suffix") else "")
  22. else: result = float(match.group("number").replace(",",""))
  23. print(result)
Success #stdin #stdout 0.01s 9016KB
stdin
Standard input is empty
stdout
3000000.0
910000.0
17.25 million