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.
- Each record is a series of fields.
- Records are separated by line terminators (CR, LF, or CRLF, depending
on the underlying file-system text format).
- Fields within records are separated by commas and perhaps also spaces.
- Fields with imbedded commas are enclosed in ASCII doublequote characters.
- Fields with leading or trailing spaces are enclosed in ASCII doublequotes.
- 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.