Syntax for arcpy.management.CalculateField

217
2
Jump to solution
a month ago
CaitlinBernier1
New Contributor II

I am trying to do a simple thing, I think, in Python.  I am very new to actually trying to write any Python code myself so please have mercy.

I have a script that defines an in-fc and an out-fc.  The in-feature is exported to the out_fc.  Then there are a bunch of fields that are deleted from the out_fc.  Then a field in the out_fc needs to be calculated using 2 fields that exist in the out_fc: STRUCTUREBEG and STRUCTUREEND.  The line of code that I am unsuccessfully using is the following:

arcpy.management.CalculateField (out_fc, "PYTHON3", ["SEGMENT"], ["!STRUCTUREBEG!+\" to \"+ !STRUCTUREEND!"])

SEGMENT is the field I want to populate with STRUCTUREBEG and STRUCTUREEND in the format STRUCTUREBEG to STRUCTUREEND.  The resulting field should be full of strings like this:

0-1 to 1-1
1-1 to 1-2
1-2 to 1-3

I have gone around and around trying to get the syntax right and my errors just get worse and worse.  I think this is simple, but I am not finding a solution.  Perhaps someone out there can set me straight?

Thank you in advance for your help.
Caitlin

 

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

You are passing expression type, which is the fourth parameter, as the second parameter, which is field.  Also, when you pass the second parameter, you don't put it in square brackets, which creates a Python list and will cause another error.

Try:

arcpy.management.CalculateField(out_fc, "SEGMENT", r"!STRUCTUREBEG! + ' to ' + !STRUCTUREEND!", "PYTHON3")

View solution in original post

0 Kudos
2 Replies
JoshuaBixby
MVP Esteemed Contributor

You are passing expression type, which is the fourth parameter, as the second parameter, which is field.  Also, when you pass the second parameter, you don't put it in square brackets, which creates a Python list and will cause another error.

Try:

arcpy.management.CalculateField(out_fc, "SEGMENT", r"!STRUCTUREBEG! + ' to ' + !STRUCTUREEND!", "PYTHON3")
0 Kudos
CaitlinBernier1
New Contributor II

Joshua,

Thank you very much. That solved my issue and I have marked your note as a solution.  I really appreciate your taking the time to help me out, especially late on a Friday night!

Caitlin

0 Kudos