Calculate Field using Python

3161
4
Jump to solution
06-16-2015 11:38 AM
CoyPotts1
Occasional Contributor III

I am trying to calculate a field based on derived values within a python script.  I keep getting the following error, though:

"Empty value for ObjectID = 3005

The calculated value is invalid for the row with ObjectID = 3005. For example, the calculated value may be too large for the field or you may be trying to add a string to a number field. This row will not be updated."

The script runs through every single feature and returns an error message identical to the one above for each feature.

# Calculate Region

# Set local variables
inTable = fiberFeatures
inField = "ProjectRegion"
expression = region

# Execute CalculateField
arcpy.CalculateField_management(inTable, inField, expression, "VB")

All variables check out just fine when I print them in the ArcMap Python window, and the field within my feature class is of text type.  The region is a simple two letter code such as "NC", "NE", "SW", etc...

I've tried with various combinations of alternatives, such as adding single/double quotes around region, I've added single/double/triple quotes around expression within the CalculateField function, and I've tried entering the variable straight into the function. 

The only help I can find through internet searches that pertains exactly to what I'm doing is HERE, but it is a little different and I'm not sure how to make it apply to my situation. 

I'm working on a Windows 7 machine and I'm running ArcGIS Standard version 10.3.0.4322

0 Kudos
1 Solution

Accepted Solutions
CoyPotts1
Occasional Contributor III

I actually just fixed it while tinkering.

I changed the expression type to "PYTHON" and added single quotes around the region variable.

Correct code:

# Calculate Region

# Set local variables
inTable = fiberFeatures
inField = "ProjectRegion"
expression = 'region'

# Execute CalculateField
arcpy.CalculateField_management(inTable, inField, expression, "PYTHON")

I've been stuck on this error for over an hour.  I would finally decide to ask on here and then figure it out right away

Thanks for responding, though! 

View solution in original post

4 Replies
WesMiller
Regular Contributor III

Your doing a vb calculation. I think you need to change

expression = region

to

expression = "region"

0 Kudos
CoyPotts1
Occasional Contributor III

Thanks for the response, but that results in the same error message. 

0 Kudos
WesMiller
Regular Contributor III

What are the properties of your field

Is it a string?

Does it have enough length > 6?

0 Kudos
CoyPotts1
Occasional Contributor III

I actually just fixed it while tinkering.

I changed the expression type to "PYTHON" and added single quotes around the region variable.

Correct code:

# Calculate Region

# Set local variables
inTable = fiberFeatures
inField = "ProjectRegion"
expression = 'region'

# Execute CalculateField
arcpy.CalculateField_management(inTable, inField, expression, "PYTHON")

I've been stuck on this error for over an hour.  I would finally decide to ask on here and then figure it out right away

Thanks for responding, though!