Why is this unicode?

3178
2
09-23-2015 12:42 AM
GinoMellino
Occasional Contributor

Hello,

Apologies, I understand this is a basic issue but it confuses me. I have a script tool which I am writing in python to work with a particular MXD. In this script tool I have the following lines of code to reference two layers in the MXD's TOC.

subjectLayer = arcpy.mapping.ListLayers(mxd,subjectlayerName)[0]

subjectLayer.definitionQuery = ""

cadastralLayer = arcpy.mapping.ListLayers(mxd,"Cadastre")[0]

arcpy.AddMessage( cadastralLayer) #To test if layer is set correctly

cadastralLayer.definitionQuery = ""

The subjectLayer object gets set correctly. This is chosen as a parameter in the tools UI. The definition query also gets set correctly.The cadastralLayer object gets populated also (AddMessage shows grouplayername\layername) but I get a "Script failure: 'unicode' object has no attribute 'definitionQuery'" error when it tries to set a definition query on it. This object is set by referencing the layer name in the TOC.

My question is why is one a unicode and the other a layer object as they were both referenced using ListLayers? How do I correctly reference layers in the TOC?

Thanks in advance.

0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

Unicode is the new String

u'formerly called a string'  Unicode notice the u

'old school string'              string/text

Unicode supports more symbol/language character sets than ASCII, most(?) programming languages now support full(ish) Unicode support:(

http://unicode.org/ 

https://en.wikipedia.org/wiki/Unicode

so when they say Unicode does not support... read string does not support...

which means you are passing a string to the function instead of what it wants.

Now to your immediate concern

ListLayers ...

http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/listlayers.htm

returns

Returns a Python list of Layer objects that exist within a map document (.mxd), a data frame within a map document, or layers within a layer (.lyr) file.

and

ListLayers always returns a Python list object even if only one layer is returned. In order to return a Layer object, an index value must be used on the list (e.g., lyr = arcpy.mapping.ListLayers(mxd)[0]).  For loops on a list provide an easy mechanism to iterate through each item in a list (e.g., for lyr in arcpy.mapping.ListLayers(mxd):).

So I suspect that in one instance, your code is returning the layer name (in Unicode) and in the other it is returning a layer object ( name? object?  not the same... an apple is only an apple only in English (ie pomme ... French))

So make sure you know whether you want the object... so you can access its properties ... or just the objects name

If this isn't the case ... never mind

RebeccaStrauch__GISP
MVP Emeritus

​I think you error message is coming from the addMessage line.  Try this instead

print arcpy.mapping.ListLayers(mxd, "", cadastralLayer)[0].name

as shown in ListLayers—Help | ArcGIS for Desktop

As Dan mentioned, you are setting the cadastralLayer variable to an object and need to grab a property, name in the sample above, to print for verification that it worked.

the help also has other code samples that may help.

0 Kudos