Syntax when passing variable to calculate field expression

2751
5
Jump to solution
09-14-2016 11:36 AM
ZacharyHart
Occasional Contributor III

OK, so I feel like I've tried just about everything here. What I'm looking to do is to concatenate a iteration value (?) from a search cursor with the object ID of a feature class to create a unique ID field. I want these two values separated by a hyphen '-'. Target field (PLOT_ID) is obviously a string. There is what I've got working so far.

i = 1

ptCur = arcpy.da.SearchCursor (ptFC, fldLst)

for row in ptCur:

[various things happen removed for brevity]

Cluster_Plot = str(i) + '!OBJECTID!'

arcpy.AddMessage(Cluster_Plot)

arcpy.CalculateField_management(outPointsDissolve,"PLOT_ID",Cluster_Plot,"PYTHON_9.3")

things are appended to a a target FC and the target field looks like this:

So it appears to be treating the expression inputs as strings. From here I've tried to get a - in between the two values with no luck....even when converting the OID to a string : 'str(!OBJECTID!)'   I just can't get things to escape right i guess. Things I've tried:

Cluster_Plot = str(i) + '-' + '!OBJECTID!'

Here the  hyphen apparently is interpreted as an operator:

Cluster_Plot = str(i) + " + '-' + " + '!OBJECTID!'

Cluster_Plot = str(i) + " + '-' + " + 'str(!OBJECTID!)'

I dont get this because you can use the + operator in the field calculator just fine (if you format the field as a string using str)

Could someone please help me out of my misery here?

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
MicahBabinski
Occasional Contributor III

Hi Zachary,

Would

Cluster_Plot = '"{}-{}"'.format(i,'!OBJECTID!')

work? I've become a big fan of using triple quoted strings and the .format() method when creating a variable to use as a field calculator expression.

Another option is to use an update cursor. Something like:

table = "myTable"
fields = ("OBJECTID", "PLOT_ID")
i = 1
with arcpy.da.UpdateCursor(table, fields) as cursor:
    for row in cursor:
        row[1] = str(i) + "-" + str(row[0])
        cursor.updateRow(row)
        i+=1‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Good luck!

View solution in original post

5 Replies
MicahBabinski
Occasional Contributor III

Hi Zachary,

Would

Cluster_Plot = '"{}-{}"'.format(i,'!OBJECTID!')

work? I've become a big fan of using triple quoted strings and the .format() method when creating a variable to use as a field calculator expression.

Another option is to use an update cursor. Something like:

table = "myTable"
fields = ("OBJECTID", "PLOT_ID")
i = 1
with arcpy.da.UpdateCursor(table, fields) as cursor:
    for row in cursor:
        row[1] = str(i) + "-" + str(row[0])
        cursor.updateRow(row)
        i+=1‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Good luck!

ZacharyHart
Occasional Contributor III

Hi Micah...I'm just getting a generic 'invalid syntax' error. for the first option.

The second wont work because the OIDs aren't coming from the FC used to run the loop/search cursor.

0 Kudos
MicahBabinski
Occasional Contributor III

I see. Can you post your full script? Also, maybe try the field calculator expression without the str() around the i variable and !OBJECTID!.

0 Kudos
ZacharyHart
Occasional Contributor III

I got it to work!!! Nice way to close out the day...for some reason the OID field needed single quotes too. If you want to modify your code I'll mark your answer as accepted.

Cluster_Plot = '"{}-{}"'.format(i,'!OBJECTID!')

MicahBabinski
Occasional Contributor III

Terrific! I've modified it. If at first you don't succeed...

0 Kudos