3

I'm trying to convert a string column Rainfall of point layer weather_l to float:

context = QgsExpressionContext()
context.appendScopes(QgsExpressionContextUtils.globalProjectLayerScopes(weather_l))

num_float = QgsExpression('float(fea["Rainfall"])')

with edit(weather_l):
    for fea in weather_l.getFeatures():
        context.setFeature(fea)
        if not fea['Rainfall'].isalpha():
            fea['Rain_D'] = num_float.evaluate(context)
        weather_l.updateFeature(fea)

but num_float.evaluate(context) returns None. How can I correct

num_float = QgsExpression('float(fea["Rainfall"])')

to convert the string to a float?

0

1 Answer 1

5

Your expression is mixing Python and Qgis function syntax.

Try the following expression syntax:

QgsExpression('to_real("Rainfall")')

You need to pass only the field name in double quotes to the to_real function.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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