How to split a comma-separated list...

Comma-Separated Value (CSV) format is commonly output by spreadsheets and databases when dumping data into plain-text files. It is important to understand the formal definition of a CSV file.

  1. Each record is a series of fields.
  2. Records are separated by line terminators (CR, LF, or CRLF, depending on the underlying file-system text format).
  3. Fields within records are separated by commas and perhaps also spaces.
  4. Fields with imbedded commas are enclosed in ASCII doublequote characters.
  5. Fields with leading or trailing spaces are enclosed in ASCII doublequotes.
  6. Fields with embedded doublequotes are enclosed in doublequotes and each interior doublequote is doubled.
Here is an example:
aaa,bbb,ccc,,"ddd,eee,fff",ggg,hhh," iii ","Muhammad ""The Greatest"" Ali"
The first three are regular fields. The fourth is an empty field, but still a field. The fifth is a field that contains embedded commas. The sixth has leading and trailing spaces. The last field has embedded quotation marks.