Replace() string function isn't working within Field Calculator and arcpy

4916
12
Jump to solution
11-23-2015 12:06 PM
EugeneDurshpek
New Contributor III

I'm trying to figure out what's wrong with my python syntax.

I have an attribute table with 4 fields containing paths to media files. I'm trying to convert the paths so that they behave more like hyperlinks on our network. The existing paths (added to the tables via 3rd party proprietary software) include network drive i.e. M:\GraniteXP\...

I'm trying to use find and replace to replace "M:\GraniteXP" with "\\vancouver.root.local\...\GraniteXP".

I can do this perfectly fine using field calculator in ArcMap as well as Model Builder. However, when I export the model to Python; the script runs (successfully), but the values are not updated. There is not error to tell me what's failing.

Here's a snippet from my script. I do have a MakeFeatureLayer prior to this snippet. (Note: this particular script is trying to change it back to the network drive pathing for testing purposes).

arcpy.SelectLayerByAttribute_management("lyrsdePipeInsp", "NEW_SELECTION", "Photo1 is not null")

arcpy.CalculateField_management("lyrsdePipeInsp", "Photo1", "Replace( [Photo1], \"\\\\vancouver.root.local\\cityapps\\GraniteXP\", \"M:\\GraniteXP\" )", "VB", "")

arcpy.SelectLayerByAttribute_management("lyrsdePipeInsp", "NEW_SELECTION", "Photo2 is not null")

    arcpy.CalculateField_management("lyrsdePipeInsp", "Photo2", "Replace( [Photo2],  \"\\\\vancouver.root.local\\cityapps\\GraniteXP\", \"M:\\GraniteXP\" )", "VB", "")

arcpy.SelectLayerByAttribute_management("lyrsdePipeInsp", "NEW_SELECTION", "Photo3 is not null")

arcpy.CalculateField_management("lyrsdePipeInsp", "Photo3", "Replace( [Photo3], \"\\\\vancouver.root.local\\cityapps\\GraniteXP\", \"M:\\GraniteXP\" )", "VB", "")

arcpy.SelectLayerByAttribute_management("lyrsdePipeInsp", "NEW_SELECTION", "Video is not null")

arcpy.CalculateField_management("lyrsdePipeInsp", "Video", "Replace( [Video], \"\\\\vancouver.root.local\\cityapps\\GraniteXP\", \"M:\\GraniteXP\" )", "VB", "")

Thanks!

Eugene Durshpek

0 Kudos
12 Replies
EugeneDurshpek
New Contributor III

Yes, we are running 64-bit OS. Thank you for the link. It's helpful.

Eugene D.

0 Kudos
Luke_Pinner
MVP Regular Contributor

You've got your find string and replacement string in the wrong order.

Try:

arcpy.CalculateField_management("lyrsdePipeInsp", "Photo1", r'Replace( [Photo1],"M:", "\\vancouver.root.local")',"VB")

Or:

arcpy.CalculateField_management("lyrsdePipeInsp", "Photo1", r"!Photo1!.replace('M:', r'\\vancouver.root.local')", "PYTHON_9.3")

EugeneDurshpek
New Contributor III

Thanks for reply Luke. I have seen a bunch of suggestions to using the update cursor approach so I was trying to use that first. Unfortunately I haven't been able to get past some workspace and SDE issues.

As far as having my find string and replacement strings backwards; I do realize that. I've been calculating data back and forth while testing my script. At the moment my actual data reflects the successful field calculation with replace() that I ran with Model Builder. I'm trying to change it back with python and then I'll replace the order. (Hope that makes sense).

For whatever reason I just cannot get the VB syntax to work while editing an SDE layer. The script runs, and I get a little message saying that it ran successfully, but my data remains unchanged.

I was finally able to use your syntax suggestion and make it work in a file geodatabase, which I end up appending to SDE to accomplish my final goal.

0 Kudos