I need to filter multiple column values using python in QGIS. I am facing issue while writing expression using QgisExpression(qgis.core).

If one of the column values is NULL then also I need to get the row. Is there a better solution to handle this or any other package to handle the expressions in QGIS?

You can see below an example to illustrate

QgsExpression(f"input='{abc}' OR value='{def}'") # here abc = 1 and def = NULL 
  • 1
    Can you give us an expression/code sample/excerpt you run and that do not work as you expect? – ThomasG77 Mar 10 at 16:09
  • QgsExpression(f"input='{abc}' OR value='{def}'") here abc = 1 and def = NULL – Vivek N S Mar 12 at 4:04
  • Integrated your comment in the question. FYI, when giving precision, it's better to edit your answer as it's clearer for other reading your question than looking at a comment. – ThomasG77 Mar 12 at 10:18

You need to manage NULL value by changing you expression to be able to manage when values are NULL. The syntax you use is not adapted as you don't compare NULL value with equal but with the syntax yourfield IS NULL OR yourfield IS NOT NULL. I've also used def1 instead of def as def is a reserved word in Python

def1 = 'NULL'
valExp = f"value IS NULL" if def1 == 'NULL' else f"value='{def1}'"
exp = QgsExpression(f"input='{abc}' OR {valExp}")
  • The above expression is working fine. Is there a way to use AND here instead of OR. When i changed the OR to AND it failed to return the expected result and instead it resulted in NULL – Vivek N S Mar 15 at 6:07
  • What i need here is to get the row value based on both the conditions – Vivek N S Mar 15 at 6:18

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.