How to translate ESRI cursor example to pyQgis?

This should get you started

from qgis.core import QgsVectorLayer, QgsFeature
layer = QgsVectorLayer(r"D:\fold\boundingBoxes.shp", "boundingBoxes", "ogr")

road_type_index = layer.fieldNameIndex("road_type")
buffer_distance_index = layer.fieldNameIndex("buffer_distance")

layer.select(layer.pendingAllAttributesList())

layer.startEditing()
for feature in layer:
   newvalue = feature.attributeMap()[road_type_index].toInt()[0] * 100
   feature.changeAttribute(buffer_distance_index, newvalue)
   layer.updateFeature(feature)

layer.commitChanges()

If in 1.7.4 try this

from qgis.core import QgsVectorLayer, QgsFeature
layer = QgsVectorLayer(r"D:\fold\boundingBoxes.shp", "boundingBoxes", "ogr")

road_type_index = layer.fieldNameIndex("road_type")
buffer_distance_index = layer.fieldNameIndex("buffer_distance")

layer.select(layer.pendingAllAttributesList())

layer.startEditing()
for feature in layer:
   newvalue = feature.attributeMap()[road_type_index].toInt()[0] * 100
   layer.changeAttributeValue(feature.id(),buffer_distance_index,newvalue) 

layer.commitChanges()

Tags:

Python

Pyqgis