<?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 Re: Remove prefixes in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367462#M69562</link>
    <description>&lt;P&gt;lstrip is treating &lt;STRONG&gt;"E "&lt;/STRONG&gt; as being &lt;STRONG&gt;["E", " "]&lt;/STRONG&gt; and stripping all instances of those two characters from the left of the string, until it reaches a character other than those two.&lt;/P&gt;&lt;P&gt;Try this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;if (row[0].startswith('E ')
    or row[0].startswith('W ')
    or row[0].startswith('N ')
    or row[0].startswith('S ')
   ):
    # print('Deleting Cardinal')
    row[0] = row[0][2:]
    cursor.updateRow(row)  # N.B. if you indent this INSIDE the if statement,
                           # then it won't update any value it doesn't have
                           # to which can be handy if you track edits, since
                           # you didn't technically have anything to change
                           # on the ones without a cardinal direction

### The ones below this are optional, if you have full-word cardinals on your prefixes.
elif (row[0].startswith('East ')
      or row[0].startswith('West ')
     ):
    # print('Deleting Cardinal')
    row[0] = row[0][4:]
elif (row[0].startswith('North ')
      or row[0].startswith('South ')
     ):
    # print('Deleting Cardinal')
    row[0] = row[0][5:]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since you're always looking for a 2-character* substring at the beginning, you know if you found it that you only need the remainder of the substring after it.&amp;nbsp; [2:] tells it to go to index 2 (the character&amp;nbsp;&lt;EM&gt;after&lt;/EM&gt; your space), and then just give you the rest of the string from there.&lt;/P&gt;&lt;P&gt;* The elif statements at line 15 &amp;amp; line 20 look for 4- and 5-character substrings, so the indices at lines 19 &amp;amp; 24 also change—assuming you need them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EDIT: Unrelated sidenote, it is surprisingly a pain to edit code block statements for typos on this forum.&amp;nbsp; Apologies to anyone who read it before I caught them.&lt;/P&gt;</description>
    <pubDate>Mon, 08 Jan 2024 17:13:25 GMT</pubDate>
    <dc:creator>MErikReedAugusta</dc:creator>
    <dc:date>2024-01-08T17:13:25Z</dc:date>
    <item>
      <title>Remove prefixes</title>
      <link>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367455#M69561</link>
      <description>&lt;P&gt;I am trying to remove prefixes from a table. I have the following code but it removes more than just street prefix. I my code I have E "space", "E ". I need to be able to strip the prefixes so I can sort, then delete duplicates. How can I remove just the prefixes?&lt;/P&gt;&lt;LI-CODE lang="c"&gt;with arcpy.da.UpdateCursor(table1,'STREET') as cursor:  
    for row in cursor:  
        #print row[0]  
        if row[0].startswith("S "):  
            #print "Deleting S"  
            row [0] = row[0].lstrip('S ')
        elif row[0].startswith("E "):  
            #print "Deleting E"  
            row [0] = row[0].lstrip('E ')
        elif row[0].startswith("W "):  
            #print "Deleting W"  
            row [0] = row[0].lstrip('W ')
        elif row[0].startswith("N "):  
            #print "Deleting N"  
            row [0] = row[0].lstrip('N ')
        cursor.updateRow(row) 
del cursor&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;After code runs i get.&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="50%"&gt;&lt;STRONG&gt;Before Code&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD width="50%"&gt;&lt;STRONG&gt;After code&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%"&gt;E Explorer&lt;/TD&gt;&lt;TD width="50%"&gt;xplorer&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%"&gt;E Expedition&lt;/TD&gt;&lt;TD width="50%"&gt;xpedtion&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%"&gt;E Exective&lt;/TD&gt;&lt;TD width="50%"&gt;xecutive&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%"&gt;E Exchange&lt;/TD&gt;&lt;TD width="50%"&gt;xchange&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 08 Jan 2024 16:57:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367455#M69561</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2024-01-08T16:57:53Z</dc:date>
    </item>
    <item>
      <title>Re: Remove prefixes</title>
      <link>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367462#M69562</link>
      <description>&lt;P&gt;lstrip is treating &lt;STRONG&gt;"E "&lt;/STRONG&gt; as being &lt;STRONG&gt;["E", " "]&lt;/STRONG&gt; and stripping all instances of those two characters from the left of the string, until it reaches a character other than those two.&lt;/P&gt;&lt;P&gt;Try this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;if (row[0].startswith('E ')
    or row[0].startswith('W ')
    or row[0].startswith('N ')
    or row[0].startswith('S ')
   ):
    # print('Deleting Cardinal')
    row[0] = row[0][2:]
    cursor.updateRow(row)  # N.B. if you indent this INSIDE the if statement,
                           # then it won't update any value it doesn't have
                           # to which can be handy if you track edits, since
                           # you didn't technically have anything to change
                           # on the ones without a cardinal direction

### The ones below this are optional, if you have full-word cardinals on your prefixes.
elif (row[0].startswith('East ')
      or row[0].startswith('West ')
     ):
    # print('Deleting Cardinal')
    row[0] = row[0][4:]
elif (row[0].startswith('North ')
      or row[0].startswith('South ')
     ):
    # print('Deleting Cardinal')
    row[0] = row[0][5:]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since you're always looking for a 2-character* substring at the beginning, you know if you found it that you only need the remainder of the substring after it.&amp;nbsp; [2:] tells it to go to index 2 (the character&amp;nbsp;&lt;EM&gt;after&lt;/EM&gt; your space), and then just give you the rest of the string from there.&lt;/P&gt;&lt;P&gt;* The elif statements at line 15 &amp;amp; line 20 look for 4- and 5-character substrings, so the indices at lines 19 &amp;amp; 24 also change—assuming you need them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EDIT: Unrelated sidenote, it is surprisingly a pain to edit code block statements for typos on this forum.&amp;nbsp; Apologies to anyone who read it before I caught them.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2024 17:13:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367462#M69562</guid>
      <dc:creator>MErikReedAugusta</dc:creator>
      <dc:date>2024-01-08T17:13:25Z</dc:date>
    </item>
    <item>
      <title>Re: Remove prefixes</title>
      <link>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367485#M69564</link>
      <description>&lt;P&gt;I'm too lazy to fix the typos in the codeblock right now, but it just occurred to me that the indices for the full-word ones are wrong, because I forgot to count the space.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Line 19:&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    row[0] = row[0][5:]&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;Line 24:&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    row[0] = row[0][6:]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2024 17:32:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367485#M69564</guid>
      <dc:creator>MErikReedAugusta</dc:creator>
      <dc:date>2024-01-08T17:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: Remove prefixes</title>
      <link>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367495#M69565</link>
      <description>&lt;P&gt;Thanks for the reply. I was coming back to my post to add code that worked for me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;try:
    # Update the street names in the table
    with arcpy.da.UpdateCursor(table, "STREET") as cursor:
        for row in cursor:
            street_name = row[0]

            # List of common street prefixes to be removed
            prefixes = ['N ', 'S ', 'E ', 'W ', 'North ', 'South ', 'East ', 'West ']

            # Remove prefixes from the street name
            for prefix in prefixes:
                if street_name.startswith(prefix):
                    row[0] = street_name[len(prefix):].strip()
                    cursor.updateRow(row)
                    break&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code did work tho. Again thanks for the reply!&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2024 17:44:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367495#M69565</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2024-01-08T17:44:23Z</dc:date>
    </item>
    <item>
      <title>Re: Remove prefixes</title>
      <link>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367582#M69569</link>
      <description>&lt;P&gt;Efficient!&amp;nbsp; I like it.&lt;/P&gt;&lt;P&gt;Just for fun, I tried to see if I could condense this further.&amp;nbsp; Here it is!&lt;/P&gt;&lt;LI-CODE lang="python"&gt;prefixes = ['N ', 'S ', 'E ', 'W ', 'North ', 'South ', 'East ', 'West ']
with arcpy.da.UpdateCursor(table, 'STREET') as cursor:
    for row in cursor:
        row[0] = min([row[0][len(prefix):] if row[0].startswith(prefix) else row[0] for prefix in prefixes], key=len)
        cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First, a list comprehension creates a list of your street name with every prefix attempted to be removed from it.&amp;nbsp; Then, it turns out the &lt;STRONG&gt;min()&lt;/STRONG&gt; function accepts a key argument.&amp;nbsp; If you give it the built-in&amp;nbsp;&lt;STRONG&gt;len&lt;/STRONG&gt;, it gives you the shortest entry from that list back as a single item.&lt;/P&gt;&lt;P&gt;Since, by definition, every item in our generated list is either the original street name or that street name minus a prefix, the shortest possible will always* be the street name minus any applicable prefix.&lt;/P&gt;&lt;P&gt;*An important caveat here: "N South 5th Street" would return as "South 5th Street", which may or may not be desired.&amp;nbsp; But then, that's a problem of&amp;nbsp;&lt;EM&gt;all&lt;/EM&gt; of the code in this thread.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, just to be cheeky, here it is even &lt;EM&gt;more&lt;/EM&gt; condensed.&amp;nbsp; I saved a whole 3 lines!&amp;nbsp; But I haven't tested it.&amp;nbsp; And&amp;nbsp; while I think the logic and syntax are all sound, it's the height of absurdity, anyway.&amp;nbsp; Please don't do this to whoever has to read your code behind you.&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":face_with_tongue:"&gt;😛&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor(table, 'STREET') as cursor:
    [cursor.updateRow(row) for [min([row[0][len(prefix):] if row[0].startswith(prefix) else row[0] for prefix in ['N ', 'S ', 'E ', 'W ', 'North ', 'South ', 'East ', 'West ']], key=len)] in cursor]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2024 20:26:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367582#M69569</guid>
      <dc:creator>MErikReedAugusta</dc:creator>
      <dc:date>2024-01-08T20:26:33Z</dc:date>
    </item>
    <item>
      <title>Re: Remove prefixes</title>
      <link>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367670#M69573</link>
      <description>&lt;P&gt;As fun as code golf can be, list comprehensions were proposed and accepted "to create lists" (&lt;A href="https://peps.python.org/pep-0202/#the-proposed-solution" target="_blank"&gt;PEP 202 – List Comprehensions | peps.python.org&lt;/A&gt;).&amp;nbsp; I think many would argue that using a list comprehension to perform a mapping function isn't very Pythonic.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2024 00:15:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367670#M69573</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-01-09T00:15:17Z</dc:date>
    </item>
    <item>
      <title>Re: Remove prefixes</title>
      <link>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367690#M69575</link>
      <description>&lt;P&gt;Another approach would be to use regular expressions.&amp;nbsp; Although regular expressions might be a bit overkill for this specific situation, they are much more flexible to handle more complex situations:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor(table1,'STREET') as cursor:  
    for row in cursor:  
        row[0] = re.sub(r"^((?:N|S|E|W|North|South|East|West) )+", "", row[0])
        cursor.updateRow(row) 

del cursor&lt;/LI-CODE&gt;&lt;P&gt;The above will result in "N South 5th Street" becoming "5th Street".&amp;nbsp; If you don't want double prefixes to be removed, just take the "+" out of the regular expression pattern.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2024 04:07:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-prefixes/m-p/1367690#M69575</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-01-09T04:07:33Z</dc:date>
    </item>
  </channel>
</rss>

