1

I've got this csv file that I'm trying to import to QGIS 3.2. It's just a data table with no geometry attached.

When I just drag it from the Browser on the left-hand side, all the columns get incorrectly read in as string. However, when I use the "Open Data Source Manager" and import the file through there, the column types are all correct (first one is integer and the others are double). But I really didn't specify that the columns should be double instead of string.

So, finally, my question is: how/where can I try to force certain column type at import for each of my columns?

Also, extra brownie points to whoever knows what's going on in the background that makes dragging a CSV from the Browser different from using the "Open Data Source Manager".

  • Importing CSV doesn’t automatically make it a string if you import it as delimited text (icon with comma) but I have had problems with integer columns getting recognized as strings when they have commas for 000 breaks. I fixed this in my case by opening CSV in Excel, choosing format cell on target column and turning on and then off ,000 which removes all the commas without need for regex search and removal. Then save as CSV and re-Import (assuming that was the problem) – Konrad Lawson Oct 14 '18 at 8:58
4

QGIS uses OGR in the background, and OGR interprets all columns as strings.

The OGR CSV driver returns all attribute columns as string data types if no field type information file (with .csvt extension) is available.

Using CSVT file with the same name as the CSV file allows you to specify types of columns in a CSV file.

Limited type recognition can be done for Integer, Real, String, Date (YYYY-MM-DD), Time (HH:MM:SS+nn), DateTime (YYYY-MM-DD HH:MM:SS+nn) columns through a descriptive file with the same name as the CSV file, but a .csvt extension. In a single line the types for each column have to be listed with double quotes and be comma separated (e.g., "Integer","String"). It is also possible to specify explicitly the width and precision of each column, e.g. "Integer(5)","Real(10.7)","String(15)".

Difference between dragging a CSV from the Browser and using the "Open Data Source Manager" is that "Open Data Source Manager (ODSM)" guesses data types of columns.

Sample .csv and .csvt file:

sample.csv

1,high,2.3
2,low,5
3,low,7.2

sample.csvt

"Integer(5)","String(10)","Real(3.5)"

If you import the file to QGIS using Browser or ODSM, the attribute table looks like this:

ss

Further information, please look at Comma Separated Value (.csv)

  • 2
    GDAL can also guess the types if special open option is defined gdal.org/drv_csv.html AUTODETECT_TYPE=YES/NO (defaults to NO). Setting it to YES will enable auto-detection of field data types. If while reading the records (beyond the records used for autodetection), a value is found to not correspond to the autodetected data type, a warning will be emitted and the field will be emptied. – user30184 Oct 12 '18 at 20:41

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.