<?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: Python - write new row based on comma-separated values in table column? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-write-new-row-based-on-comma-separated/m-p/204034#M15696</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Something like this should do it:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;fieldList = [ "ATTRB1", "ATTRB2", "ATTRB3", "ATTRB4" ]
with arcpy.da.InsertCursor(targetTable, fieldList) as insertCursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(sourceTable, fieldList) as ‍‍searchCursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in searchCursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for attrbPart in row[3].split(","):
&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;&amp;nbsp;&amp;nbsp; insertCursor.insertRow(( row[0], row[1], row[2], attrbPart ))&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would throw in some null testing, empty string testing, error handling, etc.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 10:07:41 GMT</pubDate>
    <dc:creator>JamesMacKay3</dc:creator>
    <dc:date>2021-12-11T10:07:41Z</dc:date>
    <item>
      <title>Python - write new row based on comma-separated values in table column?</title>
      <link>https://community.esri.com/t5/python-questions/python-write-new-row-based-on-comma-separated/m-p/204033#M15695</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hopefully a simple (and fun!) Python loop question.&lt;/P&gt;&lt;P&gt;We have a dataset that looks like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE style="border-width: 1px; border-color: rgba(0, 0, 0, 0);" width="405"&gt;&lt;TBODY&gt;&lt;TR style="height: 25px;"&gt;&lt;TD style="width: 61px; height: 25px;"&gt;ROWID&lt;/TD&gt;&lt;TD style="width: 58px; height: 25px;"&gt;ATTRB1&lt;/TD&gt;&lt;TD style="width: 58px; height: 25px;"&gt;ATTRB2&lt;/TD&gt;&lt;TD style="width: 58px; height: 25px;"&gt;ATTRB3&lt;/TD&gt;&lt;TD style="width: 134px; height: 25px;"&gt;ATTRB4&lt;/TD&gt;&lt;/TR&gt;&lt;TR style="height: 25px;"&gt;&lt;TD style="width: 61px; height: 25px;"&gt;1&lt;/TD&gt;&lt;TD style="width: 58px; height: 25px;"&gt;AAA&lt;/TD&gt;&lt;TD style="width: 58px; height: 25px;"&gt;BBB&lt;/TD&gt;&lt;TD style="width: 58px; height: 25px;"&gt;CCC&lt;/TD&gt;&lt;TD style="width: 134px; height: 25px;"&gt;ABCD, EFGH,IKL&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Where ATTRB4 often contains a comma-separated list of values.&amp;nbsp; Unfortunately we need these values as a join field in a SQL view, so the comma-separated list format output we receive needs to be changed so that there is only 1 value in the ATTRB4 column&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So, what I'm working on is creating a non-normalized version of this table (actually a feature class) that will have 3 duplicate rows (duplicate rows not a problem in this case) , with identical values for ATT1-3 but with the ATT4 value being a single one of the comma-separated list values:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE width="405"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="64"&gt;ROWID&lt;/TD&gt;&lt;TD width="64"&gt;ATTRB1&lt;/TD&gt;&lt;TD width="64"&gt;ATTRB2&lt;/TD&gt;&lt;TD width="64"&gt;ATTRB3&lt;/TD&gt;&lt;TD width="149"&gt;ATTRB4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;BBB&lt;/TD&gt;&lt;TD&gt;CCC&lt;/TD&gt;&lt;TD&gt;ABCD&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;BBB&lt;/TD&gt;&lt;TD&gt;CCC&lt;/TD&gt;&lt;TD&gt;EFGH&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;BBB&lt;/TD&gt;&lt;TD&gt;CCC&lt;/TD&gt;&lt;TD&gt;IJKL&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I believe I need to get the comma-separated ATTRB4 column into a list, and loop through it, but am not quite sure how to wrap my head around the code here.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Allen&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PS the comma-separated list values are the result of GeoEvent service (v 10.5.1) that uses a GeoFence and GeoTagger to identify points in certain areas and add the GeoFence name to the output - so when a point is within multiple areas, the multi-value list is result.&amp;nbsp; If anyone happens to know a way to force GeoEvent to output in the format shown in the 2nd table, that would work too.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Feb 2018 21:27:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-write-new-row-based-on-comma-separated/m-p/204033#M15695</guid>
      <dc:creator>AllenScully</dc:creator>
      <dc:date>2018-02-28T21:27:59Z</dc:date>
    </item>
    <item>
      <title>Re: Python - write new row based on comma-separated values in table column?</title>
      <link>https://community.esri.com/t5/python-questions/python-write-new-row-based-on-comma-separated/m-p/204034#M15696</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Something like this should do it:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;fieldList = [ "ATTRB1", "ATTRB2", "ATTRB3", "ATTRB4" ]
with arcpy.da.InsertCursor(targetTable, fieldList) as insertCursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(sourceTable, fieldList) as ‍‍searchCursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in searchCursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for attrbPart in row[3].split(","):
&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;&amp;nbsp;&amp;nbsp; insertCursor.insertRow(( row[0], row[1], row[2], attrbPart ))&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would throw in some null testing, empty string testing, error handling, etc.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:07:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-write-new-row-based-on-comma-separated/m-p/204034#M15696</guid>
      <dc:creator>JamesMacKay3</dc:creator>
      <dc:date>2021-12-11T10:07:41Z</dc:date>
    </item>
    <item>
      <title>Re: Python - write new row based on comma-separated values in table column?</title>
      <link>https://community.esri.com/t5/python-questions/python-write-new-row-based-on-comma-separated/m-p/204035#M15697</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;James this is perfect -&amp;nbsp;&lt;/P&gt;&lt;P&gt;Works like a charm almost 'out of the box'.&amp;nbsp; Many thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Feb 2018 22:48:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-write-new-row-based-on-comma-separated/m-p/204035#M15697</guid>
      <dc:creator>AllenScully</dc:creator>
      <dc:date>2018-02-28T22:48:48Z</dc:date>
    </item>
  </channel>
</rss>

