Best way to parse a csv...... a csv that has CRLF in the fields

M

Martin Gregorie

There are many implementation out there. You don't have to roll your own
unless you find writing finite state automata and parsers fun.

My own has configurable magic letters for comment intro, separator,
quote char and a few other variations.
Mine is a clone of a C version I'd written first. I have a configurable
separator, choice of every field quoted or 'lazy quoting (only fields
containing the separator or the quote mark get quoted). The quote char
isn't configurable. I use '"', but gets duplicated if its inside a
field. I don't allow newlines inside a field.

The main objective was to be compatible with most RDBMs and spreadsheets
and to have each function/method execute a single action. I've never
personally seen newlines in fields consumed or produced by either, so I
disallowed them.

The line must be read with Getline() before the it is parsed, so spotting
comment lines, etc is trivial. Fields can be ignored or read out of
sequence: parsing a line and extracting field values are separate
operations.

Needless to say, both library and class can read and write CSV files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top