UPDATE
In the code it needs to be f[realV]
, so:
with edit(lyr):
for f in lyr.selectedFeatures():
f[realV] = QgsExpression(str(doString))
I would like to update multiple features and multiple attributes with values, which are in the same layer. However, with the following code, nothing happens, I not even get an error message and so I don't know, why the features do not update.
This is my code:
# string values, in the form of decimals like '13.777'
fieldnames = [
'MD_str',
'MN_str',
'ME_str',
'PL1D_str',
'PL1N_str',
'PL1E_str',
'PL2D_str',
'PL2N_str',
'PL2E_str'
]
# empty fields of type 'real'
real_fields = [
'MD_r',
'MN_r',
'ME_r',
'PL1D_r',
'PL1N_r',
'PL1E_r',
'PL2D_r',
'PL2N_r',
'PL2E_r'
]
# Create tuple
dict_real = list(zip(real_fields, fieldnames))
for pair in dict_real:
realV = pair[0]
strV = pair[1]
doString = f"to_real(\"{strV}\")"
with edit(lyr):
for f in lyr.selectedFeatures():
realV = QgsExpression(str(doString)) # correction:f[realV]
lyr.updateFeature(f)
Does someone know, what I'm not doing correctly?