I'm attempting to join two tables.
The join validation report indicates several fields with illegal characters. I'm editing a large county file with over 1000 parcels. How to remove characters such as "-" or "/" for a whole column of data without doing it manually?
how many illegal characters? If you can use the field calculator a simple !YourFieldName!.replace('-','') using a python parser would be a simple approach. For many replacements a small function (def) could be written
To add on to Dan's comment. If you have a list of special characters to remove you can do it in the field calculater inside a def with something similiar to .....
>>> import string
>>> StringThatNeedsWork = "The - Time ! was well f$$$### spent don't, you agree? 12345% 1.234"
>>> removethesechars = ['$','#','%','-']
>>> StringThatNeedsWork.translate(None,''.join(removethesechars))
"The Time ! was well f spent don't, you agree? 12345 1.234"
If by illegal characters you mean all non-alphanumeric characters, you can use a list comprehension with str.isalnum():
testString = 'foo-bar#baz?qux@127/\\9]' testStringCleaned = "".join(x for x in testString if x.isalnum()) # foobarbazqux1279
Found on Stack Overflow - Turn a string into a valid filename?
Note that using .isalnum() will strip out significant '." and spaces as well testString = " North Highway Route 2.1" ---> "NorthHighwayRoute21" if that is ok then it is a quick easy way!
Sometimes I do this, and replace and illegal chars or spaces with an underscore:
testStringCleaned = "".join([x if x.isalnum() else "_" for x in testString]) # foo_bar_baz_qux_127__9_
Ted , What about your answer in same question? Does it work?
https://community.esri.com/message/688777-odd-special-character-in-field-value
Yes that works just fine, however, that solution is run on the database and not gis software, and specifically it removes "ALL" characters except for the ones with ASCII values between 0 and 255. So spaces and decimal values are not stripped. In this case, I am assuming the individual wanted to use the field calculate and only remove certain characters.
In additional to 7.1. string — Common string operations — Python 2.7.13 documentation , 7.2. re — Regular expression operations — Python 2.7.13 documentation can also be used:
>>> StringThatNeedsWork = "The - Time ! was well f$$$### spent don't, you agree? 12345% 1.234"
>>> re.sub("[$,#,%,-]","", StringThatNeedsWork)
"The Time ! was well f spent don't you agree? 12345 1.234"
>>>
I like yours better! I have a long way to go in learning Python!