1

I need to codify a field with other fields information I have this code running into a script:

            # Process: Calculate Field (12)
  arcpy.CalculateField_management(RedHID_Layer, "TIPOcorr", "cadena", "VB", "cadena = \"\"\\nif (Not "
                                                                                  "isNull ([ACC])) "
                                                                                  "Then\\ncadena = [ACC]\\nend "
                                                                                  "if\\n\\nif (Not isNull (["
                                                                                  "EDIF])) Then\\n   if cadena "
                                                                                  "= \"\" then\\n     cadena = "
                                                                                  " [EDIF]\\n   else\\n     "
                                                                                  "cadena = cadena & \", "
                                                                                  "\" & [EDIF]\\n   end "
                                                                                  "if\\nend if\\n\\nif (Not "
                                                                                  "isNull ([MAGS])) Then\\n   "
                                                                                  "if cadena = \"\" then\\n   "
                                                                                  "cadena =  [MAGS]\\n  "
                                                                                  "else\\n   cadena = cadena & "
                                                                                  "\", \" & [MAGS]\\nend "
                                                                                  "if\\nend if\\n\\nif (Not "
                                                                                  "isNull ([OBS1])) Then\\n   "
                                                                                  "if cadena = \"\" then\\n   "
                                                                                  "cadena =  [OBS1]\\n  "
                                                                                  "else\\n   cadena = cadena & "
                                                                                  "\", \" & [OBS1]\\nend "
                                                                                  "if\\nend if\\n\\nif (Not "
                                                                                  "isNull ([OBS2])) Then\\n   "
                                                                                  "if cadena = \"\" then\\n   "
                                                                                  "cadena =  [OBS2]\\n  "
                                                                                  "else\\n   cadena = cadena & "
                                                                                  "\", \" & [OBS2]\\nend "
                                                                                  "if\\nend if\\n\\nif (Not "
                                                                                  "isNull ([REM1])) Then\\n   "
                                                                                  "if cadena = \"\" then\\n   "
                                                                                  "cadena =  [REM1]\\n  "
                                                                                  "else\\n   cadena = cadena & "
                                                                                  "\", \" & [REM1]\\nend "
                                                                                  "if\\nend if\\n\\nif (Not "
                                                                                  "isNull ([REM2])) Then\\n   "
                                                                                  "if cadena = \"\" then\\n   "
                                                                                  "cadena =  [REM2]\\n  "
                                                                                  "else\\n   cadena = cadena & "
                                                                                  "\", \" & [REM2]\\nend "
                                                                                  "if\\nend if\\n\\nif (Not "
                                                                                  "isNull ([REM3])) Then\\n   "
                                                                                  "if cadena = \"\" then\\n   "
                                                                                  "cadena =  [REM3]\\n  "
                                                                                  "else\\n   cadena = cadena & "
                                                                                  "\", \" & [REM3]\\nend "
                                                                                  "if\\nend if\\n\\nif (Not "
                                                                                  "isNull ([QMD1])) Then\\n   "
                                                                                  "if cadena = \"\" then\\n   "
                                                                                  "cadena =  [QMD1]\\n  "
                                                                                  "else\\n   cadena = cadena & "
                                                                                  "\", \" & [QMD1]\\nend "
                                                                                  "if\\nend if\\n\\nif (Not "
                                                                                  "isNull ([QMD2])) Then\\n   "
                                                                                  "if cadena = \"\" then\\n   "
                                                                                  "cadena =  [QMD2]\\n  "
                                                                                  "else\\n   cadena = cadena & "
                                                                                  "\", \" & [QMD2]\\nend "
                                                                                  "if\\nend if\\n\\nif (Not "
                                                                                  "isNull ([RJT])) Then\\n   "
                                                                                  "if cadena = \"\" then\\n   "
                                                                                  "cadena =  [RJT]\\n  else\\n "
                                                                                  "  cadena = cadena & \", "
                                                                                  "\" & [RJT]\\nend if\\nend "
                                                                                  "if\\n")

This is in vb and I need it in Python, so I wrote this:

            arcpy.CalculateField_management(in_table=RedHID_Layer, field="TIPOcorr", expression='cadena',
                                        expression_type="PYTHON",
                                        code_block='cadena = ""\nif "!ACC!" is not None:\n    cadena = "!ACC!"\n\nif "!EDIF!":\n    if cadena == "":\n        cadena = "!EDIF!"\n    else:\n        cadena = cadena + ", " + "!EDIF!"\n\nif "!MAGS!":\n    if cadena == "":\n        cadena = "!MAGS!"\n    else:\n        cadena = cadena + ", " + "!MAGS!"\n\nif "!OBS1!":\n    if cadena == "":\n        cadena = "!OBS1!"\n    else:\n        cadena = cadena + ", " + "!OBS1!"\n\nif "!OBS2!":\n    if cadena == "":\n        cadena = "!OBS2!"\n    else:\n        cadena = cadena + ", " + "!OBS2!"\n\nif "!REM1!":\n    if cadena == "":\n        cadena = "!REM1!"\n    else:\n        cadena = cadena + ", " + "!REM1!"\n\nif "!REM2!":\n    if cadena == "":\n        cadena = "!REM2!"\n    else:\n        cadena = cadena + ", " + "!REM2!"\n\nif "!REM3!":\n    if cadena == "":\n        cadena = "!REM3!"\n    else:\n        cadena = cadena + ", " + "!REM3!"\n\nif "!QMD1!":\n    if cadena == "":\n        cadena = "!QMD1!"\n    else:\n        cadena = cadena + ", " + "!QMD1!"\n\nif "!QMD2!":\n    if cadena == "":\n        cadena = "!QMD2!"\n    else:\n        cadena = cadena + ", " + "!QMD2!"\n\nif "!RJT!":\n    if cadena == "":\n        cadena = "!RJT!"\n    else:\n        cadena = cadena + ", " + "!RJT!"\n')

But still don't works. The result may be: enter image description here

2

It looks like you've converted this to a python 'script' from model builder...while that produces functional Python, it's pretty ugly.

It looks like your original VBA codeblock is something like:

cadena = ""

if (Not isNull ([ACC])) Then 
    cadena = [ACC]
end if

if (Not isNull ([EDIF])) Then
    if cadena = "" then
        cadena = [EDIF]
    else
        cadena = cadena & ", " & [EDIF]
    end if
end if

if (Not isNull ([MAGS])) Then
    if cadena = "" then
        cadena =  [MAGS]
    else
        cadena = cadena & ", " & [MAGS]
    end if
end if
...

And, it looks like you've tried to do a similar pattern in Python like this:

cadena = ""
if "!ACC!" is not None:
    cadena = "!ACC!"

if "!EDIF!":
    if cadena == "":
            cadena = "!EDIF!"
    else:
            cadena = cadena + ", " + "!EDIF!"

if "!MAGS!":
    if cadena == "":
            cadena = "!MAGS!"
    else:
            cadena = cadena + ", " + "!MAGS!"
...

A quick fix should be to remove the quotes (") from around the fields: ie. !ACC! instead of "!ACC!".

I would recommend something more Pythonic, though:

cadena = ', '.join([x for x in [!ACC!,!EDIF!,!MAGS!,!OBS1!,!OBS2!,!REM1!,!REM2!,
                                 !REM3!,!QMD1!,!QMD2!,!RJT!] if x is not None])
  • 1
    Thank you very match for your answer and your time! I was not sure about the Pythonic option compatibility but works fine. – Alex_knd Jun 6 at 12:40
  • Sure. By 'Pythonic' I just mean something along the lines of 'more efficient, easier to read,' etc. Python has some patterns and syntax available that other languages do not have, like list comprehensions (which is what I've used here). Using those patterns can make code more manageable, and doing so is sometimes referred to as 'Pythonic coding.' It's a shorter way of saying 'a more efficient way of doing this in Python.' – Evil Genius Jun 6 at 19:38

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.