I have a shapefile that contains three lines. I want to add a specific numeric id to the field "line_id" and a string id to the field "border_id". Basically, for the first line, I want to add the value '1' to "line_id" and 'North' to field "border_id". For the second line I want to add the value '2' to "line_id" and 'South' to field "border_id".

Here is my code, I think I don't get how the changeAttributeValues command works on pyQGIS.

provider = lyr.dataProvider()

enter image description here


I propose to use the following code in case you have two features in the lyrr.

values = {1: 'North', 2: 'South'}
counter = 0
for feat in lyr.getFeatures():
    counter += 1
    feat.setAttribute('line_id', counter)
    feat.setAttribute('border_id', values[counter])

It will work for python 3.6 and qgis 3.2

  • You are welcome – Vadym Dec 22 '18 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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