r'  Raw String Suppress Escape not working in arcpy ArcGIS 10

1054
3
04-20-2011 04:31 AM
MikeMeeks
New Contributor III
I'm converting all my 9.3.1 python scripts to 10.0 sp1 arcpy scripts and getting fatal crashes in IDLE when I use the r raw string when defining constants which worked fine in the 9.3.1 gp.

Example:
Layers = r'Road Centerline'
Scales = r'2000;900'
All = r'Recreate All Tiles'

arcpy.ManageMapServerCacheTiles_server("server","BaseCache/RoadCache", "Layers", Layers, Scales, All.......etc)

After I changed the constants to replace r with " it worked
Example:
Layers = "'Road Centerline'"
Scales = "'2000;900'"
All = "'Recreate All Tiles'"

I've used r for years with python and had no problems in the past.  It was a habit and am now having to change all references of r when used in an arcpy call.  The r still works with python functions such as open(r'C:\GIS Scripts\RoadCacheLog.txt', 'a')

Has arcpy been designed not to accept the r now?  because it worked with gp!

Mike
Tags (2)
0 Kudos
3 Replies
LoganPugh
Occasional Contributor III
If the singlequotes are supposed to be literal parts of the string then yes they'll have to be enclosed in double-quotes or escaped a backslash (with no raw string specifier).

Take a look at the Python lexical analysis doc: http://docs.python.org/release/2.6.5/reference/lexical_analysis.html#literals

Here is how short strings (non-triple-quoted strings) are defined:
shortstring     ::=  "'" shortstringitem* "'" | '"' shortstringitem* '"'



So you have to enclose strings in either double quotes or single quotes, and since you want singlequotes in your string, enclose it with double quotes so you don't have to escape the singlequotes.
0 Kudos
MikeMeeks
New Contributor III
If that is the case, how have my python scripts worked the past 3 years from Release 9.2 thru 9.3.1? Mike
0 Kudos
LoganPugh
Occasional Contributor III
Perhaps the expected syntax of that specific command changed between ArcGIS 9.3 and 10. I would say if it works the way you have it now then keep doing what works. Alternatively, a simple test would be to see if the command works without the literal single-quotes, so just:
Layers = "Road Centerline"
Scales = "2000;900"
All = "Recreate All Tiles"


If that doesn't work then it has nothing to do with raw strings and everything to do with whether literal single-quotes are needed.

The example in the help on the ManageMapServerCacheTiles_Server isn't very enlightening as they use empty strings for those parameters.
0 Kudos