7

I want to select all of the features by attribute where the attribue in myattrib is like...

Selection "criteria H". That is not a problem.

However then I need to replace (myattrib," H", "") except I only want to replace the H at the end of the attribute, not within, like "toast is Hot H".

In this record I want the value to end up as "toast is HotH"

There are 15k records but only 4800 have the value I am replacing. And it is always the same value.

edit response:

No. All values I want to fix end in H with a space in front of it. I am depending on search replace but not sure how to leave the space in front of Hot.

In my example I am able to select with the "% H" which specifies there is nothing after the H so the select won't get Hot even though there is a space before it. However when it comes to replace it does a literal replace on " H" and doesn't care about the ot.

I think if I knew some regex and "IF" arcmap supports that, It might do the trick (from what I have seen on the FMEtalk forum). I'm sure someone can talk me through this.

4
  • what is your data store? shapefile, sde (sql server, oracle, postgres), fgdb? I think you might be looking at some regexp work. Commented Dec 8, 2010 at 23:08
  • 1
    Have you tried using the field calculator in 10.0, with a python pre-logic code block that leverages the EndsWith function? Commented Dec 8, 2010 at 23:54
  • 1. shapefile, I wish I knew how regex works.
    – Brad Nesom
    Commented Dec 9, 2010 at 2:50
  • 2. I am using the calculator in 10. but the pyth pre-logic I haven't looked for prior. I guess I will dig and see if I can figure anything out. BTW I am not a real programmer (pay no attention to that man behind the curtain!)
    – Brad Nesom
    Commented Dec 9, 2010 at 2:52

2 Answers 2

10

Yes Regular expressions are probably the best way to do this, and yes you can use regular expressions in the Calculate Field tool

First the regular expression

>>> import re
>>> val = 'National and Provincial capital'
>>> print re.sub('al$', 'X', val)
National and Provincial capitX

I'm subsituting "a" followed by "l" followed by "$" (which means the end of the sting) with the string 'X'. So 'capital' becomes 'capiX', while 'National' and 'Provincial' are unchanged.

Now use this same logic inside the code block for CalculateField. Set the calculateField parameters as follows -'Field Name" param to the output field name (i recommend calc'ing into a new field at least until you're sure the output is correct).

-'Expression' param to this

myroutine(!my_input_field_name!)
  • 'Expressioin Type' param to PYTHON

  • 'Code block' param to this

.

import re
def myroutine(val):
    return re.sub('al$', 'X', val)

Some docs if you want to read up.

3
  • The more generic form of this following the Brad's example would be re.sub(r'\s(H)$', \1, val). Which isn't really necessary as he states that value is always the same but I thought I'd throw that out there.
    – Sean
    Commented Dec 9, 2010 at 15:44
  • Thanks gotchula for making it so easy for me. I did try it and found a bug in arcmap 10 (don't have a nim3 yet), I will work on getting whatever the problem is unkinked. I wish I had more time (I would spend it on regex, python, and gdal).
    – Brad Nesom
    Commented Dec 9, 2010 at 16:44
  • I found that if you use the expression param in the code block and the code block in the expression area you get an error. then the resulting message continues popping up not sure how many times. When I did it right it worked the CHARM. Thanks gotchula
    – Brad Nesom
    Commented Dec 9, 2010 at 20:24
2

So we 100% sure good old search & replace won't cut the mustard? Is the main issue, you also have values like:

'toast is Hot H and burnt'

  • And these kinds of attributes you want to ignore, but the search and replace can't be tweaked to exclude these attributes?

alt text

0

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.