0
+---------+---------+------------+-----------+
| Input             |    Excepted            |
+---------+---------+------------+-----------+
| EUR 1.409,00      |    1409                |
| $ 409,05          |    409.05              |
| ¥409.50          |    409.5               |
| CNY 1,000         |    1000                |
+---------+---------+------------+-----------+

The first thought came into my mind is regular expression. I can't write it though. How do you figure this out?

Barmar
  • 741,623
  • 53
  • 500
  • 612
HIPPO LD
  • 477
  • 5
  • 20
  • 1
    The first thought which came into my head is that you should be using a Python library which can convert numbers from one locale to the other. The regex part of this is fairly trivial; you only need to extract the numbers from the text. – Tim Biegeleisen Aug 09 '18 at 09:44
  • For example Babel? https://stackoverflow.com/a/37598997/8069403 – xdze2 Aug 09 '18 at 09:51
  • I think you mean **currency** not **concurrency**. Currency is money, concurrency is doing multiple things at the same time. – Barmar Aug 09 '18 at 11:04

2 Answers2

1

You can use several regex iterations:

import re

results = []

# Get list of numbers
numbers = re.findall('[\d,.]+', 'EUR 1.409,00 $ 409,05 ¥409.50 CNY 1,000.00')

# Parse number
for number in numbers:
    dp = ''
    ip = number
    match = re.search('(?<=[,.])(?:\d{2})$', number)
    # Get decimal places
    if match:
        dp = match.group()
        # Extract integer places
        ip = number[:match.start()]
    # Remove commas and periods from integer
    ip = ip.translate(None, ',.')
    # Parse float
    results.append(float('{}.{}'.format(ip, dp)))
print(results)

This results in [1409.0, 409.05, 409.5, 1000.0]. I would bet there's a much more convenient regex to do this all in one, but at least it should work for you :)

Tobias Lorenz
  • 1,426
  • 11
  • 17
0

To get you started, you can use the regex ([0-9.,]+) to extract the numbers with varying decimal locales.

Then, you will likely want to follow Tim Biegeleisen's advice, and use a locale library to convert them into numbers.

Alistair Carscadden
  • 1,198
  • 5
  • 18