Osgeo.ogr not getting csv field name correctly

142 views Asked by At

Osgeo.ogr seems not able to correctly get the field name of a csv file. Take this example:

Identifian,C,254;CodeVoieRi,C,254;CodeInseeI,N,10,0
1;;13021
2;;13021
3;;13021

The problem is that ogr take the "," and the ";" as delimiters while the "," are just part of the column name.

Here is the python code i use:

dataSource = ogr.Open(file)
daLayer = dataSource.GetLayer(0)
layerDefinition = daLayer.GetLayerDefn()
for i in range(layerDefinition.GetFieldCount()):
     name = layerDefinition.GetFieldDefn(i).GetName()

Is it possible to specify to ogr to use a single delimiter?

Thanks!

1

There are 1 answers

1
kaycee On

I would suggest to never use commas in field names as it will often create conflicts when trying to parse a file. Using underscores _ instead would be a better method to name fields.

If your goal is to create a shapefile from this CSV file, you can look at this example to see how you can parse correctly your file using a specificied delimiter and convert the data to a shapefile.

https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#create-a-new-shapefile-and-add-data