python csv list separator based on regional settings

前端 未结 3 1699
臣服心动
臣服心动 2021-01-12 03:48

how to detect list separator in users machine with Python?

CSV file needs to be created on users machine and the list separator must be detected automatically (so th

3条回答
  •  一整个雨季
    2021-01-12 04:28

    Write an XLS file with xlwt.

    Take 2: Use the locale module and some heuristics:

    >>> import locale
    >>> locale.setlocale(locale.LC_ALL, '') # set to user's locale, not "C"
    'English_Australia.1252'
    >>> dec_pt_chr = locale.localeconv()['decimal_point']
    >>> if dec_pt_chr == ",":
    ...     list_delimiter = ";"
    ... else:
    ...     list_delimiter = ","
    ...
    >>> print repr(dec_pt_chr), repr(list_delimiter)
    '.' ','
    >>> locale.setlocale(locale.LC_ALL, 'French_France.1252')
    'French_France.1252'
    >>> dec_pt_chr = locale.localeconv()['decimal_point']
    >>> if dec_pt_chr == ",":
    ...     list_delimiter = ";"
    ... else:
    ...     list_delimiter = ","
    ...
    >>> print repr(dec_pt_chr), repr(list_delimiter)
    ',' ';'
    >>>
    

提交回复
热议问题