11

This might be a simple problem, but how does one load a simple two-column CSV with no associated geometric data?

I have given a couple of go's via QgsVectorlayer[delimited text, spatialite] types - but none seem to work.

for example:

theMinistryOf = ':\...\sillyWalks.csv'
uri = QgsDataSourceURI()
uri.setDatabase(theMinistryOf)
uri.setDataSource('','','')
notQuiteSillyEnough = QgsVectorLayer(uri.uri(), 'skip_step_slide', 'spatialite')

or:

uri = theMinistryOf+'?delimiter=%s' % (",")
sillyWalks = QgsVectorLayer(uri, 'sillyWalks', "delimitedtext")
QgsMapLayerRegistry.instance().addMapLayer(sillyWalks)

This need is easily fulfilled manually by selecting the load vector layer, but programmatically?

Can a CSV data table be loaded via PyQGIS without geometry?

2 Answers 2

11

The following snippet works for me:

uri = "file:///C:/testdata/somecsv.csv?delimiter=%s" % (";")
lyr = QgsVectorLayer(uri, 'New CSV','delimitedtext')
QgsMapLayerRegistry.instance().addMapLayer(lyr)

For reference, if you wanted to add it with geometry:

uri = "file:///C:/testdata/somecsv.csv?delimiter=%s&crs=epsg:4326&xField=%s&yField=%s" % (";", "x", "y")
lyr = QgsVectorLayer(uri, 'New CSV','delimitedtext')
QgsMapLayerRegistry.instance().addMapLayer(lyr)

Most importantly, make sure the correct delimiter has been specified!

1
  • 1
    I have to give you points for posting so quick with responses which indeed work. But I have figured out another solution - also posed for others references.
    – Katalpa
    Commented Feb 7, 2015 at 18:58
5

Should not have supposed that 'ogr' wouldn't be able.

someTableLayer = QgsVectorLayer(ministryOf.csv, 'sillyWalks', 'ogr')
QgsMapLayerRegistry.instance().addMapLayer(someTableLayer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.