Jim
Jim

Reputation: 19552

CSV and fields with spaces

I am using Text::CSV_XS to create CSV files. I see that if a field has a space then it is outputted within double quotes e.g.

john,smith,"Some address",,,,bla  

I was wondering are the double quotes mandatory in the example? Or is it some configuration option?

Upvotes: 4

Views: 1897

Answers (2)

Subbeh
Subbeh

Reputation: 924

you can set quote_char to change this behaviour:

quote_char

The character to quote fields containing blanks or binary data, by default the double quote character ("). A value of undef suppresses quote chars (for simple cases only). Limited to a single-byte character, usually in the range from 0x20 (space) to 0x7E (tilde).

quote_char can not be equal to sep_char.

from https://metacpan.org/pod/Text::CSV_XS#new

Upvotes: 3

James Green
James Green

Reputation: 1753

As Subbeh has already suggested, you can set quote_char to undef when calling new, to suppress this, as per https://metacpan.org/pod/Text::CSV_XS#new

I'd question whether you should, though. In the CSV specification, https://www.rfc-editor.org/rfc/rfc4180, these quotes are always permitted and sometimes necessary (although strictly only when the field contains a separator character or a quote character itself). Since it's perfectly valid for them to be there, and any CSV-parsing tool you pass the data to later will cope ... I'd be inclined to let Text::CSV do its thing.

In particular, if you set quote_char to undef as suggested, fields which contain sep_char (comma, usually) will lead to breakage.

Edit: You can set quote_space to a false value in your call to new to prevent this specific behaviour (of quoting fields with spaces in), which the CSV spec neither mandates nor prohibits.

Upvotes: 5

Related Questions