Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python csv list separator based on regional settings

Tags:

python

csv

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 that excel can read the CSV file).

I've found out that Excel takes CSV elements separator from "Regional Options -> Numbers -> List separator". locale module in Python is used to detect cultural settings, but it (locale.localeconv) does not contain list separator. Opening CSV writer with dialect='excel' does not help. Any idea how to get the correct separator?

EDIT

The following code seems to work (but can't accept any upvotes as the solution is not mine)

import locale

langlocale = locale.getdefaultlocale()[0]
locale.setlocale(locale.LC_ALL, langlocale)
dp = locale.localeconv()['decimal_point']
delimiter = ','
if dp == ',':
    delimiter = ';'
like image 695
bitman Avatar asked Oct 13 '11 08:10

bitman


People also ask

Can CSV file have different delimiter?

A CSV file stores data in rows and the values in each row is separated with a separator, also known as a delimiter. Although the file is defined as Comma Separated Values, the delimiter could be anything. The most common delimiters are: a comma (,), a semicolon (;), a tab (\t), a space ( ) and a pipe (|).


1 Answers

  • How to read 'List separator' from OS in Java?

Provided the idea to read the list separator symbol from the Windows registry.

  • Python code to read registry

Provided the code to access Windows Registry values.

Using the _winreg package, the Windows list separator value can be retrieved from the registry as follows:

from _winreg import *

def getListSeparator():
    '''Retrieves the Windows list separator character from the registry'''
    aReg = ConnectRegistry(None, HKEY_CURRENT_USER)
    aKey = OpenKey(aReg, r"Control Panel\International")
    val = QueryValueEx(aKey, "sList")[0]
    return val

print getListSeparator()
like image 186
Bert La Maire Avatar answered Sep 23 '22 12:09

Bert La Maire