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?

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Feb 7, 2015 at 18:37

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!

Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Feb 7, 2015 at 18:54
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. 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)
Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Feb 7, 2015 at 19:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.