QGIS 3.2 – Forcing Column Type When Importing CSV

csvimportqgis

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".

Best Answer

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)".

The 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 files:

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)

Related Question