[Qgis-user] Csv file.

Richard Duivenvoorde rdmailings at duif.net
Mon May 26 10:08:05 PDT 2014


On 26-05-14 18:24, Ourizo Cacho wrote:
> Hello everyone,
> I have a *csv file that I´m trying to add as an attribute only table.
> The question is that QGIS only recognizes the name of the first field.
> The others are named, field_2, field_3 and so on... The rest of the
> table, including attibutes are ok.
> How can I get the correct names of the fields into a new QGIS attribute
> table??
> Any help, please?

Hi, QGIS uses gdal to open csv files, more specifically this csv driver:

http://www.gdal.org/drv_csv.html

you can easily test that it is not QGIS fault by doing:

ogrinfo -al test.csv

from that page:

The driver attempts to treat the first line of the file as a list of
field names for all the fields. However, if one or more of the names is
all numeric it is assumed that the first line is actually data values
and dummy field names are generated internally (field_1 through field_n)
and the first record is treated as a feature. Starting with GDAL 1.9.0
numeric values are treated as field names if they are enclosed in double
quotes.

so either (as other say here) make all names start with a alfanumeric,
OR put quotes around them;

With original one:

ogrinfo -al test.csv
INFO: Open of `test.csv'
      using driver `CSV' successful.

Layer name: test
Geometry: None
Feature Count: 3
Layer SRS WKT:
(unknown)
field_1: String (0.0)
field_2: String (0.0)
field_3: String (0.0)
OGRFeature(1):1
  field_1 (String) = NAME
  field_2 (String) = 2013
  field_3 (String) = 2014

Double quoted first line:

ogrinfo -al quoted.csv
INFO: Open of `quoted.csv'
      using driver `CSV' successful.

Layer name: quoted
Geometry: None
Feature Count: 2
Layer SRS WKT:
(unknown)
NAME: String (0.0)
2013: String (0.0)
2014: String (0.0)
OGRFeature(2):1
  NAME (String) = REGION1
  2013 (String) = 1000
  2014 (String) = 2000

Regards,


Richard Duivenvoorde



More information about the Qgis-user mailing list