Skip to content

Instantly share code, notes, and snippets.

@jsok
Created February 3, 2013 06:03
Show Gist options
  • Save jsok/4700698 to your computer and use it in GitHub Desktop.
Save jsok/4700698 to your computer and use it in GitHub Desktop.
A little hack to switch locales and override some locale conventions temporarily.
# Store the current locale
default_loc = locale.getlocale()
# Swap to some foreign locale, e.g. German
locale.setlocale(locale.LC_ALL, 'de_DE')
# Define a new localeconv() with some overrides
# see http://docs.python.org/2/library/locale.html#locale.localeconv
def _temp_localeconv(lc=locale.localeconv()):
lc.update({'decimal_point': ',', 'thousands_sep': '.'})
return lc
# Do the override
locale.localeconv = _temp_localeconv
# Do the actual price string to float conversion using the locale
value = locale.atof("1.234,56)
# Should return: float 1234.56
# Reset locale
locale.setlocale(locale.LC_ALL, default_loc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment