I am trying to write a short snippet of code to break a shapefile up according to a specific attribute.
Here is my code:
from qgis.core import *
import processing
layer = processing.getObject('2012')
#counter is 15 due to limit R15
counter = 1
# Write the while filter expression and set it
while (counter <= 15):
selection = layer.getFeatures(QgsFeatureRequest().setFilterExpression('"Group Comp" LIKE \'R'+str(counter)+'\' '))
layer.setSelectedFeatures([k.id() for k in selection])
#Creates layer from selection
_writer = QgsVectorFileWriter.writeAsVectorFormat(i,r"E:\Scratch\test\NewFile"+str(counter)+".shp","utf-8",None,"ESRI Shapefile", True)
counter = counter + 1
I get the following error in Qgis when running the above code:
Traceback (most recent call last):
File "<input>", line 5, in <module>
NameError: name 'i' is not defined
Is my selection of the layer incorrect?
layerinstead ofi. However, I'm not sure that the selection is applied in that way...whileloop =)