1

I have the space and the comma as thousands and decimal separator symbols respectively on my system. I need to get this information in Python. Thus, I thought that I should use localeconv command from locale module for that purpose. However, localeconv command gives me different results: nothing and the point as thousands and decimal separator symbols respectively.

So, my question is: how to get proper system-wide information (thousands and decimal separators symbols, in my case) by means of Python but without invoking any terminal (command prompt) commands (so as to say, without using popen and stuff like that)?

OS-wide separators Separators shown by Python

ENIAC
  • 813
  • 1
  • 8
  • 19
  • Note sure if this is what you want, but you could get the system locale with `os`, eg: `os.popen('locale decimal_point').read()` – RJ Adriaansen Nov 12 '21 at 08:46
  • @RJAdriaansen, thank you for your comment. I know about a `popen` solution but thought that maybe there is another approach without invoking terminal commands. So as to say, a single piece of code that will work on all platforms (Linux, Windows, macOS, etc.). – ENIAC Nov 12 '21 at 12:16

1 Answers1

0

A bit belated, but you have to use setlocale method from the locale module first, I think. As per the docs:

According to POSIX, a program which has not called setlocale(LC_ALL, "") runs using the portable 'C' locale. Calling setlocale(LC_ALL, "") lets it use the default locale as defined by the LANG variable.

Assuming Python 3:

import locale

def get_locale_settings(locale_name: str = "", only_fields: tuple = ("decimal_point", "thousands_sep")) -> dict:
    """Return a dict of locale params.
    
    :param str locale_name: Desired locale name, or empty string for OS default locale.
    :param tuple only_fields: Desired locale fields, or None for all fields.
    
    >>>get_locale_settings(locale_name="en_US.utf8", only_fields=("decimal_point", "thousands_sep"))
    {'decimal_point': '.', 'thousands_sep': ','}  
    """
    locale.setlocale(locale.LC_ALL, locale_name)
    settings = locale.localeconv()
    if settings:
        if only_fields:
            settings = {field: value for field, value in settings.items() if field in only_fields}
    return settings


print(get_locale_settings())

{'decimal_point': ',', 'thousands_sep': '\xa0'}

You can also get all the settings for any desired locale with only_fields=None, say,

get_locale_settings(locale_name="ru_RU.utf8", only_fields=None)

{'int_curr_symbol': 'RUB', 'currency_symbol': '₽', 'mon_decimal_point': ',', 'mon_thousands_sep': '\xa0', 'mon_grouping': [3, 0], 'positive_sign': '', 'negative_sign': '-', 'int_frac_digits': 2, 'frac_digits': 2, 'p_cs_precedes': 0, 'p_sep_by_space': 1, 'n_cs_precedes': 0, 'n_sep_by_space': 1, 'p_sign_posn': 1, 'n_sign_posn': 1, 'decimal_point': ',', 'thousands_sep': '\xa0', 'grouping': [3, 0]}

If you need separators to format a number, it might be easier to just use f-string formatting from this answer.

Anatoly Alekseev
  • 2,011
  • 24
  • 27