I'm using PyQGIS 3.4, I am trying to add a new field and set the values equal to an existing field. The existing field are population totals but are in string format, I want to convert these to integers or real numbers.
I can add the new field however the problem is adding values to the field.
I am getting the following error.
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 13, in <module>
TypeError: QgsExpression.prepare(): argument 1 has unexpected type 'int'
Script:
from qgis.core import *
from PyQt5.QtCore import *
output_layer = iface.activeLayer()
provider = output_layer.dataProvider()
provider.addAttributes([QgsField('new_pop', QVariant.Int)])
output_layer.updateFields()
output_layer.startEditing()
fieldIndex = output_layer.fields().indexFromName( 'new_pop' )
e = QgsExpression( 'toInt("exist_pop")' )
e.prepare(output_layer.fields().indexFromName( 'new_pop' ))
output_layer.commitChanges()
e.prepare
is expecting aQGSExpressionContext
not anint
(see docs) but I don't know what one of those is.