1

I have a "Model_Version" field in my feature class that is being used for record keeping. I do not intend to ever do any numeric operations with it, so I am storing it as a string field. Right now my model version is 1.0, so it is being stored as "1.0".

When I used the Field Calculator manually in ArcMap, I can get my desired value by entering "1.0" or '1.0'.

But when I use ArcPy as below, the resulting string in the feature class is "1", without the decimal and trailing zero:

arcpy.CalculateField_management(myLayer, "Model_Version", 
 "1.0", "PYTHON3")

I have tried the following and get the same result of "1" every time:

arcpy.CalculateField_management(myLayer, "Model_Version", 
 '1.0', "PYTHON3")
arcpy.CalculateField_management(myLayer, "Model_Version", 
 str("1.0"), "PYTHON3")
arcpy.CalculateField_management(myLayer, "Model_Version", 
 1.0, "PYTHON3")

Why is it not writing the string as I have coded it?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jan 8, 2020 at 4:52

1 Answer 1

2

The expression that you are passing to arcpy.CalculateField_management() is not directly a value as such (either a string or a number), but rather a snippet of Python code to evaluate in order to get a value. The outer quotation marks do not delineate a string value within that Python code, but are rather the start and end of the actual Python code.

So you need to include another set of quote marks within the Python code itself to delineate a string.

One way of doing this would be to include single quote (for the string) within the double quotes (for the python code):

arcpy.CalculateField_management(myLayer, "Model_Version", "'1.0'", "PYTHON3")

Alternatively, you could use double-quotes within single-quotes, or you could use escaped double-quotes within double-quotes, etc.

Without doing this, the value in the Python code 1.0 is equal to 1, not to the string "1.0".

answered Jan 8, 2020 at 4:54
1
  • That explains it. I had previously used the CalculateField_management function to pass integer values to a field, which could be done without enclosing them in quotes. So I did not grasp the importance of enclosing the python snippet. Thanks for clearing that up! Commented Jan 8, 2020 at 5:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.