<?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 Replacing &amp;quot;_&amp;quot; with &amp;quot; &amp;quot; for classLabels using Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/replacing-quot-quot-with-quot-quot-for-classlabels/m-p/220637#M17055</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Head banging against a wall...&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have figured how to do this for layer name. The Help Docs say it is possible to change classLabel but don't show any examples and I can't find anyone who has successfully done this through Python. I am sure the answer lies in arcpy.mapping and UniqueValuesSymbology but cannot figure it. I have 10-15 different layers, all using unique_values for symbology, consisting of about 15-20 different classes in each. Each class value and therefore label uses an "_" instead of a space as is common practice for GIS data. For legend and TOC purposes, I am trying to figure a script that can quickly manipulate and change the classLabels in Layer Properties.&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas would be wonderful...thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 07 Jan 2015 22:57:28 GMT</pubDate>
    <dc:creator>DerekNelson</dc:creator>
    <dc:date>2015-01-07T22:57:28Z</dc:date>
    <item>
      <title>Replacing "_" with " " for classLabels using Python</title>
      <link>https://community.esri.com/t5/python-questions/replacing-quot-quot-with-quot-quot-for-classlabels/m-p/220637#M17055</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Head banging against a wall...&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have figured how to do this for layer name. The Help Docs say it is possible to change classLabel but don't show any examples and I can't find anyone who has successfully done this through Python. I am sure the answer lies in arcpy.mapping and UniqueValuesSymbology but cannot figure it. I have 10-15 different layers, all using unique_values for symbology, consisting of about 15-20 different classes in each. Each class value and therefore label uses an "_" instead of a space as is common practice for GIS data. For legend and TOC purposes, I am trying to figure a script that can quickly manipulate and change the classLabels in Layer Properties.&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas would be wonderful...thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 Jan 2015 22:57:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacing-quot-quot-with-quot-quot-for-classlabels/m-p/220637#M17055</guid>
      <dc:creator>DerekNelson</dc:creator>
      <dc:date>2015-01-07T22:57:28Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing "_" with " " for classLabels using Python</title>
      <link>https://community.esri.com/t5/python-questions/replacing-quot-quot-with-quot-quot-for-classlabels/m-p/220638#M17056</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The following code from the interactive Python window in ArcMap should help get you going:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.symbologyType in ["GRADUATED_COLORS", "GRADUATED_SYMBOLS"]:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyr.symbology.classBreakLabels
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif lyr.symbologyType in ["UNIQUE_VALUES"]:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyr.symbology.classLabels&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The classBreakLabels and classLabels properties are read-writeable and store lists.&amp;nbsp; After you get the list, update the contents, and set the properties again; you will need to run &lt;STRONG style="font-family: courier new,courier;"&gt;arcpy.RefreshActiveView()&lt;/STRONG&gt; to see the changes.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:45:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacing-quot-quot-with-quot-quot-for-classlabels/m-p/220638#M17056</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-11T10:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing "_" with " " for classLabels using Python</title>
      <link>https://community.esri.com/t5/python-questions/replacing-quot-quot-with-quot-quot-for-classlabels/m-p/220639#M17057</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Joshua,&lt;/P&gt;&lt;P&gt;So it did grab and create the list from the available classLabels. How do I then go about replacing the underscores with spaces though. You say "update the contents, and set the properties again", what does that mean? I am an admitted Python amateur and quite frankly still lost. This is where I'm at (couldn't figure out how to add script like you did to the discussion):&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Capture.JPG" class="jive-image image-1" src="https://community.esri.com/legacyfs/online/48495_Capture.JPG" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;Again...thanks for any advice!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Jan 2015 17:55:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacing-quot-quot-with-quot-quot-for-classlabels/m-p/220639#M17057</guid>
      <dc:creator>DerekNelson</dc:creator>
      <dc:date>2015-01-08T17:55:11Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing "_" with " " for classLabels using Python</title>
      <link>https://community.esri.com/t5/python-questions/replacing-quot-quot-with-quot-quot-for-classlabels/m-p/220640#M17058</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;For inserting scripts, i.e., syntax highlighting, you need to use the advanced editor that is accessed from a link in the upper-right corner of the reply window/frame.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Try the following:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.symbologyType in ["GRADUATED_COLORS", "GRADUATED_SYMBOLS"]:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; labels = lyr.symbology.classBreakLabels
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i, label in enumerate(labels):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; labels&lt;I&gt; = label.replace("_", " ")&lt;/I&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.symbology.classBreakLabels = labels
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif lyr.symbologyType in ["UNIQUE_VALUES"]:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; labels = lyr.symbology.classLabels
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i, label in enumerate(labels):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; labels&lt;I&gt; = label.replace("_", " ")&lt;/I&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.symbology.classLabels = labels&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Since the class labels are properties and return a list, you need to "get" the lists, which is what I am doing in lines 03 and 08.&lt;/LI&gt;&lt;LI&gt;Since the labels are stored in a list, you need to iterate over the list and do the text replace/substitution on each element of the list.&amp;nbsp; Lines 04-05 and 09-10.&lt;UL&gt;&lt;LI&gt;There are several ways to handle this part.&amp;nbsp; One could create a new list, using a for loop or list comprehension&amp;nbsp; I chose to edit the existing list in place, using a for loop and an enumerate object.&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;LI&gt;After the label updates are done, you need to "set" the properties to the new or updated lists, which is what I am doing in lines 06 and 11.&lt;/LI&gt;&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:45:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacing-quot-quot-with-quot-quot-for-classlabels/m-p/220640#M17058</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-11T10:45:56Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing "_" with " " for classLabels using Python</title>
      <link>https://community.esri.com/t5/python-questions/replacing-quot-quot-with-quot-quot-for-classlabels/m-p/220641#M17059</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Joshua,&lt;/P&gt;&lt;P&gt;You are the MAN! Had to substitute some single quotes for the doubles you had in the replace areas, added it to my script for renaming the layers with a "arcpy.RefreshTOC()" and presto! Thanks so much for the help!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Jan 2015 20:58:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacing-quot-quot-with-quot-quot-for-classlabels/m-p/220641#M17059</guid>
      <dc:creator>DerekNelson</dc:creator>
      <dc:date>2015-01-08T20:58:30Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing "_" with " " for classLabels using Python</title>
      <link>https://community.esri.com/t5/python-questions/replacing-quot-quot-with-quot-quot-for-classlabels/m-p/220642#M17060</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your welcome.&amp;nbsp; Keeping plugging away at learning Python, I know I have really enjoyed learning and applying it, there is so much you can do with it.&amp;nbsp; Cheers....&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Jan 2015 21:04:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replacing-quot-quot-with-quot-quot-for-classlabels/m-p/220642#M17060</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-01-08T21:04:34Z</dc:date>
    </item>
  </channel>
</rss>

