<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic 'Layer' object is unsubscriptable in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/layer-object-is-unsubscriptable/m-p/698035#M54107</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am attempting to list all of the shapefiles in the "Layers" dataframe of my Table of Contents. I've also created a list of layer files (.lyr) in a specified folder. My goal is to assign a layer file in this folder to a shapefile in my TOC based on common characters between&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the two. I've set up both of these lists in my script. One block of code generates and prints the TOC shapefiles, another block generates the layer files in the specified folder.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;An entry in layer file list looks like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles\031g04_9_0_LX_2420009_1.lyr&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;An entry in a TOC shapefile list looks like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;031g04_9_0_LX_2420009_1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This I've managed to do without error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What I want to do next is take each shp in the TOC and match it to it's corresponding layer file in the folder.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I was hoping to do that by comparing a subsection of each element in each list.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For example, I would extract from the layer the "_9_0_LX_2420009_1" [59:-4] text string. If it equals same subsection of the shp TOC list, "_9_0_LX_2420009_1" [6:], I would apply the corresponding layer file to the shapefile. My code is pasted below.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Author: Zachary Bartlett
# February 15, 2012
# Location: N:\DOCUMENTATION\PYTHON\Canvec_Symbolization\canvec_autosym.py

# This tool searches the ArcMap TOC for Canvec shapefiles and symbolizes them
# based on layer files in:
#&amp;nbsp;&amp;nbsp; N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles

import arcpy, glob, os

arcpy.env.Workspace = r"N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles"
arcpy.env.OverwriteOutput = True

mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

layer_file_list = glob.glob(str(arcpy.env.Workspace) + "\*.lyr")

arcpy.AddMessage("Layer File List: ")
for layer in layer_file_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(layer)
print layer_file_list
arcpy.AddMessage("-------")

TOC_shp_list = arcpy.mapping.ListLayers(mxd, "", dataFrame)

arcpy.AddMessage("Table of Contents Shapefile list: ")
for shp in TOC_shp_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(shp)
print TOC_shp_list
arcpy.AddMessage("-------")

for shp in TOC_shp_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; shp_ext = shp[6:]
&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in layer_file_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr_ext = lyr[59:-4]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyr_ext
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr_ext == shp_ext:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ApplySymbologyFromLayer_management(shp, lyr)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del lyr, lyr_ext
&amp;nbsp;&amp;nbsp;&amp;nbsp; del shp, shp_ext

arcpy.RefreshTOC()
arcpy.RefreshActiveView()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run this script I get the following error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;type 'exceptions.TypeError'&amp;gt;: 'Layer' object is unsubscriptable&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (canvecautosym).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I even tried removing some of the problematic code and running the following (I simplified the for loop):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Author: Zachary Bartlett
# February 15, 2012
# Location: N:\DOCUMENTATION\PYTHON\Canvec_Symbolization\canvec_autosym.py

# This tool searches the ArcMap TOC for Canvec shapefiles and symbolizes them
# based on layer files in:
#&amp;nbsp;&amp;nbsp; N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles

import arcpy, glob, os

arcpy.env.Workspace = r"N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles"
arcpy.env.OverwriteOutput = True

mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

layer_file_list = glob.glob(str(arcpy.env.Workspace) + "\*.lyr")

arcpy.AddMessage("Layer File List: ")
for layer in layer_file_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(layer)
print layer_file_list
arcpy.AddMessage("-------")

TOC_shp_list = arcpy.mapping.ListLayers(mxd, "", dataFrame)

arcpy.AddMessage("Table of Contents Shapefile list: ")
for shp in TOC_shp_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(shp)
print TOC_shp_list
arcpy.AddMessage("-------")

for shp in TOC_shp_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; shp_ext = shp[6:]
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(shp_ext)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print shp_ext
&amp;nbsp;&amp;nbsp;&amp;nbsp; del shp, shp_ext

arcpy.RefreshTOC()
arcpy.RefreshActiveView()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run this script I get the same error as before.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Everything up to the first for loop runs OK.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 16:49:07 GMT</pubDate>
    <dc:creator>ZackBartlett</dc:creator>
    <dc:date>2021-12-12T16:49:07Z</dc:date>
    <item>
      <title>'Layer' object is unsubscriptable</title>
      <link>https://community.esri.com/t5/python-questions/layer-object-is-unsubscriptable/m-p/698035#M54107</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am attempting to list all of the shapefiles in the "Layers" dataframe of my Table of Contents. I've also created a list of layer files (.lyr) in a specified folder. My goal is to assign a layer file in this folder to a shapefile in my TOC based on common characters between&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the two. I've set up both of these lists in my script. One block of code generates and prints the TOC shapefiles, another block generates the layer files in the specified folder.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;An entry in layer file list looks like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles\031g04_9_0_LX_2420009_1.lyr&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;An entry in a TOC shapefile list looks like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;031g04_9_0_LX_2420009_1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This I've managed to do without error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What I want to do next is take each shp in the TOC and match it to it's corresponding layer file in the folder.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I was hoping to do that by comparing a subsection of each element in each list.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For example, I would extract from the layer the "_9_0_LX_2420009_1" [59:-4] text string. If it equals same subsection of the shp TOC list, "_9_0_LX_2420009_1" [6:], I would apply the corresponding layer file to the shapefile. My code is pasted below.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Author: Zachary Bartlett
# February 15, 2012
# Location: N:\DOCUMENTATION\PYTHON\Canvec_Symbolization\canvec_autosym.py

# This tool searches the ArcMap TOC for Canvec shapefiles and symbolizes them
# based on layer files in:
#&amp;nbsp;&amp;nbsp; N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles

import arcpy, glob, os

arcpy.env.Workspace = r"N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles"
arcpy.env.OverwriteOutput = True

mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

layer_file_list = glob.glob(str(arcpy.env.Workspace) + "\*.lyr")

arcpy.AddMessage("Layer File List: ")
for layer in layer_file_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(layer)
print layer_file_list
arcpy.AddMessage("-------")

TOC_shp_list = arcpy.mapping.ListLayers(mxd, "", dataFrame)

arcpy.AddMessage("Table of Contents Shapefile list: ")
for shp in TOC_shp_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(shp)
print TOC_shp_list
arcpy.AddMessage("-------")

for shp in TOC_shp_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; shp_ext = shp[6:]
&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in layer_file_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr_ext = lyr[59:-4]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyr_ext
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr_ext == shp_ext:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ApplySymbologyFromLayer_management(shp, lyr)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del lyr, lyr_ext
&amp;nbsp;&amp;nbsp;&amp;nbsp; del shp, shp_ext

arcpy.RefreshTOC()
arcpy.RefreshActiveView()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run this script I get the following error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;type 'exceptions.TypeError'&amp;gt;: 'Layer' object is unsubscriptable&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (canvecautosym).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I even tried removing some of the problematic code and running the following (I simplified the for loop):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Author: Zachary Bartlett
# February 15, 2012
# Location: N:\DOCUMENTATION\PYTHON\Canvec_Symbolization\canvec_autosym.py

# This tool searches the ArcMap TOC for Canvec shapefiles and symbolizes them
# based on layer files in:
#&amp;nbsp;&amp;nbsp; N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles

import arcpy, glob, os

arcpy.env.Workspace = r"N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles"
arcpy.env.OverwriteOutput = True

mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

layer_file_list = glob.glob(str(arcpy.env.Workspace) + "\*.lyr")

arcpy.AddMessage("Layer File List: ")
for layer in layer_file_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(layer)
print layer_file_list
arcpy.AddMessage("-------")

TOC_shp_list = arcpy.mapping.ListLayers(mxd, "", dataFrame)

arcpy.AddMessage("Table of Contents Shapefile list: ")
for shp in TOC_shp_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(shp)
print TOC_shp_list
arcpy.AddMessage("-------")

for shp in TOC_shp_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; shp_ext = shp[6:]
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(shp_ext)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print shp_ext
&amp;nbsp;&amp;nbsp;&amp;nbsp; del shp, shp_ext

arcpy.RefreshTOC()
arcpy.RefreshActiveView()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run this script I get the same error as before.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Everything up to the first for loop runs OK.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:49:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-object-is-unsubscriptable/m-p/698035#M54107</guid>
      <dc:creator>ZackBartlett</dc:creator>
      <dc:date>2021-12-12T16:49:07Z</dc:date>
    </item>
    <item>
      <title>Re: 'Layer' object is unsubscriptable</title>
      <link>https://community.esri.com/t5/python-questions/layer-object-is-unsubscriptable/m-p/698036#M54108</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;hey Zack,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;didn´t look at your code in detail, but my guess is you´re trying to access the layer object, not it´s name (which you need to compare the "subsections").&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the help page has a nice overview and some code samples as well:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 20 Feb 2012 14:12:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-object-is-unsubscriptable/m-p/698036#M54108</guid>
      <dc:creator>RaphaelR</dc:creator>
      <dc:date>2012-02-20T14:12:42Z</dc:date>
    </item>
    <item>
      <title>Re: 'Layer' object is unsubscriptable</title>
      <link>https://community.esri.com/t5/python-questions/layer-object-is-unsubscriptable/m-p/698037#M54109</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I suspect the problem is occuring with your "for shp in TOC_shp_list" block.&amp;nbsp; Your second line is stripping away characters from a layer object, not a string.&amp;nbsp; You could change it to something like &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;shp_ext = shp.name[6:]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jeff&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 20 Feb 2012 14:39:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-object-is-unsubscriptable/m-p/698037#M54109</guid>
      <dc:creator>JeffBarrette</dc:creator>
      <dc:date>2012-02-20T14:39:54Z</dc:date>
    </item>
    <item>
      <title>Re: 'Layer' object is unsubscriptable</title>
      <link>https://community.esri.com/t5/python-questions/layer-object-is-unsubscriptable/m-p/698038#M54110</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Jeffrey! It worked perfectly.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I suspected it must be some issue of treating a list item as a string, I just didn't know any method to convert it to a string.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Feb 2012 12:11:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-object-is-unsubscriptable/m-p/698038#M54110</guid>
      <dc:creator>ZackBartlett</dc:creator>
      <dc:date>2012-02-21T12:11:04Z</dc:date>
    </item>
  </channel>
</rss>

