<?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 Parse Invalid Characters Arcpy in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689622#M53423</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm back at it again. I've written a script to Iterate through an MXD and export all features within 50 miles of my state to a feature class. There are over 550 FCs to process and it keeps failing due to invalid characters. Does arcpy offer a way to parse out invalid characters when naming a conversion output?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Below is the code i'm referencing&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import glob
import os
files = glob.glob(r'C:\Users\JOC-001\Documents\GIS\HSIP\IL_Infrastructure2015\*')
for f in files:
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.remove(f)

mxd = arcpy.mapping.MapDocument(r'C:\Users\JOC-001\Documents\GIS\HSIP\Infrastructure\HSIP_Gold_2015_Infrastructure.mxd')&amp;nbsp; 
&amp;nbsp; 
layers = arcpy.mapping.ListLayers(mxd)&amp;nbsp; 
&amp;nbsp; 
for lyr in layers:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.isGroupLayer:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyr
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(lyr, "WITHIN_A_DISTANCE", r"C:\Users\JOC-001\Documents\ArcGIS\Default.gdb\Illinois", "50 Miles", "NEW_SELECTION")&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.FeatureClassToFeatureClass_conversion(lyr, r'C:\Users\JOC-001\Documents\GIS\HSIP\IL_Infrastructure2015', str(lyr.name))&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 05:01:22 GMT</pubDate>
    <dc:creator>RusselKlueg</dc:creator>
    <dc:date>2021-12-12T05:01:22Z</dc:date>
    <item>
      <title>Parse Invalid Characters Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689622#M53423</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm back at it again. I've written a script to Iterate through an MXD and export all features within 50 miles of my state to a feature class. There are over 550 FCs to process and it keeps failing due to invalid characters. Does arcpy offer a way to parse out invalid characters when naming a conversion output?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Below is the code i'm referencing&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import glob
import os
files = glob.glob(r'C:\Users\JOC-001\Documents\GIS\HSIP\IL_Infrastructure2015\*')
for f in files:
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.remove(f)

mxd = arcpy.mapping.MapDocument(r'C:\Users\JOC-001\Documents\GIS\HSIP\Infrastructure\HSIP_Gold_2015_Infrastructure.mxd')&amp;nbsp; 
&amp;nbsp; 
layers = arcpy.mapping.ListLayers(mxd)&amp;nbsp; 
&amp;nbsp; 
for lyr in layers:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.isGroupLayer:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyr
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(lyr, "WITHIN_A_DISTANCE", r"C:\Users\JOC-001\Documents\ArcGIS\Default.gdb\Illinois", "50 Miles", "NEW_SELECTION")&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.FeatureClassToFeatureClass_conversion(lyr, r'C:\Users\JOC-001\Documents\GIS\HSIP\IL_Infrastructure2015', str(lyr.name))&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:01:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689622#M53423</guid>
      <dc:creator>RusselKlueg</dc:creator>
      <dc:date>2021-12-12T05:01:22Z</dc:date>
    </item>
    <item>
      <title>Re: Parse Invalid Characters Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689623#M53424</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Could you post the entire error message? What is an example of an invalid character? Is it always the same character that you could simply replace or ignore?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You might try &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//001700000035000000"&gt;Copy Features&lt;/A&gt; rather than Feature Class to Feature Class, as well.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Jun 2015 16:16:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689623#M53424</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2015-06-24T16:16:28Z</dc:date>
    </item>
    <item>
      <title>Re: Parse Invalid Characters Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689624#M53425</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I actually found a pretty nice method to handle converting a string to Alpha Numeric characters &lt;/P&gt;&lt;P&gt;''.join(ch for ch in str(lyr) if ch.isalnum())&lt;/P&gt;&lt;P&gt;as for the copy features, thats a much better idea than FC2FC. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Jun 2015 17:02:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689624#M53425</guid>
      <dc:creator>RusselKlueg</dc:creator>
      <dc:date>2015-06-24T17:02:57Z</dc:date>
    </item>
    <item>
      <title>Re: Parse Invalid Characters Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689625#M53426</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Since you didn't give a specific example, I am guessing you are running into layer names that contain Windows file system reserve characters, e.g., a colon, double quote, etc....&amp;nbsp; Feature Class to Feature Class fails because the underlying call to create the new feature class fails at the OS level.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you are willing to limit output names to only alphanumeric ASCII characters, you could also try the sub method in the regular expression module.&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14351840441954077 jive_text_macro" data-renderedposition="92_8_912_16" jivemacro_uid="_14351840441954077"&gt;&lt;P&gt;re.sub('[^A-Za-z0-9]+', '', lyr.name)&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Jun 2015 22:14:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689625#M53426</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-06-24T22:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: Parse Invalid Characters Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689626#M53427</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That is a nice method that &lt;A href="https://community.esri.com/migrated-users/3420"&gt;Joshua Bixby&lt;/A&gt; shows to obtain a valid file name, although in special cases it may produce problems, but I like it (clean and short code). Probably going into to much detail, but there is a slugify project at GitHub that goes more into detail (also for URLs):&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/un33k/python-slugify/blob/master/slugify/slugify.py" title="https://github.com/un33k/python-slugify/blob/master/slugify/slugify.py"&gt;python-slugify/slugify.py at master · un33k/python-slugify · GitHub&lt;/A&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and some discussions here:&lt;/P&gt;&lt;P&gt;&lt;A href="http://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename-in-python" title="http://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename-in-python"&gt;Turn a string into a valid filename in Python - Stack Overflow&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;A href="http://stackoverflow.com/questions/7406102/create-sane-safe-filename-from-any-unsafe-string" title="http://stackoverflow.com/questions/7406102/create-sane-safe-filename-from-any-unsafe-string"&gt;python - Create (sane/safe) filename from any (unsafe) string - Stack Overflow&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;A href="http://stackoverflow.com/questions/8686880/validate-a-filename-in-python" title="http://stackoverflow.com/questions/8686880/validate-a-filename-in-python"&gt;Validate a filename in python - Stack Overflow&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 25 Jun 2015 01:32:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689626#M53427</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-06-25T01:32:12Z</dc:date>
    </item>
    <item>
      <title>Re: Parse Invalid Characters Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689627#M53428</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;arcpy.ValidateTableName does a good job of removing problematic characters as well.&lt;/P&gt;&lt;P&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//002z00000020000000" title="http://resources.arcgis.com/en/help/main/10.1/index.html#//002z00000020000000"&gt;ArcGIS Help 10.1&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 27 Jun 2015 00:00:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689627#M53428</guid>
      <dc:creator>GrantHerbert</dc:creator>
      <dc:date>2015-06-27T00:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: Parse Invalid Characters Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689628#M53429</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm still fairly new to python and I'm not sure how that method works. I would say i know just enough to casue myself a lot of problems! &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/laugh.png" /&gt; Would you mind giving a quick explanation? If not i'll dive into the python help pages! I did get my code working though. I'll post it below if you want to see it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import glob
import os


#glob is used to use the wildcard option for anything under this file&amp;nbsp; to allow for removal of all files
files = glob.glob(r'C:\Users\JOC-001\Documents\GIS\HSIP\IL_Infrastructure2015\*')
for f in files:
&amp;nbsp;&amp;nbsp;&amp;nbsp; os.remove(f)
#Defines 'mxd' as the Map Document to be referenced
mxd = arcpy.mapping.MapDocument(r'C:\Users\JOC-001\Documents\GIS\HSIP\Infrastructure\HSIP_Gold_2015_Infrastructure.mxd') 
#Defines the layers to be used as everything under Layers dataframe in the table of contents for the mxd
layers = arcpy.mapping.ListLayers(mxd) 
#Iterates over each layer in the list of 'layers'
for lyr in layers:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #checks to see if any file is a Group layer rather than a single layer
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.isGroupLayer:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
&amp;nbsp;&amp;nbsp;&amp;nbsp; #Once layers pass the group layer check this initiates the geoprocessing.
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr2 =&amp;nbsp; ''.join(ch for ch in str(lyr) if ch.isalnum())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyr2
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(lyr, "WITHIN_A_DISTANCE", r"C:\Users\JOC-001\Documents\ArcGIS\Default.gdb\Illinois", "50 Miles", "NEW_SELECTION") 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.FeatureClassToFeatureClass_conversion(lyr, r'C:\Users\JOC-001\Documents\GIS\HSIP\IL_Infrastructure2015', lyr2)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:01:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689628#M53429</guid>
      <dc:creator>RusselKlueg</dc:creator>
      <dc:date>2021-12-12T05:01:25Z</dc:date>
    </item>
    <item>
      <title>Re: Parse Invalid Characters Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689629#M53430</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you plan on continuing with any programming/scripting, I strongly encourage you to learn more about regular expressions.&amp;nbsp; They are an extremely powerful form of pattern matching, and regular expressions are either built-in or available through libraries in many programming languages, including Python.&amp;nbsp; : )&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regarding the specific code snippet I gave, let's look at the Python syntax first:&amp;nbsp; &lt;SPAN style="font-family: courier new,courier;"&gt;re.sub(pattern, repl, string, count=0, flags=0)&lt;/SPAN&gt;.&amp;nbsp; We are not using count or flags, so we need three things:&amp;nbsp; 1)&amp;nbsp; a regular expression pattern, 2) a replacement string, and 3) a string to apply the regular expression.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Looking next at the regular expression:&amp;nbsp; &lt;SPAN style="font-family: courier new,courier;"&gt;[^A-Za-z0-9]+&lt;/SPAN&gt;.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;A bracket expression, &lt;SPAN style="font-family: courier new,courier;"&gt;[ ], &lt;/SPAN&gt;matches a single character that is within the brackets, sort of an implicit or operator.&lt;/LI&gt;&lt;LI&gt;A bracket expression that starts with a caret, &lt;SPAN style="font-family: courier new,courier;"&gt;[^ ]&lt;/SPAN&gt;, matches a single character that is not within the brackets.&lt;/LI&gt;&lt;LI&gt;Bracket expressions allow for ranges, e.g., &lt;SPAN style="font-family: courier new,courier;"&gt;A-Z&lt;/SPAN&gt; means any uppercase character from A through Z.&lt;/LI&gt;&lt;LI&gt;The plus sign, &lt;SPAN style="font-family: courier new,courier;"&gt;+&lt;/SPAN&gt;, matches the preceding occurrence/element one or more times.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So, the bracket expression I provided, &lt;SPAN style="font-family: courier new,courier;"&gt;[^A-Za-z0-9]&lt;/SPAN&gt;, matches any character that &lt;SPAN style="text-decoration: underline;"&gt;isn't&lt;/SPAN&gt; A through Z, a through z, or 0 through 9.&amp;nbsp; The plus sign is used to match one or more occurrences.&amp;nbsp; Finally, the matched characters are replaced with an empty string to remove them from the original string.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The regular expression I provided is rather simplistic in that valid non-alphanumeric characters will also be removed, e.g., hyphens and underscores.&amp;nbsp; If one knew his/her code was only going to be run on Windows, an assumption I wouldn't make myself, a regular expression could be make to only remove Windows file system reserved characters.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As &lt;A href="https://community.esri.com/migrated-users/17443"&gt;Grant Herbert&lt;/A&gt;​ pointed out, you could also use ArcPy's &lt;A href="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-functions/validatetablename.htm"&gt;ValidateTableName&lt;/A&gt;​ method.&amp;nbsp; Using that approach, though, the invalid characters are replaced with an underscore and there is no way to change the replacement character.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 27 Jun 2015 14:38:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689629#M53430</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-06-27T14:38:53Z</dc:date>
    </item>
    <item>
      <title>Re: Parse Invalid Characters Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689630#M53431</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Oh sweet that's pretty nice and super easy too. I was lost at the bracket. I caught on that you were using ranges but was still kind of confused. This is pretty sweet! I think i'll update my code to use this! Thanks:D&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 27 Jun 2015 15:40:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689630#M53431</guid>
      <dc:creator>RusselKlueg</dc:creator>
      <dc:date>2015-06-27T15:40:04Z</dc:date>
    </item>
    <item>
      <title>Re: Parse Invalid Characters Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689631#M53432</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi &lt;A href="https://community.esri.com/migrated-users/42345" target="_blank"&gt;Russel Klueg&lt;/A&gt; , I do have some considerations. Have a look at the TOC of a dummy document that I created:&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/114274_pastedImage_0.png" style="max-width: 1200px; max-height: 900px;" /&gt;&lt;/P&gt;&lt;P&gt;It contains nested grouplayers, some featurelayers a raster layer, special characters.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I run the following code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import arcpy

fldr = r"C:\Forum"

mxd = arcpy.mapping.MapDocument(r"C:\Forum\ValidName\test.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, '*', df):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if not lyr.isGroupLayer:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "&amp;nbsp; - using str(lyr)&amp;nbsp; : ", ''.join(ch for ch in str(lyr) if ch.isalnum())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "... produced an error"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "&amp;nbsp; - using lyr.name&amp;nbsp; : ", ''.join(ch for ch in lyr.name if ch.isalnum())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "&amp;nbsp; - using re&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : ", re.sub('[^A-Za-z0-9]+', '', lyr.name)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "&amp;nbsp; - using validate TN: ", arcpy.ValidateTableName(lyr.name, fldr)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It will output this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp; - using str(lyr)&amp;nbsp; :&amp;nbsp; ... produced an error
&amp;nbsp; - using lyr.name&amp;nbsp; :&amp;nbsp; wielokąty
&amp;nbsp; - using re&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; :&amp;nbsp; wielokty
&amp;nbsp; - using validate TN:&amp;nbsp; wielokąty
&amp;nbsp; - using str(lyr)&amp;nbsp; :&amp;nbsp; ... produced an error
&amp;nbsp; - using lyr.name&amp;nbsp; :&amp;nbsp; інфармацыя
&amp;nbsp; - using re&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : 
&amp;nbsp; - using validate TN:&amp;nbsp; інфармацыя
&amp;nbsp; - using str(lyr)&amp;nbsp; :&amp;nbsp; ... produced an error
&amp;nbsp; - using lyr.name&amp;nbsp; :&amp;nbsp; Coöperativerésumé
&amp;nbsp; - using re&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; :&amp;nbsp; Coperativersum
&amp;nbsp; - using validate TN:&amp;nbsp; Coöperative_résumé
&amp;nbsp; - using str(lyr)&amp;nbsp; :&amp;nbsp; ContinentACountryEThisisaraster
&amp;nbsp; - using lyr.name&amp;nbsp; :&amp;nbsp; Thisisaraster
&amp;nbsp; - using re&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; :&amp;nbsp; Thisisaraster
&amp;nbsp; - using validate TN:&amp;nbsp; This_is_a_raster
&amp;nbsp; - using str(lyr)&amp;nbsp; :&amp;nbsp; ContinentACountryEProvinceFLimits
&amp;nbsp; - using lyr.name&amp;nbsp; :&amp;nbsp; Limits
&amp;nbsp; - using re&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; :&amp;nbsp; Limits
&amp;nbsp; - using validate TN:&amp;nbsp; Limits
&amp;nbsp; - using str(lyr)&amp;nbsp; :&amp;nbsp; ContinentACountryEProvinceFMinicipalityGConstructions
&amp;nbsp; - using lyr.name&amp;nbsp; :&amp;nbsp; Constructions
&amp;nbsp; - using re&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; :&amp;nbsp; Constructions
&amp;nbsp; - using validate TN:&amp;nbsp; Constructions
&amp;nbsp; - using str(lyr)&amp;nbsp; :&amp;nbsp; ContinentACountryBProvinceCMinicipalityDConstructions
&amp;nbsp; - using lyr.name&amp;nbsp; :&amp;nbsp; Constructions
&amp;nbsp; - using re&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; :&amp;nbsp; Constructions
&amp;nbsp; - using validate TN:&amp;nbsp; Constructions&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What I want to explain with this (that only shows part of the problems one can encounter) is:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;using str(lyr) instead of lyr.name will include the name of the (nested) grouplayer(s) it resides in&lt;/LI&gt;&lt;LI&gt;using str(lyr) will produce an error when you have special characters (diacritical marks)&lt;/LI&gt;&lt;LI&gt;a name is valid although it contains special characters. The re expression though can result in eliminating all characters&lt;/LI&gt;&lt;LI&gt;When you have a layer name that is used twice in your TOC, you will have to handle what should be done to create a unique file name&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;These are just some considerations. Maybe none of them apply for your specific case, you know your data and you are the only one to run the code, then this will not be a problem. However, when create a tool that others will run, then you should account for all these cases.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kind regards, Xander&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:01:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689631#M53432</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-12T05:01:28Z</dc:date>
    </item>
    <item>
      <title>Re: Parse Invalid Characters Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689632#M53433</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/3100"&gt;Xander Bakker&lt;/A&gt;​, The way my code is written, that is part of the reason I chose to use str(lyr) i'm not exactly a strong scripter and using the group layer name to create unique files was easiest for me . I'm self taught and only over the last couple months. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've still got a lot to learn. Especially in arcpy. I really appreciate your advice:D&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 27 Jun 2015 19:27:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689632#M53433</guid>
      <dc:creator>RusselKlueg</dc:creator>
      <dc:date>2015-06-27T19:27:34Z</dc:date>
    </item>
    <item>
      <title>Re: Parse Invalid Characters Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689633#M53434</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have also used lyr.longName, as this has a slash between the group name and the layer name. While this isn't a safe character for output, it is a handy way to separate the names sometimes.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;for example, I have a script which creates layer files from an mxd, and to identify if a layer is in a group (rather than being a group layer) I use the following and change the output name accordingly (as I am outputting all layers - stand alone, group layers and sub layers and there is a high likelihood of duplicate names):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.longName.find("\\") &amp;gt; -1:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # change the name&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out_lyr = lyr.longName.replace("\\","_")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # or, functionally the same&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out_lyr = arcpy.validateTablename(lyr.longName)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;GroupLayer\Layer becomes GroupLayer_Layer.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For generating unique output names I do an Exists() check, and increment a counter at the end of the name as necessary.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 29 Jun 2015 17:45:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/689633#M53434</guid>
      <dc:creator>GrantHerbert</dc:creator>
      <dc:date>2015-06-29T17:45:33Z</dc:date>
    </item>
    <item>
      <title>Re: Parse Invalid Characters Arcpy</title>
      <link>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/1088688#M62079</link>
      <description>&lt;P&gt;Six years later, the solution worked like a charm.&amp;nbsp;&lt;/P&gt;&lt;P&gt;As a bit of background, I was creating individual FC's from a larger FC based on an attribute.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MGAL_0-1628806604683.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/20779i8E01E49CBE842A82/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MGAL_0-1628806604683.png" alt="MGAL_0-1628806604683.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The MB failed due to spaces &amp;amp; dashes in the attribute name. As a workaround, I created a new field and calculated using the following expression.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import re
def strip_invalid(plant):
    x = re.sub('[^A-Za-z0-9]+', '', plant)
    return x&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Thanks,&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/1371"&gt;@JoshuaBixby&lt;/a&gt;&amp;nbsp;!!&lt;/P&gt;</description>
      <pubDate>Thu, 12 Aug 2021 22:19:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parse-invalid-characters-arcpy/m-p/1088688#M62079</guid>
      <dc:creator>MGAL</dc:creator>
      <dc:date>2021-08-12T22:19:49Z</dc:date>
    </item>
  </channel>
</rss>

