3

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.

  • @Luke - it's not me that's doing the Excel import. It downstream users that I have no personal contact with. But I understand your point. – rgwozdz Apr 23 '18 at 12:24
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.