Select to view content in your preferred language

Arcpy trouble with string passsed with GetParameterAsText

1848
9
Jump to solution
11-30-2016 07:56 AM
JimCousins
MVP Regular Contributor

I have a very simple program to toggle on and off layers and export PNGs. The issue is trying to pass a string to search for in the ListLayers result.

If I pass the parameter with the tool dialog, I get no return, even though I AddMessage the string and it shows as expected (see the 4th line under Messages).

If I hardcode the string on line 27:

     for lyr in arcpy.mapping.ListLayers(mxd, "*Events", df):

, no troubles.

I expect I am missing something basic in the interpolation of the passed string, but I have tried enclosing in additional quotes and using escapes before the quotes, to no avail.

Best Regards,

Jim

import arcpy

Folder = arcpy.GetParameterAsText(0)
Folder = Folder.replace("\\","/")
SearchStr = arcpy.GetParameterAsText(1)
arcpy.AddMessage( SearchStr)
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"Layers") [0]

def ExportPNG(sspaText):
    # Update the layout text
    #for el in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","SSPA_Title"):
    #    el.text = sspaText
    arcpy.AddMessage("Processing " + sspaText)
    arcpy.RefreshActiveView()
    # Export the Map
    arcpy.mapping.ExportToPNG(mxd, Folder + "/" + sspaText + ".png", resolution=600)

def SetVisibility(visNam):
    for lyr in arcpy.mapping.ListLayers(mxd,visNam,df):
        lyr.visible = 'true'

def SetInvisibility(visNam):
    for lyr in arcpy.mapping.ListLayers(mxd, visNam,df):
        lyr.visible = 'false'

for lyr in arcpy.mapping.ListLayers(mxd, SearchStr, df):

    baseName = lyr.name
    arcpy.AddMessage(baseName)

    # Call the SetVisibility function to turn on the layer
    SetVisibility(baseName)
    arcpy.RefreshActiveView()

    sspaText = lyr.name[:-7]

    ExportPNG(sspaText)
    # Call the SetInvisibility function to turn off the layer
    SetInvisibility(baseName)
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Have you tried passing just *Events, no quotes.  Since the parameter is defined as String, whatever you are passing is already being interpreted as a string.  I would have to double check, but I believe quotes in string parameters are escaped to be treated as literal single or double quotes, which isn't what you want because you don't have literal quotes in your layer names.

View solution in original post

9 Replies
RebeccaStrauch__GISP
MVP Emeritus

A guess (without testing), remove lines 20 and 24 above (and then adjust the indents after, of curse).

Since you are already passing the basename on 30 and 34,

0 Kudos
DanPatterson_Retired
MVP Emeritus
>>> search_string = "*Events"
>>> search_string
'*Events'
>>> print(search_string)
*Events
>>> s = '"{}"'.format(search_string)
>>> s
'"*Events"'
>>> print(s)
"*Events"

It is almost like it has no double or single quotes..  Examine the above. especially line 6 to generate quotes around strings.  I don't think this is an AddFieldDelimiters issue that is commonly suggested, but have a look at it as well

JimCousins
MVP Regular Contributor

I don't quite follow. The code runs without issues if I hardcode the SearchStr variable.

Lines 20 and 24 turn on the layer prior to export, and then turn it back off before the next event theme is turned on and exported.

Line 30 is simply an add message statement so I can see what the code is working with, and line 34 is just an active view refresh. 

Perhaps the line numbers are skewed?

Thanks for your input just the same!

0 Kudos
DanPatterson_Retired
MVP Emeritus

It appeared to me that the quotes might be being dropped... if "*Events" with the quotes worked, then *Events wouldn't

0 Kudos
JimCousins
MVP Regular Contributor

That was my original thought as well. I have just tried:

SearchStr = arcpy.GetParameterAsText(1)

SearchStr = '"{}"'.format(SearchStr)

No luck

Tried:

SearchStr = '"'+str(SearchStr)+'"'

No luck

The parameter is set to a string type in the dialog. 

Actually at a bit of a loss for what else to try.

0 Kudos
DanPatterson_Retired
MVP Emeritus

print it using print(SearchStr) and if running from a dialog, arcpy.AddMessage(SearchStr) and see what if anything it is returning.  In any event, if hardcoding works and the filter doesn't the answer lies in what it is returning.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Have you tried passing just *Events, no quotes.  Since the parameter is defined as String, whatever you are passing is already being interpreted as a string.  I would have to double check, but I believe quotes in string parameters are escaped to be treated as literal single or double quotes, which isn't what you want because you don't have literal quotes in your layer names.

JimCousins
MVP Regular Contributor

That is a good thought, Joshua. I had tried that as well as using single quotes, double quotes, escaped quotes (/', \', " ' ", ' " ') No joy yet. I have submitted a support ticket to ESRI, and will let you all know what shakes out.

Regards,

Jim

0 Kudos
JimCousins
MVP Regular Contributor

I have restarted my machine and it runs successfully now. I have no idea why it was failing previously, as I can look at the Geoprocessing Results tab, and see the same arguments entered with an unsuccessful result as what I have used for a successful result today. The entry for the string does not need any quotes, as Joshua surmised. ESRI support ran my code as provided with no troubles, I tried it on a different machine with no troubles, and then tried it again on mine with success. No idea what was preventing it before. Thanks to all for your help and input. 

Best Regards,

Jim

0 Kudos