About Comma-Separated Values (CSV) Files [RFC 4180].
MIME type is text/csv with 2 optional parameters: charset and header:
charset by default is US-ASCII but any another IANA text one can be specified. UTF-8 being the most common.
header by default is 'present' (can be specified 'absent'). Not supported by most software CSV processing utilities.
Recommended:
Content-Type: text/csv; charset=utf-8 //Very standard and supported, assumes header=present.
Others:
Content-Type: text/csv; charset=utf-8; header=present //Correct but usually unsupported. Since .csv files usually have a header row, it is unnecessary.
Content-Type: text/csv; charset=utf-8; header=absent //Correct but usually unsupported. Most .csv files have a header row, probably will never need it.
Content-Type: text/csv //Correct default charset US-ASCII and header row present. Using UTF-8 is nowadays more common.
Line breaks:
Although the media type uses CRLF, some implementations may use other values.
Recommended:
LF //Not the default CRLF but more efficient and the standard for most modern systems and programming languages
Others:
CRLF //The media type defined one, but LF is very common
CR //Was used by Mac OS 9 and earlier
If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with
another double quote. For example:
"aaa","b""bb","ccc"
The CSV format itself is a simple text format and does not define data types. It treats all values as strings. Therefore, the interpretation of a type is left to the application or system that is reading the file.
Quotes are only required if a field contains the delimiter itself, a newline, or the quote character.
Sample:
"name", "birthday" , "age", "deathday", "wage", "appointment" , "remarks"
"John","2020-08-20", 29 , , 27.72 , "2020-02-10T15:30:59Z" , "He said ""Hello World!"""
Typical convention:
Let's generate .csv as indicated by column "GENERATING".
Let's read .csv in a flexible way, see column "REMARKS".
DATA_TYPE GENERATING REMARKS
null Unquoted (leave the field blank)
string "aaa" Read quoted or unquoted
boolean false | true Read case-insensitive, quoted or unquoted
number 27 | 33.72 No thousands separator. Read quoted or unquoted
date "2020-08-20" | "2020-08-20+02:00" Local or w/ timezone. Read quoted or unquoted
time "15:30:59" | "15:30:59+02:00" Local or w / timezone. Read quoted or unquoted
datetime "2020-02-10T15:30:59" | "2020-02-10T15:30:59+02:00" Local or w / timezone. Read quoted or unquoted