114

How is a CSV file built in general? With commas or semicolons? Any advice on which one to use?

3
  • 7
    See Comma-separated values (Wikipedia).
    – Jesper
    Apr 13, 2012 at 12:42
  • 7
    CSV -> Comma Seperated Values
    – ChadNC
    Apr 13, 2012 at 13:02
  • @ChadNC I thought of that too when selecting the delimiter that my code would use. Easiest solution was to use ;, which is automatically recognized both in Ubuntu and Windows, without me having to dictate any specific setting(s).
    – gsamaras
    Dec 11, 2018 at 13:07

10 Answers 10

100

In Windows it is dependent on the "Regional and Language Options" customize screen where you find a List separator. This is the char Windows applications expect to be the CSV separator.

Of course this only has effect in Windows applications, for example Excel will not automatically split data into columns if the file is not using the above mentioned separator. All applications that use Windows regional settings will have this behavior.

If you are writing a program for Windows that will require importing the CSV in other applications and you know that the list separator set for your target machines is ,, then go for it, otherwise I prefer ; since it causes less problems with decimal points, digit grouping and does not appear in much text.

5
  • 42
    This change will also affect Opening of CSV files in Excel. If you want to make more compatible for windows CSV file, put sep=; on top of the CSV file.
    – papo
    Dec 30, 2015 at 3:23
  • 17
    > "i prefer ; since it causes less problems with decimal points". This is the wrong solution. If your separator is a comma and a cell's value also contains a comma, you must enclose the value into double quotes. For example: 111,222,"33,5",444,"55,98",666
    – Elmue
    Jul 27, 2016 at 20:41
  • 1
    It is the case for Mac Numbers app as well. I was pulling my hair out because I couldn't make a csv that Photoshop recognized. I changed region option back to US english and it all worked fine again
    – Tibidabo
    May 11, 2017 at 8:30
  • 37
    That regional thing is the most stupid thing it could ever be made. How can format of the csv be different across regions, this means my colleagues cannot open or save csv for guys with a pc in another region .. oucch
    – Miguel
    Jun 1, 2018 at 20:04
  • If you're programming in C or C++ under Windows you can use the following API to retrieve system-wide, or user delimiter: GetLocaleInfo(lcid, LOCALE_SLIST, buff, _countof(buff));
    – ahmd0
    Nov 3, 2018 at 3:21
76

CSV is a standard format, outlined in RFC 4180 (in 2005), so there IS no lack of a standard. https://www.ietf.org/rfc/rfc4180.txt

And even before that, the C in CSV has always stood for Comma, not for semiColon :(

It's a pity Microsoft keeps ignoring that and is still sticking to the monstrosity they turned it into decades ago (yes, I admit, that was before the RFC was created).

  • One record per line, unless a newline occurs within quoted text (see below).
  • COMMA as column separator. Never a semicolon.
  • PERIOD as decimal point in numbers. Never a comma.
  • Text containing commas, periods and/or newlines enclosed in "double quotation marks".
  • Only if text is enclosed in double quotation marks, such quotations marks in the text escaped by doubling. These examples represent the same three fields:

    1,"this text contains ""quotation marks""",3

    1,this text contains "quotation marks",3

The standard does not cover date and time values, personally I try to stick to ISO 8601 format to avoid day/month/year -- month/day/year confusion.

11
  • 31
    The monstruosity would have never appeared if semicolon was chosen in first place. The comma is not only used for decimals or thousands separator, but also very often in text, unlike the semicolon. Semicolon would have been a better choice because it's much more rare...
    – AFract
    Jun 27, 2016 at 9:04
  • 20
    When I said monstrosity, I meant that Microsoft made it language dependent. Excel opens CSV files and treats them as spreadsheets, and can save spreadsheets in CSV format, but a CSV file saved by Excel in the US (for example) will not work if someone in France or Belgium tries to open it, and vice versa. Whatever standard is OK for me, as long as there's a standard that works for everybody.
    – Luc VdV
    Jun 28, 2016 at 10:21
  • 6
    "CSV is a standard format, outlined in RFC 4180 (in 2005), so there IS no lack of a standard." Unfortunately, RFC 4180 is a request for comments, NOT a standard. It says right at the top -- "does not specify an Internet standard of any kind." Later, it says that RFC 4180 "documents the format that seems to be followed by most implementations." It appears that this was created by a small private company, not associated with any standard's body. It's an excellent starting point, but there are unfortunately lots of CSV files out there in the wild that don't follow this "standard."
    – Jim Rea
    Jan 4, 2019 at 21:50
  • 11
    ALL internet standards are called RFC.
    – Luc VdV
    Jan 5, 2019 at 22:10
  • 2
    But when they are standard, they do not state they are not intended to be normative. Instead, they have a number of normative sections within, which is not the case of RFC 4180.
    – Frédéric
    Sep 11, 2019 at 12:28
63

I'd say stick to comma as it's widely recognized and understood. Be sure to quote your values and escape your quotes though.

ID,NAME,AGE
"23434","Norris, Chuck","24"
"34343","Bond, James ""master""","57"
5
  • 6
    +1 for covering a lot of csv scenarios with a very short example Sep 3, 2012 at 12:53
  • 1
    Comma separating on its own is good, but you get problems when you get fields that have commas in them. Here is a good reference i found on the manual
    – Ibu
    May 16, 2013 at 18:35
  • 4
    WRONG! In my machine if I write a CSV file using commas as separators and open it in Excel, I get the whole line to occupy only one column! It must be some Windows regional setting!
    – sergiol
    Jan 4, 2017 at 14:56
  • 3
    @sergiol I'm not sure how that invalidates what they said.
    – AMC
    Apr 9, 2020 at 1:02
  • Seems only the USA and UK use comma delimiters, the rest of the world prefers semicolon, so I wouldn't suggest this answer at all. The better solution is to specify the separator at the first line of the file using sep=; like Excel supports.
    – MasterHD
    Jul 12, 2023 at 9:19
49

Also relevant, but specially to excel, look at this answer and this other one that suggests, inserting a line at the beginning of the CSV with

"sep=,"

To inform excel which separator to expect

1
  • 3
    It does work with Excel, but It is not recognized by Microsoft Power BI. Dec 14, 2017 at 12:32
11

1.> Change File format to .CSV (semicolon delimited)

To achieve the desired result we need to temporary change the delimiter setting in the Excel Options:

Move to File -> Options -> Advanced -> Editing Section

Uncheck the “Use system separators” setting and put a comma in the “Decimal Separator” field.

Now save the file in the .CSV format and it will be saved in the semicolon delimited format.

7

Initially it was to be a comma, however as the comma is often used as a decimal point it wouldnt be such good separator, hence others like the semicolon, mostly country dependant

http://en.wikipedia.org/wiki/Comma-separated_values#Lack_of_a_standard

2
  • That section of the Wikipedia article disappeared. You may want to re-link to en.wikipedia.org/wiki/Comma-separated_values#Basic_rules, This has a passage on locales: > "Adjacent fields must be separated by a single comma. However, "CSV" formats vary greatly in this choice of separator character. In particular, in locales where the comma is used as a decimal separator, semicolon, TAB, or other characters are used instead." May 28, 2019 at 10:26
  • Nice overview on available "standards" is given at w3c.github.io/csvw/syntax/#standards.
    – koppor
    Apr 15, 2023 at 21:41
4

CSV is a Comma Seperated File. Generally the delimiter is a comma, but I have seen many other characters used as delimiters. They are just not as frequently used.

As for advising you on what to use, we need to know your application. Is the file specific to your application/program, or does this need to work with other programs?

3
  • 8
    I think in Europe the ; is prevalent. Mostly because we have commas in numbers like 60,00€.
    – oers
    Apr 13, 2012 at 12:43
  • 7
    @oers It shouldn't matter as long as you "quote" your values.
    – adarshr
    Apr 13, 2012 at 12:45
  • Well, I mostly saw other delimiters used in networking seedfiles, but European money works too!
    – Youssef G.
    Apr 13, 2012 at 12:45
3

To change comma to semicolon as the default Excel separator for CSV - go to Region -> Additional Settings -> Numbers tab -> List separator and type ; instead of the default ,

1
  • 1
    If your regional settings are set with , as decimal separator and you export a file with columns separated by , when you open it In Excel, text will appear in ONLY ONE column. In Excel 2013, select the first column, go to DATA -> Text To Columns -> Choose Delimited, click Next then choose onlyComma on Delimiters and you will see changes immediately in the Data preview and finally click Finish
    – sergiol
    Jan 4, 2017 at 15:15
2

Well to just to have some saying about semicolon. In lot of country, comma is what use for decimal not period. Mostly EU colonies, which consist of half of the world, another half follow UK standard (how the hell UK so big O_O) so in turn make using comma for database that include number create much of the headache because Excel refuse to recognize it as delimiter.

Like wise in my country, Viet Nam, follow France's standard, our partner HongKong use UK standard so comma make CSV unusable, and we use \t or ; instead for international use, but it still not "standard" per the document of CSV.

-3

best way will be to save it in a text file with csv extension:

Sub ExportToCSV()
Dim i, j As Integer
Dim Name  As String

Dim pathfile As String

Dim fs As Object
    Dim stream As Object

    Set fs = CreateObject("Scripting.FileSystemObject")
On Error GoTo fileexists

i = 15
Name = Format(Now(), "ddmmyyHHmmss")
pathfile = "D:\1\" & Name & ".csv"

Set stream = fs.CreateTextFile(pathfile, False, True)

fileexists:

If Err.Number = 58 Then
    MsgBox "File already Exists"
    'Your code here
    Return
End If
On Error GoTo 0

j = 1
Do Until IsEmpty(ThisWorkbook.ActiveSheet.Cells(i, 1).Value)

    stream.WriteLine (ThisWorkbook.Worksheets(1).Cells(i, 1).Value & ";" & Replace(ThisWorkbook.Worksheets(1).Cells(i, 6).Value, ".", ","))

    j = j + 1
    i = i + 1
Loop


stream.Close

End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.