Replace string in field attribute

16010
9
04-20-2012 03:54 AM
NikolaosGaliatsatos
New Contributor
In ArcGIS 9.x was in the options..., Find and Replace. In ArcGIS 10.x, we have to use Python script !FieldName!.replace("OldText", "NewText")

I may be missing something here, but why on earth the Python script is not working? Here is what I type in the Expression:

!Hyperlink!.replace("Dec20112", "Dec2011")

, and here is the message from the Geoprocessing results:

ERROR 000539: <type 'exceptions.SyntaxError'>: invalid syntax (<expression>, line 1)
Failed to execute (CalculateField).

I have managed to do the replacing with VBscript: Replace ([Hyperlink], "Dec20112", "Dec2011")

Any idea about what may be wrong with Python here? It says PYTHON 9.3; I'm running ArcGIS10sp4 under Win7-64bit.

Cheers,
Niko
Tags (2)
0 Kudos
9 Replies
MarcinGasior
Occasional Contributor III
In ArcGIS10 there's Find&Replace funcion when you browse Attribute Table:
[ATTACH=CONFIG]13694[/ATTACH]
To be able to replace something, you have to be in edit session.

BTW I've no idea why .replace() do not work for you. I've made some test and it worked even for field with Date type.
NikolaosGaliatsatos
New Contributor
Yes, I am in the Edit session (I tried both in and outside the Edit session). I even tried the following code:

Block:
import re
def replace(value):
   return re.sub ('Dec20112', 'Dec2011', value)

Expression:
replace(!Hyperlink!)

which did not work either. I'm not a Python expert, but shouldn't this work?

Yes, "Find and Replace" is there..., for some reason (frustration for Python not working perhaps?) I didn't see it.
0 Kudos
RyanForbes1
New Contributor III
I think the issue with the python is that .replace() is a string function and it looks to me like you're trying to apply it to the field name.  I don't know for sure without playing with your code, but I imagine that you would need an updateCursor and then set the fieldValue = fieldValue.replace()
0 Kudos
RyanForbes1
New Contributor III
I think the issue with the python is that .replace() is a string function and it looks to me like you're trying to apply it to the field name. I don't know for sure without playing with your code, but I imagine that you would need an updateCursor and then set the fieldValue = fieldValue.replace()
0 Kudos
DarrenWiens2
MVP Honored Contributor
Just FYI, this works as the expression in ArcGIS 10, SP2: !Hyperlink!.replace("originalstring","replacewiththis")
0 Kudos
NikolaosGaliatsatos
New Contributor
Many thanks for your responses.

I'm going through the Python training of ESRI right now, and it seems there is some problem with Python in ArcGIS (could it be the installation in my machine?). For example, I'm trying to run this piece of code in Field Calculator:
def label(name, type):
 if type == "HIGH":
  return name + " HS"
 elif type == "MIDDLE":
  return name + " MS"
 elif type == "ELEMENTARY":
  return name + " ELEM"


with Expression:
label( !NAME!, !TYPE! )

and I'm receiving a 999999 error. Even if I put the code that the ESRI training is providing (.cal file), the same error comes up.

Sorry to bother you guys with this..., nothing to do with programming..., just the usual ArcGIS bugs.
0 Kudos
BruceNielsen
Occasional Contributor III
I think 'type' is a reserved word. Try replacing it with a different variable name.
0 Kudos
NikolaosGaliatsatos
New Contributor
Following the advice of the ESRI people, I called the Administrator to run a "repair" for the ArcGIS Desktop installation. This fixed all issues and the described code worked.

Many thanks to all for your helpful comments.
0 Kudos
UsmanYousaf1
New Contributor
Hi

It is fairly simple. Try this one. It works for me perfectly.

replace([Field_Name],"oldtextToFind","NewText")