I'm pretty new to Python and this is my first post on this forum so I could use some help. I am currently attempting to run some code in the Field Calculator in a model in order to replace string characters (hyphen, period, space) with an underscore ('_'). Here's what I have now.

def customReplace(Layer):
repList = ['-', ' ', '.'];
rep = '_'
for repList in Layer:
    return Layer.replace(repList, rep)
    return Layer

Im getting a syntax error: ERROR 000539: SyntaxError: unexpected EOF while parsing (, line 1) Failed to execute (Calculate Field (2)).

Anybody out there give me a hand?

By the way, I defined the above function in the pre-logic script code area and I'm calling it via

customReplace( !Layer! )

4 Answers 4


I am not completely sure to understand the aim of your script, but here are some comments

def customReplace(fieldV): # you must indent after your def
    repList = ['-', ' ', '.'] #remove the ;
    rep = '_'
    for old in repList: #I guess you want to replace the values in replist
        b = b.replace(old, rep) #here you need return, not print
    return b #no need to use else, but you must unindent

note that you need to put the function in a code block and call it with the name of the field


  • SO I thought your help worked because it ran without error, however I checked the field values after running and got some strange results. I really just need to run the replace() function but I need mutliple "old values" (hyphen, period, and space) and only one new value to replace with (an underscore). Maybe I need to rethink and not use lists....?
    – user37090
    Sep 24, 2014 at 20:27
  • the for loop is supposed to replace each value of your list with "_", it is done one by one as you cannot use the list n one go. Can you tell more precisely what's wrong ?
    – radouxju
    Sep 24, 2014 at 21:19

I am assuming you mean field value that needs changed and not your layer name since you mentioned Field Calculator

Same logic could be applied in a script for actual layer names too though

Ok.. I understand you and have it down.

(1) Make sure your parser is marked for python.

(2) In the Pre-Logic Script Code:

def customReplace(fieldValue):
  repList = ["DRG", "2014"] #my examples, change to yours
  repValue = "_"
  for item in repList:
    if item in fieldValue:
      fieldValue = fieldValue.replace(item, repValue)
  return fieldValue

(3) In the 2nd box, make sure you call this function and pass in the field you're editing.

See my photo for more info.Screenshot

  • Fantastic. That worked very well. I was hung up on how to access the values in repList where you used for item... if item in fieldValue:....etc. Very helpful. Thanks.
    – user37090
    Sep 24, 2014 at 20:37

I'm not sure what you're trying to do exactly. Is Layer supposed to be a feature class or shapefile, or a field?

One, all the lines in the function should be indented. Two, you can't have a semicolon end a line. Three, Layer is probably a reserved word, don't use it. Four, repList isn't in Layer; that loop will either error out or give you unexpected results. Five, while not exactly an error, is there a point in returning Layer in the else clause?

I don't think what you want is that hard, but what exactly are you trying to do?

  • Layer is the name of the field I am calculating with the python expression. Not sure if there is a need to return Layer if there is no replacement strings to be made?
    – user37090
    Sep 24, 2014 at 20:10
  • I'm creating a model that imports all geometries of a cad drawing (point, polyline, annotation, multipatch, polygon) and then creating featureclasses from a common field value in each cad geometry, the field being 'Layer'. But first I have to get the field values without spaces, hyphens, or periods in the names. Does that make any sense?
    – user37090
    Sep 24, 2014 at 20:14

I wanted to provide another alternative to my previous answer

I am more of a fan of using the python interactive window and an UpdateCursor for most of my attribute table changes. When I wrote my above answer, I wrote it in the interactive window and translated it to the field calculator. My interactive code was:

replaceList = ["DRG", "2014"]
replaceValue = "_"

rows = arcpy.UpdateCursor(#LAYERNAME)
for row in rows:
  currentValue = row.getValue("UniqueID") #your field name as a parameter
  for item in replaceList:
    if item in currentValue:
      currentValue = currentValue.replace(item, replaceValue)
    row.setValue("UniqueID", currentValue)

Notes... Typically I am more of a fan of using syntax like currentValue = row.FIELDNAME instead of getValue, and the same goes with setting it like row.FIELDNAME = currentValue. Using setValue and getValue are good for understanding the process, but I prefer the shorter syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.