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.

Revisions

  1. jsok created this gist Feb 3, 2013.
    21 changes: 21 additions & 0 deletions locale_hack.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # 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)