Created
February 3, 2013 06:03
-
-
Save jsok/4700698 to your computer and use it in GitHub Desktop.
A little hack to switch locales and override some locale conventions temporarily.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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