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!)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked May 16, 2016 at 21:27
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. Commented 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. Commented 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. Commented 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. Commented 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 Commented 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.

answered May 16, 2016 at 22:02
2
  • Does the field calculator recognize imported modules? Commented 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. Commented 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().

answered May 16, 2016 at 22:17
5
  • Thank you for this. Where can I get a list of chr codes? Commented May 16, 2016 at 22:25
  • 3
    They're ASCII codes, try ascii-code.com Commented 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() Commented 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! Commented May 16, 2016 at 22:34
  • Could the down-voter please leave a comment? Commented 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).

answered May 16, 2016 at 22:11

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.