2

I would like to translate a vbsript expression into a python expression. But it doesn't work! Here is the code in vbscript:

Function FindLabel ( [CONTENU], [NOM_OBJET_USUEL], [ALTITUDE_FIL_EAU_M] )
If ( [CONTENU] = "Eaux usées") then
    FindLabel = "<CLR red='168' >" & [NOM_OBJET_USUEL] & vbnewline & [ALTITUDE_FIL_EAU_M] & "</CLR>"
ElseIf ([CONTENU] = "Eaux pluviales") then
    FindLabel = "<CLR green='77' blue='168' >" &[NOM_OBJET_USUEL] & vbnewline & [ALTITUDE_FIL_EAU_M] & "</CLR>"
ElseIf ([CONTENU] = "Eaux mélangées") then
    FindLabel = "<CLR red='79' green='41' >" & [NOM_OBJET_USUEL] & vbnewline & [ALTITUDE_FIL_EAU_M] & "</CLR>"
ElseIf ([CONTENU] = "Eaux de drainage") then
    FindLabel = "<CLR green='168' blue='132' >" & [ALTITUDE_FIL_EAU_M] & "</CLR>"
End if
End Function

My translation:

def FindLabel  ([CONTENU], [NOM_OBJET_USUEL], [ALTITUDE_FIL_EAU_M] ):
 if [CONTENU] == "Eaux mélangées":
    return  "<CLR red='79'  >" + [NOM_OBJET_USUEL] +'\n' + [ALTITUDE_FIL_EAU_M] + "</CLR>"
  elif [CONTENU] == "Eaux usées": 
    return  "<CLR red='168' >" +[NOM_OBJET_USUEL] +'\n' + [ALTITUDE_FIL_EAU_M] +"</CLR>"
  elif  [CONTENU] == "Eaux de drainage":
    return  "<CLR green='168' blue='132' >" +[ALTITUDE_FIL_EAU_M] + "</CLR>"
  elif [CONTENU] == "Eaux pluviales":
    return  "<CLR green='77' blue='168' >" + [NOM_OBJET_USUEL] +'\n' + [ALTITUDE_FIL_EAU_M] + "</CLR>"

I need some help finding the right syntax, because i have this error message (image).

enter image description here

If I write code as below, only labels "Eaux pluviales" are displayed

def FindLabel  ([CONTENU], [NOM_OBJET_USUEL], [ALTITUDE_FIL_EAU_M] ):
  if [CONTENU] == "Eaux usées":
    return  "<CLR red='168' >" +[NOM_OBJET_USUEL] +'\n' + [ALTITUDE_FIL_EAU_M] +"</CLR>"
  elif [CONTENU] == "Eaux pluviales":
    return  "<CLR green='77' blue='168' >" +[NOM_OBJET_USUEL] +'\n' + [ALTITUDE_FIL_EAU_M] + "</CLR>"
  elif [CONTENU] == "Eaux mélangées":
    return  "<CLR red='79' green='41' >" + [NOM_OBJET_USUEL] +'\n' + [ALTITUDE_FIL_EAU_M] + "</CLR>"
  elif [CONTENU] == "Eaux de drainage":
    return  "<CLR green='168' blue='132' >" +[ALTITUDE_FIL_EAU_M] + "</CLR>"
  • 3
    You are seeing two issues then? The first translation has the ifneeding to be indented the same as the elif. As for only 'Eaux pluviales' working, my wild guess is string encoding. Python 2 is notorious for messing up French words. – Richard Morgan Oct 7 '16 at 15:47
4

When the text field values are passed to python they are in unicode, and include ths special characters. But you are comparing to values that are encoded as ascii. This works OK for "Eaux de drainage" because it does not include any special characters.

Try adding a u before the quotation mark to mark the string as unicode.

u"Eaux usées"
1

You have two issues here.

First issue:

IndentationError: unindent does not match any outer indentation level (, line 4)

The error from your first translation is telling you that there is an issue with the line indentation. Python requires strict indentation - if you indent one line by four spaces, everything else in the same block of code also needs to be indented four spaces. When you return to a previous block of code you need the indent to be the same as the last time you were in that block of code.

def FindLabel  ([CONTENU], [NOM_OBJET_USUEL], [ALTITUDE_FIL_EAU_M] ):
 if [CONTENU] == "Eaux mélangées":

  elif [CONTENU] == "Eaux usées": 

You can see here that your if line is only indented by one space, but your elif is indented by two spaces. These need to be the same. By making the if line indent by two spaces you will overcome this first issue.

Second issue:

Your French words are being passed as unicode values but you are trying to compare them to strings. In python it will not see these as the same due to the accents over some of the letters, so you'll need to make it compare unicode to unicode.

Try adding a u in front of your strings in the if and elif etc. lines

if [CONTENU] == u"Eaux mélangées":
    return  "<CLR red='79'  >" + [NOM_OBJET_USUEL] +'\n' + [ALTITUDE_FIL_EAU_M] + "</CLR>"
elif [CONTENU] == u"Eaux usées": 
    return  "<CLR red='168' >" +[NOM_OBJET_USUEL] +'\n' + [ALTITUDE_FIL_EAU_M] +"</CLR>"
  • Glad to help @Scotbi. If this has solve your issue please consider marking it as the answer. See What should I do when someone answers my question? – Midavalo Oct 12 '16 at 7:01
  • Thank you, you are solved my issues! However I have another issue: Some attribute values are empty. It follows that label isn't displayed. I can't concatenate string and noneType object. What is the solution? – Scotbi Oct 12 '16 at 8:41
  • @Scotbi That sounds like a new question. Please mark this one as answered, and then ask your new question separately. – Midavalo Oct 12 '16 at 8:44

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.