2

I'm using a QGIS 2.8.1 in windows. Have some doubts with syntax when use string in python console. I want to use the field calculator funtion like:

processing.runalg('qgis:fieldcalculator', input, field_name, type_field, width, precision, Create new field, formula, output)

In the formula parameter I want use the condition:

case when "Municipio" is NULL then 'PENICHE'

I know that PENICHE is a string, so have to be between '' (plicas) but all the expression should be too between '' (plicas) like:

' case when "Municipio" is NULL then 'PENICHE' ' 

The problem is that there happened a syntax error.

How can I correct the expression?


The code I´m using is:

processing.runalg('qgis:fieldcalculator', input, "Mun", 2, 27, 0, True, ' case when "Municipio" is NULL then 'PENICHE' else "Municipio" end ', output)


I used formula = "case when \"Municipio\" is NULL then 'PENICHE' end" know its working!

3

While Joseph is correct about the syntax of the CASE statement, your Python issue stems from string quoting.

You can use either ' or " to quote strings, and you don't have to escape the other character if used, but you do have to escape the same character. Or you can triple quote (ie ''') which will also allow multi line quotes.

In other words:

# this should work 
formula = "case when \"Municipio\" is NULL then 'PENICHE' end" 
# so should this
formula = 'case when "Municipio" is NULL then \'PENICHE\' end'

# or you can use multi line quotes
formula = '''
case when "Municipio" is NULL then 'PENICHE' end
'''
  • +1, that's a much easier method than typing the formula inside the function! – Joseph Sep 3 '15 at 11:11
0

You need to add end to close of your case statement. The following should work:

case when "Municipio" is NULL then 'PENICHE' end

Tip:

What I normally do is to make an expression in the field calculator from the GUI before adding it to the python console. This way, it tells you immediately if your formula will work or not instead of running it though the python console and receiving an error which you then have to try and figure out.

Hope this helps!

  • I use your suggestion and didn't work, I try these two ways: ' case when "Municipio" is NULL then 'PENICHE' end ' and case when "Municipio" is NULL then 'PENICHE' end any help? – Patrícia Fernandes Sep 3 '15 at 10:36
  • appeared in the two ways a syntax error! – Patrícia Fernandes Sep 3 '15 at 10:38
  • Are you receiving an error or does it run and nothing happens? Is the Municipio field a string field? Could you edit your question to include exactly the code you are using please? – Joseph Sep 3 '15 at 10:40
  • I'm receiving a syntax error that don't let run the code , the Municipio field is a string field. The code I'm using is: processing.runalg('qgis:fieldcalculator', input, "Mun", 2, 27, 0, True, ' case when "Municipio" is NULL then 'PENICHE' else "Municipio" end ', output) – Patrícia Fernandes Sep 3 '15 at 10:59

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.