16

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.

2
  • 1
    So you're copying them with the quotes in your test, did you already attempt stripping the quotes with something like !testing![1:len(!testing!)-1]
    – Roy
    Commented Jun 25, 2012 at 18:30
  • The snippet you provided dies with the same message. Commented Jun 25, 2012 at 20:20

7 Answers 7

16

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('"', '')
5
  • Well, you got my hopes up, but that doesn't work for me. Neither the codeblock nor the straight up '!testing!' Commented Jun 25, 2012 at 21:39
  • Hm, it worked on my 10.0sp4. May be it is because somewhere in your field there is actually single quote... Have you considered using UpdateCursor in python script? Commented Jun 25, 2012 at 21:52
  • 2
    You know what? you're right, it does work. I hadn't had my 3:00 wake up juice. You win. Commented Jun 25, 2012 at 22:03
  • I have managed to solve problem with a single quote. Check my update to the answer. Commented Jun 25, 2012 at 22:43
  • The Pre-Logic Script Code worked fine when used within a script - many thanks
    – meryloo
    Commented Sep 10, 2013 at 21:09
10

If you are using version 10.1 and you are sure you want to get rid of every instance of double quotes you can use:

!testing!.replace("\"","")

If anyone knows why this works in 10.1 and not 10.0 I would be interested.

Here are the results entry from my run.

enter image description here

12
  • What is the magic trick here, your script did not work?
    – artwork21
    Commented Jun 25, 2012 at 18:50
  • This doesn't work. It looks like it should, but it doesn't.
    – KiloGeo
    Commented Jun 25, 2012 at 18:50
  • Both of your fields are text correct?
    – TurboGus
    Commented Jun 25, 2012 at 19:02
  • @Gus, Yes field is text.
    – artwork21
    Commented Jun 25, 2012 at 19:05
  • 2
    I am not using 10.1. I am using 10.0 - that archaic version that is sooooo 2011. Commented Jun 25, 2012 at 20:22
8

Here is a non script way to do it.

  1. Start edit session on you layer.
  2. Open up table and highlight field column.
  3. Select Table Options drop down arrow and select Find & Replace.
  4. Select Replace tab and enter Find what: ", select Replace all (you will have to click the Replace All button twice).
1
  • while I appreciate the possibility, I'm not only wanting to replace the double quotes and I'd rather work via scripting. Commented Jun 25, 2012 at 20:19
3

A hybrid approach worked on my system:

'!testing!'.replace("\"","")
2

I haven't tried this inside of Arc, but I was able to remove double quotes from a string in a Python IDE in this (quick) way:

aStr ='"test"'

a1 = aStr.lstrip('"')
a2 = a1.rstrip('"')

print a2

a2 prints test, without quotes.

1
  • I realize this doesn't help with the 'Find' portion of your question... Commented Jun 25, 2012 at 21:22
0

hi try using single quotes like this !test!.replace('"','')

1
  • Hi, this does not work. Commented Jun 25, 2012 at 20:29
0

I found it pretty easy with the old VBScript Function: Replace ([testing],"""","")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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