5

Depending on the regional settings, the CSV separator (or the list separator) might be ; instead of ,, which, at least on Windows depends on the Regional Settings.

Is there a cross-platform way to detect what the CSV separator is with Qt?

If no cross-platform way is available, is there a Windows-specific way?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • @Ivaylo Strandjev: For example in Finland `,` is used as a decimal separator. That's why some other character (`;`) is needed for the group separator. –  May 23 '13 at 15:35
  • @Roku yes, I figured. I just thought that CSV should be called SSV for instance in this case :) – Ivaylo Strandjev May 23 '13 at 15:42
  • The list separator is a `;` in Bulgaria also. – sashoalm May 23 '13 at 15:43
  • 1
    @Roku: That's why there are quotes: `X,"1,200",950`. Sadly, no one follows RFC 4180. – Joker_vD May 23 '13 at 16:24
  • 1
    The problem with RFC 4180 was that it was created post factum. There really is no standard with CSV files. – sashoalm May 23 '13 at 16:29
  • 1
    @Joker_vD, while you are right, don't forget the often main point is to make your app compatible with existing system. For example, MS Excel will not divide strings in columns, if you pass the separator, it doesn't except. – Lol4t0 May 23 '13 at 16:33

3 Answers3

2

There is QLocale::groupSeparator():

QChar separator = QLocale().groupSeparator();

Edit:

But that it not a correct answer. Group separator is a character used in long numbers between number groups, for example: "1,234.56". In that example group separator is comma and decimal separator is period.

It seems that the QLocale doesn't contain list separator at all. You might try to make a guess according to what decimal separator is used. If decimal separator is . then use , as a CSV separator, if decimal separator is , then use ; as a CSV separator. But I don't know if that covers all languages.

  • Interesting. What uses this? – Phlucious May 23 '13 at 15:37
  • 1
    No, that's wrong. Prints `' '`, that's sure not csv separator: `qDebug() << QLocale(QLocale::Russian, QLocale::RussianFederation).groupSeparator();`. It seems `groupSeparator` is digits groups separator in a number. – Lol4t0 May 23 '13 at 16:03
  • @Roku Thanks, I hadn't come around to testing it actually, it looked so plausible that I just assumed that it would be correct and accepted it immediately. Nevermind, your edited answer is still correct :) – sashoalm May 23 '13 at 16:26
1

For Windows, you can use GetLocaleInfo API to read the list separator.

GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, szBuf, sizeof(szBuf));
Cem Polat
  • 101
  • 1
  • 7
1

In Qt 5.15, there is a function QLocale::createSeparatedList that you can use to create a separated list or to detect the separator (by creating a dummy list and detect what separator has been used)

Example (from comment below)

QLocale l; 
qDebug() << l.createSeparatedList({"1","2","3"}).at(1);
sashoalm
  • 75,001
  • 122
  • 434
  • 781
Fred
  • 11
  • 3
  • ***detect the separator (by creating a dummy list and detect what separator has been used)***, I like this idea, you could make your answer event better if you could provide a code example on how to do this exactly. Perhaps something like this: `QLocale l; qDebug()< – Abderrahmene Rayene Mihoub Jun 21 '23 at 12:12