6

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 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 load vectorlayer, but programmatically?

Can a CSV data-table be loaded via pyqgis without geometry?

7

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
    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 Feb 7 '15 at 18:58
3

Wow. Way simpler than I expected. 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, privacy policy and cookie policy

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