<?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: Move text after comma in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011185#M59335</link>
    <description>&lt;P&gt;The Python questions must be light lately, everyone has pent up energy to write some code snippets.&lt;/P&gt;</description>
    <pubDate>Fri, 18 Dec 2020 22:58:30 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2020-12-18T22:58:30Z</dc:date>
    <item>
      <title>Move text after comma</title>
      <link>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011011#M59324</link>
      <description>&lt;P&gt;I have a table and in that table I have field Fld_1 and I would like to move the text after the comma to the front of the attribute and remove the comma. Not all attributes will have a comma.&lt;/P&gt;&lt;P&gt;I am able to split the filed attrubutes but am not sure how to work with the comma ','.&lt;/P&gt;&lt;P&gt;with arcpy.da.UpdateCursor(Table, ['Fld_1']) as cursor:&lt;BR /&gt;for row in cursor:&lt;BR /&gt;row[0] = " ".join(row[0].split()[:3])&lt;BR /&gt;cursor.updateRow(row)&lt;BR /&gt;del cursor&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example&lt;/P&gt;&lt;P&gt;Island Village, The --&amp;gt; The Island Village&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 18:06:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011011#M59324</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2020-12-18T18:06:17Z</dc:date>
    </item>
    <item>
      <title>Re: Move text after comma</title>
      <link>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011044#M59325</link>
      <description>&lt;P&gt;How fancy do you want to get??&lt;/P&gt;&lt;LI-CODE lang="python"&gt; def r(a, pad=" "):
    if "," in a:
        b = a.split(",")
        c = b[1].strip() + pad + b[0]
    else:
        return a 
    return c
    

r("first, last")
'last first'&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 18 Dec 2020 18:31:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011044#M59325</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2020-12-18T18:31:55Z</dc:date>
    </item>
    <item>
      <title>Re: Move text after comma</title>
      <link>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011047#M59326</link>
      <description>&lt;P&gt;I would use rsplit() to ensure you only split once on the last separator. You can use .reverse() to put the last value first, then join it all back together.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor(Table, ['Fld_1']) as cursor:
    for row in cursor:
        value_split = row[0].rsplit(", ", 1)
        value_split.reverse()
        row[0] = " ".join(value_split)
        cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;Also note: you don't need to &lt;FONT face="courier new,courier"&gt;del&lt;/FONT&gt; the cursor when using &lt;FONT face="courier new,courier"&gt;with&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 18:35:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011047#M59326</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2020-12-18T18:35:00Z</dc:date>
    </item>
    <item>
      <title>Re: Move text after comma</title>
      <link>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011063#M59327</link>
      <description>&lt;P&gt;Check for the presence of a comma.&amp;nbsp; Treat row[0] as a list, pop() off the last member of that list and glue the whole mess back together, replacing the comma....&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor(Table, ['Fld_1']) as cursor:
    for row in cursor:
        if ',' not in row[0]:
            pass
        elif ',' in row[0]:
            rowList = row[0].split(' ')
            first = rowList.pop()
            last = ' '.join(rowList)
            newRow = f'{first} {last}'.replace(',','')
            row[0] = newRow
            cursor.updateRow(row)
         
    
    &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 18:59:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011063#M59327</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-12-18T18:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: Move text after comma</title>
      <link>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011089#M59328</link>
      <description>&lt;P&gt;Just for the code golf fun of it:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; s = "Island Village, The"
&amp;gt;&amp;gt;&amp;gt; " ".join(reversed(list(map(str.strip, s.split(',')))))
'The Island Village'
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; s = "Peninsula Town"
&amp;gt;&amp;gt;&amp;gt; " ".join(reversed(list(map(str.strip, s.split(',')))))
'Peninsula Town'
&amp;gt;&amp;gt;&amp;gt;                                                   &lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 18 Dec 2020 19:40:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011089#M59328</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2020-12-18T19:40:59Z</dc:date>
    </item>
    <item>
      <title>Re: Move text after comma</title>
      <link>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011090#M59329</link>
      <description>&lt;P&gt;I love the contrast in our styles!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 19:42:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011090#M59329</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-12-18T19:42:53Z</dc:date>
    </item>
    <item>
      <title>Re: Move text after comma</title>
      <link>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011105#M59330</link>
      <description>&lt;P&gt;You guys...&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="golf_code.png" style="width: 564px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/2586i549C921C930D3846/image-size/large?v=v2&amp;amp;px=999" role="button" title="golf_code.png" alt="golf_code.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 20:01:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011105#M59330</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2020-12-18T20:01:46Z</dc:date>
    </item>
    <item>
      <title>Re: Move text after comma</title>
      <link>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011157#M59331</link>
      <description>&lt;P&gt;Can't believe I forgot about &lt;FONT face="courier new,courier"&gt;pop()&lt;/FONT&gt;!&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 21:36:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011157#M59331</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2020-12-18T21:36:01Z</dc:date>
    </item>
    <item>
      <title>Re: Move text after comma</title>
      <link>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011171#M59333</link>
      <description>&lt;P&gt;That's what's so fun about a post like this!&amp;nbsp; Several different approaches that all work. I've never ever used rsplit(). Joshua's nested map() is pretty cool too!&amp;nbsp; Just hope the OP isn't overwhelmed!&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 21:59:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011171#M59333</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-12-18T21:59:02Z</dc:date>
    </item>
    <item>
      <title>Re: Move text after comma</title>
      <link>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011172#M59334</link>
      <description>&lt;P&gt;Haha, yeah, we all jumped on this like a pack of hungry &lt;STRIKE&gt;wolves&lt;/STRIKE&gt; pythons.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 22:01:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011172#M59334</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2020-12-18T22:01:42Z</dc:date>
    </item>
    <item>
      <title>Re: Move text after comma</title>
      <link>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011185#M59335</link>
      <description>&lt;P&gt;The Python questions must be light lately, everyone has pent up energy to write some code snippets.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 22:58:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011185#M59335</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2020-12-18T22:58:30Z</dc:date>
    </item>
    <item>
      <title>Re: Move text after comma</title>
      <link>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011256#M59337</link>
      <description>&lt;LI-CODE lang="python"&gt;" ".join([i.strip() for i in a.split(",")][::-1])  # ---- forgot list comprehensions&lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 19 Dec 2020 22:18:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/move-text-after-comma/m-p/1011256#M59337</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2020-12-19T22:18:43Z</dc:date>
    </item>
  </channel>
</rss>

