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

1 Answer 1

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])
0

Your Answer

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

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