Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!)
share|improve this question
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 2 days ago
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. – coffee and gis 2 days ago
1  
Yes, you can do this in ArcMap field calculator with the Python parser. I would probably try it with regex. – Peter 2 days ago
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. – Michael Miles-Stimson 2 days ago
2  
the ! may cause problems and may need to be escaped due to arcpy using ! to identify field names – Midavalo 2 days ago
up vote 2 down vote accepted

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().

share|improve this answer
    
Thank you for this. Where can I get a list of chr codes? – coffee and gis 2 days ago
3  
They're ASCII codes, try ascii-code.com – Michael Miles-Stimson 2 days ago
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 2 days ago
    
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! – coffee and gis 2 days ago
    
Could the down-voter please leave a comment? – Midavalo 2 days ago

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.

share|improve this answer
    
Does the field calculator recognize imported modules? – coffee and gis 2 days ago
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. – Michael Miles-Stimson 2 days ago

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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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