arcpy.CalculateField_management not passing (variable + !FieldValue!) calculation

3247
7
Jump to solution
10-28-2015 11:50 AM
RebeccaStrauch__GISP
MVP Esteemed Contributor

I'm trying to use arcpy.CalculateField in a script where I'm setting a "short" field to basically  "(int(TransSeriesNo) * 100) + !OID!"

I have tried many incarnations of this based on the help docs and various geonet and other threads with no luck.  I have success if I hardcode the the first part of the equation (ie.  "int('!OID!') + 400") but of course that is not what I want to do.  Below are some of the various things I have tried....the error message, if there is one, is in the comment above the set. 

by the way, I know I can .format the better  ( Curtis Price​ ) and will, but just trying to get it working first.  Thanks for any help

#works
TransSeriesNo = "4"
setNo = (int(TransSeriesNo) * 100)
expression = "int('!OID!') + 400"
arcpy.CalculateField_management("Pts1c", "testID", expression , "PYTHON_9.3")

# doesn't work   NameError: name 'setNo'is not defined
TransSeriesNo = "3"
setNo = (int(TransSeriesNo) * 100)
expression = "int('!OID!') + setNo"
arcpy.CalculateField_management("Pts1c", "testID", expression , "PYTHON_9.3")

# doesn't work   NameError: name 'setNo'is not define
TransSeriesNo = "2"
setNo = (int(TransSeriesNo) * 100)
codeblock = "setNo"
expression = "int('!OID!') + setNo"
arcpy.CalculateField_management("Pts1c", "testID", expression , "PYTHON_9.3", codeblock)

#  doesn't work   NameError: name 'setNo'is not define
TransSeriesNo = "1"
setNo = (int(TransSeriesNo) * 100)
expression = "getPtID(setNo, !OID!)"
codeblock = """def getPtID(setNo, theOID):
    setPtID = int(theOID + setNo)
    return setPtID"""
arcpy.CalculateField_management("Pts1c", "testID", expression , "PYTHON_9.3", codeblock)
0 Kudos
1 Solution

Accepted Solutions
IanMurray
Frequent Contributor

Hi Rebecca,

I think in this case you will need to use some type of string formatting since you need to insert the value(an integer) into a string expression.

See below

Formatting.png

I have this thread bookmarked as reference for string formatting examples, it gets confusing if you don't use it often.

Python - Select by attributes query issue

View solution in original post

7 Replies
Zeke
by
Regular Contributor III

I'm going to guess that setNo in expression needs to be formatted somehow. It may just be seen as a string as is.

RebeccaStrauch__GISP
MVP Esteemed Contributor

Good guess, but I could never figure out the right way.  The issues seems to be that I'm mixing a variable or variable calculation with a field value.  Although this wasn't what I was going for, it dawned on me, if that was the problem I could just set the field to the variable, then calc it again adding the two fields.  That worked.

TransSeriesNo = "9"
setNo = (int(TransSeriesNo) * 100)
arcpy.CalculateField_management("Pts1c", "testID", setNo , "PYTHON_9.3", codeblock)
arcpy.CalculateField_management("Pts1c", "testID", "!testID! + !OID!", "PYTHON_9.3", codeblock)

With an added comment or two in my code, it may not be the most efficient way, but at least I finally got it to calc the values I expected. 

0 Kudos
IanMurray
Frequent Contributor

Hi Rebecca,

I think in this case you will need to use some type of string formatting since you need to insert the value(an integer) into a string expression.

See below

Formatting.png

I have this thread bookmarked as reference for string formatting examples, it gets confusing if you don't use it often.

Python - Select by attributes query issue

RebeccaStrauch__GISP
MVP Esteemed Contributor

Ian, thanks.  I had something similar on several occasions, but had the " in the wrong place in the expresison.  Below is the basically the code I'll use:

TransSeriesNo = "3"
setNo = (int(TransSeriesNo) * 100)
print setNo
expression = "int('!OID!') + {}".format(setNo)
arcpy.CalculateField_management("Pts1c", "testID", expression , "PYTHON_9.3")

Thanks for the quick help.  Been driving my batty for a day.  ugh!

0 Kudos
IanMurray
Frequent Contributor

Quite welcome, sometimes making valid expressions like that with python can be a pain, hence why I found myself a reference to go back to.  I don't use python quite enough to be fully comfortable with string formatting without a guide.

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

yes, that link has many good samples.  I usually don't have that many issues with the formatting, but what typically worked for me in the past didn't with CalculateField.  I'm glad I didn't need to deal with the code block, etc.  Anyway, now I have two ways to fix it....yours, which I will use....and my double calc if all else fails. 

0 Kudos
curtvprice
MVP Esteemed Contributor

I'm trying to use arcpy.CalculateField in a script where I'm

setting a "short" field to basically  "(int(TransSeriesNo) * 100) + !OID!"

#  no .format
expr = "(int(" + str(TransSeriesNo) + ") * 100) + !OID!"
# format
expr = "(int({}) * 100) + !OID!".format(TransSeriesNo)
# or, if TransSeriesNo is an integer (1) or string with no decimals ("1"):
expr = "(" + str(TransSeriesNo)  + " * 100) + !OID!)"
# .format:
expr = "({} * 100) + !OID!".format(transSeriesNo)

Hints:

1. The calculate value expression must be entirely a string

2. Field definitions (eg !OID!) are replaced at runtime with the field value for each row at runtime 'in data type' (!OID! will be integer and doesn't need an int() around it, and a string field would not need quotes around it for its to be recognized as a string). For example:

expr = "!CITY! + ' ' + !STATE! + ' ' + !ZIP!"