Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSV and fields with spaces

Tags:

csv

perl

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?

like image 452
Jim Avatar asked Feb 13 '23 20:02

Jim


1 Answers

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.

like image 79
James Green Avatar answered Feb 27 '23 01:02

James Green