Select to view content in your preferred language

User Input Conditional ( x in vs x ==)

1219
9
09-25-2017 07:34 AM
DevinUnderwood2
Regular Contributor

I am having an issue with raw_input as a variable x. The user types the name and receives returned values. x is in the ly variable is not working.( Line 20) I know that I need to set this up slightly different but not sure how. (Line 22,23)

I can set the variable x as a hardcoded name and I get the correct results which confirms everything else is correct. (Line 21)

import arcpy
import os
#Set folder space
folder = r'S:\\GIS\ServerMXDs\\_Future_Services\\CityWorks'

for filename in os.listdir(folder):
    fullpath = os.path.join(folder, filename)
    if os.path.isfile(fullpath):
        basename, extension = os.path.splitext(fullpath)
        if extension.lower() == ".mxd":
            mxd = arcpy.mapping.MapDocument(fullpath)
            dfs = arcpy.mapping.ListDataFrames(mxd)
            for df in dfs:
                layers = arcpy.mapping.ListLayers(mxd, "", df)
                for layer in layers:
                    if layer.isFeatureLayer:
                        lyr_source = layer.dataSource
                        lyr_name = layer.name.encode("utf8", "replace")
                        ly = lyr_source.encode("utf8", "replace")-
                        #x = raw_input("type in feature name: ")
                        x = 'ServiceEstablishments'
                        if x in str((ly.split ('\\')[2:])):
                            print filename + " " + "Service Name:" + " " + df.name + " " + "Layer Name:" + " " + lyr_name


                


                 
                        

0 Kudos
9 Replies
JoshuaBixby
MVP Esteemed Contributor

When you say it isn't working with raw_input, what exactly isn't working?  Is there an error?  If so, what error?  Does it run but not print anything?

And, where/how are you running this code? 

0 Kudos
DevinUnderwood2
Regular Contributor

It runs with  a  type in feature name:  prompt that repeats every time I type something and yield no results.

I am running it in Jupyter Notebook

0 Kudos
DanPatterson_Retired
MVP Emeritus

Not sure if I follow the logic why you are only looking at the slice portion ( ie [2:] ), but I suspect it might have something to do with it....

lyr = r'my\ServiceEstablishments\stuff'

a = 'ServiceEstablishments'

a in lyr
True

a in lyr.split('\\')
True
a in lyr.split('\\')[2:]  # should fail the slice is wrong
False

a in lyr.split('\\')[1:]  # change the slice... remember slices are 0-based
True
DevinUnderwood2
Regular Contributor

I use the following print statement  and get the correct results as seen below (the feature class name)

print "Feature Classes Name:" + str((ly.split ('\\')[2:]))

I have it indexed correctly and results are correct but I have an issue when using the user input.

Feature Classes Name:['BACK.Address_Points']

0 Kudos
DanPatterson_Retired
MVP Emeritus

Well it returns a string, but is that a little minus sign on the line before your commented out line?

I use python 3.X so raw_input is now 'input', just to show that it does return a string

b = input("type in feature name: ")

type in feature name: back.atyoub

'back.atyou'‍‍‍‍‍‍

c = 'here is a string... back.atyou'

b in c
True
0 Kudos
DevinUnderwood2
Regular Contributor

Thanks for this advice.

I haven't used the in very much, I did and it works.

I now see that the  

if x in str((ly.split ('\\')[2:])):

is causing the issue.

It is indexed correctly, but something is not right in the loop input syntax.

0 Kudos
RandyBurton
MVP Alum

Just a couple of things that might help in my understanding your question.  First, escaping vs. raw string, I just want to make sure this isn't the actual issue.

folder = r'S:\\GIS\ServerMXDs\\_Future_Services\\CityWorks'
# should be
folder = r'S:\GIS\ServerMXDs\_Future_Services\CityWorks'
# or
folder = 'S:\\GIS\\ServerMXDs\\_Future_Services\\CityWorks'‍‍‍‍‍

Second. Are you examining the layer source path for a specific name, such as layer.dataSource contains "ServiceEstablishments"? Are you also checking layer.name for this value as "ServiceEstablishments" may be an alias for a data source path that does not contain the phrase?

I think your "in" and "split" would otherwise work.

DanPatterson_Retired
MVP Emeritus

sort of agree, but at the operating system level...

a = r'C:\Temp\a\aa\a0\a00'

os.path.exists(a)
True

b = r'C:\\Temp\\a\\aa\\a0\\a00'

os.path.exists(b)
True
0 Kudos
DevinUnderwood2
Regular Contributor

I tried the different \\ regarding escape characters and I don't get any different results.

I am looking up layer.dataSource which is the actual feature class name.

0 Kudos