QGIS 3.2 - Forcing column type when importing csv

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)

Tags:

Csv

Import

Qgis