<div dir="ltr"><div dir="ltr">Thanks for the help Nyall, but I still don't get any result. <div>I'll put the whole script here, maybe I'm doing something wrong on another level (I'm rather new in coding):</div><div><br></div><div><div>from PyQt5.QtCore import *</div><div>from qgis.core import *</div><div><br></div><div>canvas = qgis.utils.iface.mapCanvas()</div><div>my_layers = canvas.layers()</div><div><br></div><div>for a_layer in my_layers:</div><div>    a_layer.startEditing()</div><div>    a_layer.addAttribute (QgsField('perc_nr', QVariant.String))</div><div>    a_layer.commitChanges()</div><div><br></div><div>    my_features = a_layer.getFeatures()</div><div>    an_expr = QgsExpression('''to_string(  to_int( substr(  "CAPAKEY" , 7, 4)))+' ' +  CASE WHEN substr(  "CAPAKEY" , 14, 1) = '_'  THEN '' else  lower(substr(  "CAPAKEY" , 14, 1)) END + ' ' +  CASE WHEN to_int( substr(  "CAPAKEY" , 15, 3)) = '0'  THEN ''  else  to_string(  to_int( substr(  "CAPAKEY" , 15, 3)))  END  + ' ' +  CASE WHEN to_int( substr(  "CAPAKEY" , 12, 2)) = '0'   THEN '' else '/' + to_string( to_int( substr(  "CAPAKEY" , 12, 2))) END''')</div><div>    context = QgsExpressionContext()</div><div>    for a_feat in my_features:</div><div>        my_index = a_feat.fieldNameIndex('perc_nr')</div><div>        context.setFeature(a_feat)</div><div>        value = an_expr.evaluate(context)</div><div>        a_feat.setAttribute(my_index, value)</div><div>    </div><div>    a_layer.commitChanges()</div></div><div><br></div><div>The first part - ading the attribute - works fine, but filling in the fields don't give any result (nor an error either).</div><div>I'm working in QGIS 3.</div><div><br></div><div>Thanks!</div><div>Wouter</div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Op do 7 feb. 2019 om 08:08 schreef Nyall Dawson <<a href="mailto:nyall.dawson@gmail.com">nyall.dawson@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Wed, 6 Feb 2019 at 18:14, Wouter Impens <<a href="mailto:wouter.impens@gmail.com" target="_blank">wouter.impens@gmail.com</a>> wrote:<br>
>> > I'm trying to iterate over the fields of a vector layer (using the index) and fill in the values by an expression<br>
>> ><br>
>> >  my_features = a_layer.getFeatures()<br>
>> >     for a_feat in my_features:<br>
>> >         mijn_index = a_feat.fieldNameIndex('perc_nr')<br>
>> >         value = an_expr<br>
<br>
Change this to:<br>
<br>
my_features = a_layer.getFeatures()<br>
context = QgsExpressionContext()<br>
for a_feat in my_features:<br>
    mijn_index = a_feat.fieldNameIndex('perc_nr')<br>
    context.setFeature(a_feat)<br>
    value = an_expr.evalute(context)<br>
    ...<br>
<br>
Nyall<br>
<br>
<br>
>><br>
>> What's "an_expr" here? Is it a QgsExpression object?<br>
>><br>
>> Nyall<br>
>><br>
>> >          a_feat.setAttribute(mijn_index, value)<br>
>> ><br>
>> >     a_layer.commitChanges()<br>
>> ><br>
>> > I don't get an error but nothing is filled in. I evaluated the expression and it's true. It also works in the field calculator.<br>
>> ><br>
>> > Anyone an idea?<br>
>> ><br>
>> ><br>
>> > Kind regards<br>
>> > Wouter<br>
>> > _______________________________________________<br>
>> > Qgis-user mailing list<br>
>> > <a href="mailto:Qgis-user@lists.osgeo.org" target="_blank">Qgis-user@lists.osgeo.org</a><br>
>> > List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
>> > Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
</blockquote></div>