Skip to content

Commit

Permalink
Merge pull request #8822 from OSGeo/backport-8813-to-release/3.8
Browse files Browse the repository at this point in the history
[Backport release/3.8] CSV writer: do not quote integer fields by default (only if STRING_QUOTING=ALWAYS is specified)
  • Loading branch information
rouault authored Nov 28, 2023
2 parents 05f2068 + b4f5eb6 commit 12ed8de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 16 additions & 4 deletions autotest/ogr/ogr_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2633,7 +2633,7 @@ def test_ogr_csv_string_quoting_always(tmp_vsimem):
data = gdal.VSIFReadL(1, 10000, f).decode("ascii")
gdal.VSIFCloseL(f)

assert data.startswith('"AREA","EAS_ID","PRFEDEA"\n215229.266,"168","35043411"')
assert data.startswith('"AREA","EAS_ID","PRFEDEA"\n"215229.266","168","35043411"')

ds = gdal.OpenEx(
tmp_vsimem / "ogr_csv_string_quoting_always.csv",
Expand All @@ -2653,7 +2653,7 @@ def test_ogr_csv_string_quoting_always(tmp_vsimem):
gdal.VSIFCloseL(f)

assert data.startswith(
'"AREA","EAS_ID","PRFEDEA"\n215229.266,"168","35043411"\n247328.172,"179","35043423"'
'"AREA","EAS_ID","PRFEDEA"\n"215229.266","168","35043411"\n"247328.172","179","35043423"'
)


Expand All @@ -2667,10 +2667,16 @@ def test_ogr_csv_string_quoting_if_ambiguous(tmp_vsimem):
lyr.CreateField(ogr.FieldDefn("foo"))
lyr.CreateField(ogr.FieldDefn("bar"))
lyr.CreateField(ogr.FieldDefn("baz"))
lyr.CreateField(ogr.FieldDefn("intfield", ogr.OFTInteger))
lyr.CreateField(ogr.FieldDefn("int64field", ogr.OFTInteger64))
lyr.CreateField(ogr.FieldDefn("realfield", ogr.OFTReal))
f = ogr.Feature(lyr.GetLayerDefn())
f["foo"] = "00123"
f["bar"] = "x"
f["baz"] = "1.25"
f["intfield"] = 1
f["int64field"] = 1234567890123
f["realfield"] = 1.25
lyr.CreateFeature(f)

gdal.VectorTranslate(
Expand All @@ -2681,7 +2687,7 @@ def test_ogr_csv_string_quoting_if_ambiguous(tmp_vsimem):
data = gdal.VSIFReadL(1, 10000, f).decode("ascii")
gdal.VSIFCloseL(f)

assert '"00123",x,"1.25"' in data
assert '"00123",x,"1.25",1,1234567890123,1.25' in data

gdal.Unlink(tmp_vsimem / "ogr_csv_string_quoting_if_ambiguous.csv")

Expand All @@ -2696,10 +2702,16 @@ def test_ogr_csv_string_quoting_if_needed(tmp_vsimem):
lyr.CreateField(ogr.FieldDefn("foo"))
lyr.CreateField(ogr.FieldDefn("bar"))
lyr.CreateField(ogr.FieldDefn("baz"))
lyr.CreateField(ogr.FieldDefn("intfield", ogr.OFTInteger))
lyr.CreateField(ogr.FieldDefn("int64field", ogr.OFTInteger64))
lyr.CreateField(ogr.FieldDefn("realfield", ogr.OFTReal))
f = ogr.Feature(lyr.GetLayerDefn())
f["foo"] = "00123"
f["bar"] = "x"
f["baz"] = "1.25"
f["intfield"] = 1
f["int64field"] = 1234567890123
f["realfield"] = 1.25
lyr.CreateFeature(f)

gdal.VectorTranslate(
Expand All @@ -2713,7 +2725,7 @@ def test_ogr_csv_string_quoting_if_needed(tmp_vsimem):
data = gdal.VSIFReadL(1, 10000, f).decode("ascii")
gdal.VSIFCloseL(f)

assert "00123,x,1.25" in data
assert "00123,x,1.25,1,1234567890123,1.25" in data


###############################################################################
Expand Down
4 changes: 3 additions & 1 deletion ogr/ogrsf_frmts/csv/ogrcsvlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2309,7 +2309,9 @@ OGRErr OGRCSVLayer::ICreateFeature(OGRFeature *poNewFeature)
{
const OGRFieldType eType(
poFeatureDefn->GetFieldDefn(iField)->GetType());
if (eType == OFTReal)
if ((eType == OFTReal || eType == OFTInteger ||
eType == OFTInteger64) &&
m_eStringQuoting != StringQuoting::ALWAYS)
{
if (poFeatureDefn->GetFieldDefn(iField)->GetSubType() ==
OFSTFloat32 &&
Expand Down

0 comments on commit 12ed8de

Please sign in to comment.