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
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!
-
1I 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– Katalpa2015年02月07日 18:58:27 +00:00Commented Feb 7, 2015 at 18:58
Should not have supposed that 'ogr'
wouldn't be able.
someTableLayer = QgsVectorLayer(ministryOf.csv, 'sillyWalks', 'ogr')
QgsMapLayerRegistry.instance().addMapLayer(someTableLayer)