Skip to main content
1 of 3
GeorgeC
  • 8.3k
  • 7
  • 56
  • 143

PyQGIS changeAttributeValue not saving modified records

I've been trying to save some changes to the a dataset using PyQGIS but I can't see any changes even after closing/reloading the layer attribute table.

I have tried a few different options but none of them work.

import re
layer = iface.activeLayer()
selection = layer.getFeatures() #all features
#selection = layer.selectedFeatures() #only selected features
layer_provider=layer.dataProvider()
layer.startEditing()
for f in selection:
   text=f['RoadMntnc']
   print (text)
   stext=re.split('_+', text)
   print(stext)
   rtype=stext[-1:]
   newtext=' '.join(stext[:-1])
   print (ntext)
   attr_value={2:newtext}
   #layer_provider.changeAttributeValues({1:attr_value})
   #layer.changeAttributeValue(f.id(), RoadName, 30)
   print (f.id())
   layer.changeAttributeValue(f.id(), 2, newtext)
layer.commitChanges()
layer.updateFields()

Dataset looks like enter image description here

GeorgeC
  • 8.3k
  • 7
  • 56
  • 143