7

Is it possible to remove more than 1 character from a string in a field using the field calculator?

I know

!file!.replace(" ","")

removes spaces in a string, but is it possible to remove spaces and other characters such as "!", "?", "," all at once?

I have also tried:

Pre Logic Script:

def replChars(Name):
    replaceChars = ["!","?",","]
    for char in replaceChars:
        Name.replace(char,"")

Field =

replChars(!NH_NAME!)
11
  • 1
    This one is a pure Python question that can be researched at Stack Overflow. Try putting your function into a test Python script to see that.
    – PolyGeo
    May 16, 2016 at 21:34
  • 2
    How is it a pure python question? It's not talking about a script, it's simply asking about the python parser within the field calculator. I see many questions like this. May 16, 2016 at 21:35
  • 1
    Yes, you can do this in ArcMap field calculator with the Python parser. I would probably try it with regex.
    – PMK
    May 16, 2016 at 21:53
  • 3
    There could be any number of fancy ways to do this, I do a replace for each: !file!.replace(" ","").replace("!","").replace(" ","").replace("?",""). PolyGeo is correct, this is a python question more than an ArcGis question. May 16, 2016 at 22:04
  • 2
    the ! may cause problems and may need to be escaped due to arcpy using ! to identify field names
    – Midavalo
    May 16, 2016 at 22:05

3 Answers 3

6

Even though this question can be considered off topic I though I would make a suggestion.

Try using the Python re module in the Field Calculator. Here is a simple example used in the ArcMap field calculator.

Pre-Logic Script Code

 import re
 def repChar(strg)
     regexChars = '[!@$*#]'
     line = re.sub(regexChars,'',x)
     return line

Field

repChar(strg)

I know this isn't a very detailed excample but it's all I have time for at the moment.

2
  • Does the field calculator recognize imported modules? May 16, 2016 at 22:04
  • 3
    Yes it does. I've done this many times with no problem.. you don't want to get too complex as there's no debugging though. May 16, 2016 at 22:05
2

Your single line

!file!.replace(" ","")

can be added to by including more .replace()

!file!.replace(" ","").replace("?","").replace(",","").replace(chr(33),"")

and so on. You may need to escape some of the characters, or use their chr() code if the field calculator doesn't like them. chr(33) in my example replaces the !.

Chr codes can be retrieved from: http://www.ascii-code.com/ or by opening the python window in ArcMap and typing ord("!"), for example, or whatever character you want to find into ord().

5
  • Thank you for this. Where can I get a list of chr codes? May 16, 2016 at 22:25
  • 3
    They're ASCII codes, try ascii-code.com May 16, 2016 at 22:26
  • 1
    or in the python window type ord("!") which will return the chr() code 33. Put whatever character you want to find into ord()
    – Midavalo
    May 16, 2016 at 22:27
  • This worked wonderfully! I love the simplicity of it! I did not know adding more .replace() after the first one was possible. Thank you so very much! May 16, 2016 at 22:34
  • Could the down-voter please leave a comment?
    – Midavalo
    May 16, 2016 at 22:44
2

It's not pretty, but

''.join([x for x in list(!file!) if x not in ["!","@","$"]])

should remove any single character you specify in the list, and return the resulting characters as a string (the "etc" in your example wouldn't work, however).

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.