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 = ';'
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 (|).
Provided the idea to read the list separator symbol from the Windows 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With