Removing redundant characters from a string
2 answers
Please note that the separator characters used differ from country to country. In some cultures, "." used to separate groups, and "," means, for example, a decimal point. If you are parsing user-entered strings like this, it is better to use a local module. For example:
>>> import locale
>>> locale.atof('12,423,343.93') # No locale set yet, so this will refuse to parse
ValueError: invalid literal for float(): 12,423,343.93
>>> locale.setlocale(locale.LC_NUMERIC, "en_GB") # Use a UK locale.
>>> locale.atof('12,423,343.93')
12423343.93
+6
source to share