1

I have some csv lookup tables which are too heterogeneous for ordinary join so I want to process them in PyQGIS. However the csv files have no geometry column whatsoever. If used as QgsVectorLayer the layer ends up empty:

uri='path/to/csv/file.csv?delimiter=;'
csvLyr = QgsVectorLayer(uri, "csvLayer", "delimitedtext")
QgsProject.instance().addMapLayer(csvLyr) #throws 'source data cannot be found'
csvLyr.getFeature(0) #returns a non callable QgsFeature

Is there a way in PyQGIS to query and manipulate non-spatial csv data just like any other vector data (respectively just like in the GUI) or am I required to use the csv module/another Python library?

I am using Python/PyQGIS 3 in QGIS 3.6. The csv files all have header rows.

  • 1
    How did you set your uri? Make sure it begins with file:/// so that your path looks something like: uri = 'file:///C:/Users/You/Desktop/points.csv?delimiter=;' – Joseph Mar 7 at 10:40
  • Thanks, that was the issue. I always assumed this prefix for the path is a windows only thing and also it is not required when adding other data sources... Anyway feel free to paste your comment as answer so i can mark it. – maxwhere Jul 14 at 19:59
  • Most welcome, glad it helped :) – Joseph Jul 16 at 10:09
1

You must include file:/// as a prefix in the path. So your path should look something like:

uri = 'file:///C:/Users/You/Desktop/points.csv?delimiter=;'

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.