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.
7 Answers 7
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('"', '')
-
Well, you got my hopes up, but that doesn't work for me. Neither the codeblock nor the straight up
'!testing!'
Random Geo guy– Random Geo guy2012年06月25日 21:39:58 +00:00Commented 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?Alex Markov– Alex Markov2012年06月25日 21:52:54 +00:00Commented Jun 25, 2012 at 21:52
-
2You know what? you're right, it does work. I hadn't had my 3:00 wake up juice. You win.Random Geo guy– Random Geo guy2012年06月25日 22:03:18 +00:00Commented Jun 25, 2012 at 22:03
-
I have managed to solve problem with a single quote. Check my update to the answer.Alex Markov– Alex Markov2012年06月25日 22:43:16 +00:00Commented Jun 25, 2012 at 22:43
-
The Pre-Logic Script Code worked fine when used within a script - many thanksmeryloo– meryloo2013年09月10日 21:09:48 +00:00Commented Sep 10, 2013 at 21:09
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
-
What is the magic trick here, your script did not work?artwork21– artwork212012年06月25日 18:50:27 +00:00Commented Jun 25, 2012 at 18:50
-
This doesn't work. It looks like it should, but it doesn't.KiloGeo– KiloGeo2012年06月25日 18:50:52 +00:00Commented Jun 25, 2012 at 18:50
-
Both of your fields are text correct?TurboGus– TurboGus2012年06月25日 19:02:00 +00:00Commented Jun 25, 2012 at 19:02
-
@Gus, Yes field is text.artwork21– artwork212012年06月25日 19:05:51 +00:00Commented Jun 25, 2012 at 19:05
-
2I am not using 10.1. I am using 10.0 - that archaic version that is sooooo 2011.Random Geo guy– Random Geo guy2012年06月25日 20:22:07 +00:00Commented Jun 25, 2012 at 20:22
Here is a non script way to do it.
- Start edit session on you layer.
- Open up table and highlight field column.
- Select Table Options drop down arrow and select Find & Replace.
- Select Replace tab and enter Find what:
"
, select Replace all (you will have to click the Replace All button twice).
-
while I appreciate the possibility, I'm not only wanting to replace the double quotes and I'd rather work via scripting.Random Geo guy– Random Geo guy2012年06月25日 20:19:14 +00:00Commented Jun 25, 2012 at 20:19
A hybrid approach worked on my system:
'!testing!'.replace("\"","")
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.
-
I realize this doesn't help with the 'Find' portion of your question...Timothy Michael– Timothy Michael2012年06月25日 21:22:52 +00:00Commented Jun 25, 2012 at 21:22
hi try using single quotes like this !test!.replace('"','')
-
Hi, this does not work.Random Geo guy– Random Geo guy2012年06月25日 20:29:44 +00:00Commented Jun 25, 2012 at 20:29
I found it pretty easy with the old VBScript Function: Replace ([testing],"""","")
Explore related questions
See similar questions with these tags.
!testing![1:len(!testing!)-1]