Select to view content in your preferred language

Calculate Field

697
3
Jump to solution
02-07-2014 02:29 PM
HollyZhang1
Occasional Contributor
I am using ArcPy 10.2 CalculateField management to format a field from an existing field.
e.g. from 123456789 to {12-3456-789}
It works good using attribute table, but it shows "syntax error" when using python scripting.
Anyone has any solution?
Thanks.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Alum
in the Python script like:
"{"+!field![:2]+"-"+!field![2:6]+"-"+!field![6:]+"}"
it works good in attribute table!
By the way, !field! is string.


This has to be quoted and fed in as an expression to the CalculateField_management tool.  I seriously hope you did not actually create a field in a table called field.  That is a keyword that will cause problems.  If it has another name in the table use that name.  Anyway, something like:

import arcpy
from arcpy import env

# Set environment settings
env.workspace = "C:/data/airport.gdb"
 
# Set local variables
inFeatures = "parcels"
fieldName = "IDValue"
expression = '"{"+!IDValue![:2]+"-"+!IDValue![2:6]+"-"+!IDValue![6:]+"}"'

# Execute CalculateField
arcpy.CalculateField_management(inFeatures, fieldName, expression, "PYTHON_9.3")

View solution in original post

0 Kudos
3 Replies
RichardFairhurst
MVP Alum
I am using ArcPy 10.2 CalculateField management to format a field from an existing field.
e.g. from 123456789 to {12-3456-789}
It works good using attribute table, but it shows "syntax error" when using python scripting.
Anyone has any solution?
Thanks.


What does the calculation look like in the Python script?
0 Kudos
HollyZhang1
Occasional Contributor
What does the calculation look like in the Python script?


in the Python script like:
"{"+!field![:2]+"-"+!field![2:6]+"-"+!field![6:]+"}"
it works good in attribute table!
By the way, !field! is string.
0 Kudos
RichardFairhurst
MVP Alum
in the Python script like:
"{"+!field![:2]+"-"+!field![2:6]+"-"+!field![6:]+"}"
it works good in attribute table!
By the way, !field! is string.


This has to be quoted and fed in as an expression to the CalculateField_management tool.  I seriously hope you did not actually create a field in a table called field.  That is a keyword that will cause problems.  If it has another name in the table use that name.  Anyway, something like:

import arcpy
from arcpy import env

# Set environment settings
env.workspace = "C:/data/airport.gdb"
 
# Set local variables
inFeatures = "parcels"
fieldName = "IDValue"
expression = '"{"+!IDValue![:2]+"-"+!IDValue![2:6]+"-"+!IDValue![6:]+"}"'

# Execute CalculateField
arcpy.CalculateField_management(inFeatures, fieldName, expression, "PYTHON_9.3")
0 Kudos