Configuring CSV parsing and writing

suggest change

RFC 4180 documents a common CSV format, but real-world files use many variants.

Package encoding/csv supports most common CSV formats and lets you adjust how records are read and written.

Configuring CSV Reader

After you create csv.Reader with csv.NewReader(), you can set the following fields to change the behavior.

Comma

Most CSV files use , to separate fields, but other characters are used too. If a file uses ; as the separator, set r.Comma = ';'.

Comment

If you want to treat some CSV as comments and ignore them during reading, you can set a comment character.

For example if CSV file is:

# Comment
2013-02-08,15.07,AAL

you can ignore comment lines by setting r.Comment = '#'.

By default the reader does not treat any character as starting a comment, so a line such as # Comment is parsed as data.

FieldsPerRecord

Each line in a CSV file (a record) can have a different number of fields.

If you know that e.g. CSV file you’re parsing always has 5 fields in each record (line) then set r.FieldsPerRecord = 5. Read() will return an error if there’s a mismatch.

If you don’t know how many fields there are but know that it’s always the same number, use r.FieldsPerRecord = 0. This is a default so you don’t have to do it explicitly.

In that case csv.Reader will use the first line to detect number of fields and will return an error if subsequent records have a different number of fields.

If you want to allow a variable number of fields per record, set r.FieldsPerRecord = -1.

LazyQuotes

By default false.

If true, csv.Reader is more lax about parsing of quoted values i.e. a quote may appear in an unquoted field and a non-doubled quote may appear in a quoted field.

TrimLeadingSpace

By default false.

If true, leading white space in a field is ignored.

ReuseRecord

By default false.

If true, the []string slice returned by Read() might be reused across Read() calls.

This is faster but you have to be more careful when using the result.

Configuring CSV writer

After you create csv.Writer with csv.NewWriter(), you can set the following fields to change the behavior.

Comma

Field delimiter, , by default.

UseCRLF

False by default.

If true, uses Windows style line terminator (CRLF i.e. \r\n).

By default uses Unix style line terminator (LF i.e. \n).

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents