Is there a way to force ogr2ogr to wrap string-column values with double-quotes? I have a column with string values like "000010001", which OGR correctly interprets as a string when writing to CSV. However, Excel converts that value to a numeric 10001 when parsing the CSV, due to a lack double-quotes wrapping the value.
1 Answer
From the docs for ogr2ogr
and the CSV driver it doesn't look possible to specify quoting text fields on output.
You might like to try converting to XLSX format:
ogr2ogr -f XLSX test.xlsx test.csv
Or you can force quotes using OGR SQL, but be aware they get quoted again on output:
$ ogr2ogr -sql "select concat('\"', a, '\"') as A, B, C from test" -f csv /vsistdout/ test.csv
A,B,C
"""0123""",12,a
"""0234""",123,b
"""2345""",1234,c
"""0101""",12345,d
if you are using a Mac/Linux PC you can strip the triple double quotes by piping to sed (or awk or use bash variable manipulation)
$ ogr2ogr -sql "select concat('\"', a, '\"') as A, B, C from test" -f csv /vsistdout/ test.csv | sed -e 's/\"\"\"/\"/g'
A,B,C
"0123",12,a
"0234",123,b
"2345",1234,c
"0101",12345,d