Hello, I try to use Calculate Field to get the value for field “DESCRIPTION” from combine data of other fileds, as the image 1 attached.
However, my below first Syntax is not working
arcpy.CalculateField_management(UT_CROSSING, "DISCREPTION", "[COMPANY]+\" \"+ [LOCATION]+\" \"+ [TYPE]+\" \"+\" SIZE=\"+ [SIZE]", "VB", "")
If I use below syntax, there no space between each field data, show as in image 2 arcpy.CalculateField_management("UT_CROSSING","DISCREPTION","!COMPANY!" + " " + "!LOCATION!" + " " + "!TYPE!" + " " + "!SIZE!" ,"PYTHON")
It would be wonderful if you can tell me my mistake.
Thanks in advance!
See if this works for you. Not tested; no guarantees...
import arcpy
expression = f'{!COMPANY!} {!LOCATION!} {!TYPE!} {!SIZE!}'
arcpy.CalculateField_management("UT_CROSSING","DISCREPTION", expression, PYTHON3)
From your question
“DESCRIPTION”
from your code examples
"DISCREPTION"
correct the spelling discrepancy and see if that fixes things
Thanks, yes it is a typing mistake.
#arcpy.CalculateField_management("UT_CROSSING","DISCREPTION","!COMPANY!" + " " + "!LOCATION!" + " " + "!TYPE!" + " " + "!SIZE!" ,"PYTHON")
# this gives the following string, which isn't a valid python expression, into CalculateField:
#expr = "!COMPANY! !LOCATION! !TYPE! !SIZE!"
# What you want to do is something like this:
# Notice that the expression is wrapped in single quotes
expr = '!COMPANY! + " " + !LOCATION! + " " + !TYPE! + " " + !SIZE!'
# even better
expr = '" ".join([!COMPANY!, !LOCATION!, !TYPE!, !SIZE!])'
arcpy.CalculateField_management("UT_CROSSING","DISCREPTION",expr ,"PYTHON")
Or use Arcade:
expr = 'Concatenate([$feature.COMPANY, $feature.LOCATION, $feature.TYPE, $feature.SIZE], " ")'
arcpy.CalculateField_management("UT_CROSSING","DISCREPTION",expr ,"ARCADE")
Thank a lot, I will try it.