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')