Select to view content in your preferred language

Calculate Field to get the value for field from combine data of other fileds

2048
5
11-19-2021 09:55 AM
BinFeng_2021
Emerging Contributor

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!

0 Kudos
5 Replies
JoeBorgione
MVP Emeritus

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)

 

That should just about do it....
0 Kudos
DanPatterson
MVP Esteemed Contributor

From your question

 “DESCRIPTION” 

from your code examples

 "DISCREPTION"

correct the spelling discrepancy and see if that fixes things


... sort of retired...
0 Kudos
BinFeng_2021
Emerging Contributor

Thanks, yes it is a typing mistake.

0 Kudos
JohannesLindner
MVP Frequent Contributor
#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")

 

 


Have a great day!
Johannes
BinFeng_2021
Emerging Contributor

Thank a lot, I will try it.

0 Kudos