0

The code below is not working: This code doesn't affect the table at all

layer = point_layer3
selected_feature = layer.selectedFeatures()
print(len(selected_feature))
layer.startEditing()

for feature in selected_feature:
    a = f['parkeer_approx']

    b = parking_capacity[f.id()-int(1)]['parking_capacity']
    feature["parkeer_approx"] = a - b
    layer.updateFeature(feature)

layer.commitChanges()
6
  • Possible duplicate of How to update the attribute table of feature using PyQGIS? – vagvaf Aug 1 '19 at 13:11
  • Do you get an actual python error or the code just doesn't update the attribute values? – vagvaf Aug 1 '19 at 13:13
  • 1
    What errors do you recieve when you run it? I'm pretty sure it should be a = feature['parkeer_approx']. What are you trying to calculate with b? – Joseph Aug 1 '19 at 13:14
  • a and b are the no of parkings actually where a is total parkings and b is alloted parking for that specific id so i want the total - alloted to be updated in the table – Chetan Vashisth Aug 2 '19 at 3:39
  • You got it right @vagvaf there is no actual python error but the code seems not to be working – Chetan Vashisth Aug 2 '19 at 3:44
3

Similar to what @Joseph pointed out in the comments, in the lines where you set a and b did you mean to use "f" instead of "feature"? Try the updates below:

for feature in selected_feature:
    a = feature['parkeer_approx']

    b = parking_capacity[feature.id()-int(1)]['parking_capacity']
    feature["parkeer_approx"] = a - b
    layer.updateFeature(feature)

Not the answer you're looking for? Browse other questions tagged or ask your own question.