[GIS] Find/Replace Double Quotes using Python Parser of ArcGIS Field Calculator

arcgis-10.0arcgis-desktoparcmapfield-calculatorpython-parser

I'm trying to use the Field Calculator (ArcMap 10.0) to strip out double quotes in an address field (I didn't put them in there, believe me). I've tried many incantations but am still unable to even get off the ground with a calculation. yes, both fields are string, and both are of acceptable length.

This calculation:
Sample calculation

Results in this:

fail

With this error message in the Results dialog:

error message

And I repeat, I didn't put them in there.

Best Answer

I have found that in 10.0 Field Calculator is quite weird.

But I've managed to get it work. The main idea is to enclose field name with single quotes.

Example. let suppose we have fields text1 and text2. Rather than Calculating field text2 with expression !text1!, which probably will fail, try this one: '!text1'. As you see I am using single quotes here.

So, back to your task. It will be more clear to use Pre-Logic Script Code:

def calc(value):
    return value.replace('"', '')

Expression will be:

calc('!text1!')

I hope it will work for you.

I have not experimented further but I think that such weird behavior happens because field calculation is translated into the call to ArcToolbox tool CalculateField_management and expression is provided as a parameter to it (probably additionally enclosed with single or double quotes).

UPDATE:

My previous solution will fail in case if there are single quotes in the values of field text1.

Now I have managed to get it work both in case when there are chars ' and " (single and double quotes) inside any value in attribute text1.

Here is expression, which will return original string, supporting both types of quotes:

'''!text1!'''[1:-1]

For your task it can be extended to (without Pre-Logic Script Code):

'''!text1!'''[1:-1].replace('"', '')
Related Question