<?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: Calculate field/ python code question in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355668#M75764</link>
    <description>&lt;P&gt;Sorry about that. The basis of the code was correct, but I forgot how to work with regex a bit (I only recently began to understand how it works) and had to change things around.&amp;nbsp;(Also this code assumes that your species fields are integers. If they're not, you might have to play around and convert to text or something. Similarly, it assumes that each species field matches the value in SPCOMP exactly.)&lt;/P&gt;&lt;P&gt;My fields are a bit different (so I used fid and test instead of OBJECTID and SPCOMP), but below is the before and after. I made sure to test with any number of spaces.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AlfredBaldenweck_0-1701440450901.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/87726i106AA03ED0BB5FC8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AlfredBaldenweck_0-1701440450901.png" alt="AlfredBaldenweck_0-1701440450901.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AlfredBaldenweck_1-1701440496156.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/87727i352107830D0F65F6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AlfredBaldenweck_1-1701440496156.png" alt="AlfredBaldenweck_1-1701440496156.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;One final thing: If you run an update cursor with the attribute table open, you have to refresh the table to see the changes. Also, this was written for a file gdb. If you're using an enterprise gdb, it gets a little bit more complicated.&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;&lt;LI-CODE lang="python"&gt;import re #Edited to add this line in since I forgot it whoops.
fc = r"Trail_MDB1.gdb\testingFC"
specDict ={}
fieldList = ['OBJECTID']
# Search cursor, split the SPCOMP and load into dictionary.
# Populate field list to calculate fields later.
with arcpy.da.SearchCursor(fc, ['OBJECTID', 'SPCOMP']) as cursor:
    for row in cursor:
        # Find all instances of two letters, followed by one or more spaces and 
        #   any number of numbers.
        for line in re.findall(r"([a-zA-z]{2} +\d+)", row[1]):
            # You could probably do .split()[0] and [1] here but
            #   I'm on a roll with the regex.
            sp = re.match(r"[a-zA-z]{2}", line).group(0)
            #I have no idea why, but the match doesn't like the number 
            #   when there are also letters in the string
            #   so we're using search instead.
            num = re.search(r"(\d+)", line).group(0)
            # num = line.split(" ")[1]
            # sp = line.split(" ")[0]
            
            # Add species to fieldList
            # You could probably comment this out and manually populate
            # the fieldList
            if sp not in fieldList:
                fieldList.append(sp)
                
            # Add to the dictionary.
            # specDict = {ObjectID: {Species 1: Number, Species 2: Number...}}
            if str(row[0]) in specDict:
                specDict[str(row[0])][sp] = num
            else:
                specDict[str(row[0])] = {sp: num}

with arcpy.da.UpdateCursor(fc, fieldList) as cursor:
    for row in cursor:
        # Search through the field list and get the index of the field
        # in the row tuple, then apply the the correct value for that 
        # species to that field.
        for field in fieldList:
            if field in specDict[str(row[0])]:
                row[fieldList.index(field)] = specDict[str(row[0])][field]
        cursor.updateRow(row)&lt;/LI-CODE&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 01 Dec 2023 22:05:35 GMT</pubDate>
    <dc:creator>AlfredBaldenweck</dc:creator>
    <dc:date>2023-12-01T22:05:35Z</dc:date>
    <item>
      <title>Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355469#M75736</link>
      <description>&lt;P&gt;Good day all,&lt;/P&gt;&lt;P&gt;I have a layer with the following fields in the attribute table:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="BrodieChisholm_0-1701387279253.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/87661i300EC47595B556E8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="BrodieChisholm_0-1701387279253.png" alt="BrodieChisholm_0-1701387279253.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I am trying to populate the species fields (PW, PR, PJ, SB, etc.)&amp;nbsp;with the information found in the "SPCOMP" field.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="BrodieChisholm_2-1701387479307.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/87663i0B40388C7772D6A2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="BrodieChisholm_2-1701387479307.png" alt="BrodieChisholm_2-1701387479307.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;All of the values that are in the “SPCOMP” field should be put into related individual species fields, and leave the rest of species fields to 0. For example, if the “SPCOMP” = “Pj 60Pt 30Bw 10”, that means that Pj=60, Pt=30, Bw=10. Then 60 should go under “PJ”, 30 goes under “PT”, and 10 goes under “BW”.&lt;/P&gt;&lt;P&gt;I've tried various different things through the "Calculate field" tool but have been unsuccessful.&lt;/P&gt;&lt;P&gt;I am thinking something like:&lt;BR /&gt;For field PW: if SPCOMP includes the text "Pw" return following number - omit the rest of the string, else return 0.&lt;/P&gt;&lt;P&gt;Obviously this is not in proper Python code, and would have to be repeated for every field.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If anyone has a solution, or Python code example to accomplish this, I would be so grateful.&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Nov 2023 23:44:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355469#M75736</guid>
      <dc:creator>BrodieChisholm</dc:creator>
      <dc:date>2023-11-30T23:44:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355483#M75737</link>
      <description>&lt;P&gt;your spcomp field is poorly combined&lt;/P&gt;&lt;P&gt;&amp;nbsp; PJ60 PT30 BW10&lt;/P&gt;&lt;P&gt;would have allowed one to split the inputs on the spaces.&lt;/P&gt;&lt;P&gt;Are you stuck with that column?&amp;nbsp; If so, are all the numbers in increments of 10? (eg 10 20 30.... 90 (no 0 or 100)&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 00:19:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355483#M75737</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2023-12-01T00:19:45Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355511#M75739</link>
      <description>&lt;P&gt;There's always regex if you're brave.&lt;/P&gt;&lt;P&gt;This assumes that your fields are always going to match the species codes. It's also more or less untested.&lt;/P&gt;&lt;P&gt;The basic workflow is:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Cycle through the table and create dictionary of objectid: {species: number} using regex to find all instances of two letters followed by one or more spaces and any number of digits in the SPCOMP field.&lt;/LI&gt;&lt;LI&gt;Cycle through again with an update cursor and update the rows as needed.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;You could probably consolidate this all into a single update cursor, but I didn't have time today to think it through.&lt;/P&gt;&lt;P&gt;Anyway, I think (hope) that this is at least somewhat useful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import re

fc = #your feature class
specDict ={}
fieldList = ["OBJECTID"]
# Search cursor, split the SPCOMP and load into dictionary.
# Populate field list to calculate fields later.
with arcpy.da.SearchCursor(fc, ['OBJECTID', 'SPCOMP']) as cursor:
    for row in cursor:
        # Find all instances of two letters, followed by one or more spaces and 
        #   any number of numbers.
        for line in re.findall(r"([a-zA-z]{2} +\d*)", row[1]):
            # You could probably do .split()[0] and [1] here but
            #   I'm on a roll with the regex.
            num = re.match(r"\d", line)
            sp = re.match(r"[a-zA-z]{2}", line) 
            # num = line.split(" ")[1]
            # sp = line.split(" ")[0]
            
            # Add species to fieldList
            # You could probably comment this out and manually populate
            # the fieldList
            if sp not in fieldList:
                fieldList.append(sp)
                
            # Add to the dictionary.
            # specDict = {ObjectID: {Species 1: Number, Species 2: Number...}}
            if str(row[0]) in specDict:
                specDict[str(row[0])][sp] = num
            else:
                specDict[str(row[0])] = {sp: num}
                
with arcpy.da.UpdateCursor(fc, fieldList) as cursor:
    for row in cursor:
        # Search through the field list and get the index of the field
        # in the row tuple, then apply the the correct value for that 
        # species to that field.
        for field in fieldList:
            if field in specDict[str(row[0])]:
                row[row.index(field)] = specDict[row[0]][field]
        cursor.updateRow(row)&lt;/LI-CODE&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, 01 Dec 2023 01:49:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355511#M75739</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-12-01T01:49:31Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355625#M75757</link>
      <description>&lt;P&gt;Hello Dan,&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Since this was an exercise I was given to test my coding skills (which are very limited - I have only used python a handful of times, for very basic stuff) -&lt;/SPAN&gt;&amp;nbsp;if reorganizing the column and keeping the data intact allows for an easier solution to my problem - then I think this is a good way to go.&lt;/P&gt;&lt;P&gt;I also see what you are saying with regards to its formatting "Pj 10Sb 60 La 30" should infact be written: "Pj10 Sb60 La30".&lt;/P&gt;&lt;P&gt;Although, rearranging the column format would necessitate more python code I am not familiar with...&lt;/P&gt;&lt;P&gt;The numbers in the SPCOMP field are all in increments of 10 - ranging from 10 to 100 (10 % to 100% species composition).&lt;BR /&gt;&lt;BR /&gt;I really appreciate the comment and ideas to get me on the right path.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 13:25:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355625#M75757</guid>
      <dc:creator>BrodieChisholm</dc:creator>
      <dc:date>2023-12-01T13:25:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355634#M75758</link>
      <description>&lt;P&gt;Hello Alfred,&lt;/P&gt;&lt;P&gt;I greatly appreciate you taking the time to type all this out for me.&lt;BR /&gt;&lt;BR /&gt;I am fairly new to python and have only used it a handful of times to perform basic tasks. Not at all familiar with regex, unfortunately.&lt;/P&gt;&lt;P&gt;I will try the code you provided and see if I am able to perform the required task.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 14:21:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355634#M75758</guid>
      <dc:creator>BrodieChisholm</dc:creator>
      <dc:date>2023-12-01T14:21:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355650#M75759</link>
      <description>&lt;P&gt;No worries, I just tested it and found some issues. Will upload an update shortly.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 14:08:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355650#M75759</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-12-01T14:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355668#M75764</link>
      <description>&lt;P&gt;Sorry about that. The basis of the code was correct, but I forgot how to work with regex a bit (I only recently began to understand how it works) and had to change things around.&amp;nbsp;(Also this code assumes that your species fields are integers. If they're not, you might have to play around and convert to text or something. Similarly, it assumes that each species field matches the value in SPCOMP exactly.)&lt;/P&gt;&lt;P&gt;My fields are a bit different (so I used fid and test instead of OBJECTID and SPCOMP), but below is the before and after. I made sure to test with any number of spaces.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AlfredBaldenweck_0-1701440450901.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/87726i106AA03ED0BB5FC8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AlfredBaldenweck_0-1701440450901.png" alt="AlfredBaldenweck_0-1701440450901.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AlfredBaldenweck_1-1701440496156.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/87727i352107830D0F65F6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AlfredBaldenweck_1-1701440496156.png" alt="AlfredBaldenweck_1-1701440496156.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;One final thing: If you run an update cursor with the attribute table open, you have to refresh the table to see the changes. Also, this was written for a file gdb. If you're using an enterprise gdb, it gets a little bit more complicated.&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;&lt;LI-CODE lang="python"&gt;import re #Edited to add this line in since I forgot it whoops.
fc = r"Trail_MDB1.gdb\testingFC"
specDict ={}
fieldList = ['OBJECTID']
# Search cursor, split the SPCOMP and load into dictionary.
# Populate field list to calculate fields later.
with arcpy.da.SearchCursor(fc, ['OBJECTID', 'SPCOMP']) as cursor:
    for row in cursor:
        # Find all instances of two letters, followed by one or more spaces and 
        #   any number of numbers.
        for line in re.findall(r"([a-zA-z]{2} +\d+)", row[1]):
            # You could probably do .split()[0] and [1] here but
            #   I'm on a roll with the regex.
            sp = re.match(r"[a-zA-z]{2}", line).group(0)
            #I have no idea why, but the match doesn't like the number 
            #   when there are also letters in the string
            #   so we're using search instead.
            num = re.search(r"(\d+)", line).group(0)
            # num = line.split(" ")[1]
            # sp = line.split(" ")[0]
            
            # Add species to fieldList
            # You could probably comment this out and manually populate
            # the fieldList
            if sp not in fieldList:
                fieldList.append(sp)
                
            # Add to the dictionary.
            # specDict = {ObjectID: {Species 1: Number, Species 2: Number...}}
            if str(row[0]) in specDict:
                specDict[str(row[0])][sp] = num
            else:
                specDict[str(row[0])] = {sp: num}

with arcpy.da.UpdateCursor(fc, fieldList) as cursor:
    for row in cursor:
        # Search through the field list and get the index of the field
        # in the row tuple, then apply the the correct value for that 
        # species to that field.
        for field in fieldList:
            if field in specDict[str(row[0])]:
                row[fieldList.index(field)] = specDict[str(row[0])][field]
        cursor.updateRow(row)&lt;/LI-CODE&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 22:05:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355668#M75764</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-12-01T22:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355690#M75770</link>
      <description>&lt;P&gt;Hello Alfred,&lt;BR /&gt;&lt;BR /&gt;Again, I really appreciate your time and effort in trying to find this solution.&lt;/P&gt;&lt;P&gt;As I am trying to perform this task with a shapefile - the above code is no working.&lt;BR /&gt;&lt;BR /&gt;I will keep trying things though.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 14:57:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355690#M75770</guid>
      <dc:creator>BrodieChisholm</dc:creator>
      <dc:date>2023-12-01T14:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355692#M75771</link>
      <description>&lt;P&gt;Hi Brodie, just tested it on a shapefile and it worked fine.&lt;/P&gt;&lt;P&gt;If it's not giving you an error message, then I assume you just have to refresh the table for it to show up correctly.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 15:04:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355692#M75771</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-12-01T15:04:43Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355774#M75778</link>
      <description>&lt;P&gt;Hi Alfred,&lt;/P&gt;&lt;P&gt;I confirmed that my SPCOMP field is in fact text format.&lt;/P&gt;&lt;P&gt;Made the following changes to the code provided:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="BrodieChisholm_3-1701448981141.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/87756i6ADD5BB81F362BCC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="BrodieChisholm_3-1701448981141.png" alt="BrodieChisholm_3-1701448981141.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I changed OBJECTID to FID since I did not have an OBJECTID field:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="BrodieChisholm_5-1701449080073.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/87759iF70A6AF08A77A8EF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="BrodieChisholm_5-1701449080073.png" alt="BrodieChisholm_5-1701449080073.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Here is the error I am receiving:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="BrodieChisholm_4-1701449017247.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/87757iD75B35D9E52DFCBE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="BrodieChisholm_4-1701449017247.png" alt="BrodieChisholm_4-1701449017247.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Line 10 being:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt; for line in re.findall(r"([a-zA-z]{2} +\d*)", row[1]):&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Again your help is invaluable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 16:46:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355774#M75778</guid>
      <dc:creator>BrodieChisholm</dc:creator>
      <dc:date>2023-12-01T16:46:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355866#M75787</link>
      <description>&lt;P&gt;I'm sorry, there was a copy-paste error on my part.&lt;/P&gt;&lt;P&gt;Add "import re" as the very first line of the code.&lt;/P&gt;&lt;P&gt;(I've updated my snippet to reflect this)&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 22:04:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1355866#M75787</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-12-01T22:04:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356007#M75803</link>
      <description>&lt;P&gt;For future reference if you need to fix similar data structures, and for learning&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def separate_string_number(string, as_list=False):
    """Return a string split into strings and numbers, as a list.

    z = 'Pj 60Pt 30Bw 10'
    z0 = 'PJ60PT30BW10'
    separate_string_number(z)
    separate_string_number(z0)
    returned value
    ['Pj', '60', 'Pt', '30', 'Bw', '10']

    separate_string_number("A .1 in the 1.1 is not 1")
    ['A', '.1', 'in', 'the', '1.1', 'is', 'not', '1']

    Modified from https://stackoverflow.com/a/57359921/6828711
    """
    groups = []
    prev = string[0]
    newword = string[0]
    if len(string) &amp;lt;= 1:
        return [string]
    for x, i in enumerate(string[1:]):
        if i.isalpha() and prev.isalpha():
            newword += i
        elif (i.isnumeric() or i == '.') and (prev.isnumeric() or prev == '.'):
            newword += i
        else:
            groups.append(newword.strip())
            newword = i
        prev = i
        if x == len(string) - 2:
            groups.append(newword.strip())  # strip any spaces
            newword = ''
    # remove extraneous space values in groups
    groups = [i for i in groups if i != '']
    if as_list:
        return groups
    # -- pair values, special case
    s = " ".join(["".join(groups[pos:pos + 2])
                  for pos in range(0, len(groups), 2)]
                 )
    return s&lt;/LI-CODE&gt;&lt;P&gt;example&lt;/P&gt;&lt;LI-CODE lang="python"&gt;z = 'Pj 60Pt 30Bw 10'

separate_string_number(z, as_list=False)
'Pj60 Pt30 Bw10'

separate_string_number(z, as_list=True)
['Pj', '60', 'Pt', '30', 'Bw', '10']&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 01 Dec 2023 22:02:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356007#M75803</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2023-12-01T22:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356094#M75814</link>
      <description>&lt;P&gt;Although a regular expression answer has been put forward, I believe there is a more straightforward regular expression than what has been already proposed.&amp;nbsp; Also, all that is needed in terms of cursors is a single pass through the data set with an update cursor.&lt;/P&gt;&lt;P&gt;First, the regular expression.&amp;nbsp; You are trying to match pairs of species-values that have been concatenated into a single string, so there are 3 things to differentiate:&amp;nbsp; 1) a species-value pair, 2) the species within that pair, and 3) the numeric value for the species in that pair.&lt;/P&gt;&lt;P&gt;The following regular expression captures that logic:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; import re
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; reg_exp = "(?:([A-Za-z]+)\s*([0-9]+))+?"
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; SPCOMP_samples = [
...     "Pj 60Sb 40",
...     "Pj 60Sb 40",
...     "Sb 80Pj 10La 10",
...     "Pj 60Sb 40",
...     "Pj 80Sb 10Pt 10",
...     "Pj 80Sb 10Pt 10"
... ]
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; for sample in SPCOMP_samples:
...     re.findall(reg_exp, sample)
...
[('Pj', '60'), ('Sb', '40')]
[('Pj', '60'), ('Sb', '40')]
[('Sb', '80'), ('Pj', '10'), ('La', '10')]
[('Pj', '60'), ('Sb', '40')]
[('Pj', '80'), ('Sb', '10'), ('Pt', '10')]
[('Pj', '80'), ('Sb', '10'), ('Pt', '10')]
&amp;gt;&amp;gt;&amp;gt; &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The regular expression uses a non-capturing outer group to find the species-value pairs, and then uses two internal capturing groups to differentiate the species from the numeric value.&amp;nbsp; Using the expression with re.findall returns a list of tuples containing a species and a numeric value.&lt;/P&gt;&lt;P&gt;Since each tuple contains the species and the value, you can use the species to look up the index of the species field in a cursor and update that field with the value.&lt;/P&gt;&lt;LI-CODE lang="python"&gt; # Note: Code below hasn't been tested
 
 reg_exp = "(?:([A-Za-z]+)\s*([0-9]+))+?"
 
 fc = # path to feature class or shape file 
 with arcpy.da.UpdateCursor(fc, "*") as cur:
    spcomp_idx = cur.fields.index("SPCOMP")
    for row in cur:
        for species, value in re.findall(reg_exp, row[spcomp_idx]):
            species_idx = cur.fields.index(species.upper())
            row[species_idx] = value
        cur.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Dec 2023 20:17:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356094#M75814</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2023-12-02T20:17:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356245#M75836</link>
      <description>&lt;P&gt;Hello Alfred,&lt;BR /&gt;&lt;BR /&gt;I amended your code and have been able to run through most of it - currently getting an error right at the end (line 41, 42):&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="BrodieChisholm_0-1701694414079.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/87882i6648918E2AC95098/image-size/medium?v=v2&amp;amp;px=400" role="button" title="BrodieChisholm_0-1701694414079.png" alt="BrodieChisholm_0-1701694414079.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I tried replacing "str" with the field name, 'Pw' for example or 'SPCOMP' but yielded no better results.&lt;BR /&gt;&lt;BR /&gt;I apologize as at this point my lack of knowledge involving python really is hindering me, I don't want to be a nuissance.&lt;BR /&gt;&lt;BR /&gt;Though, I really do appreciate the work you have put into this and help you have offered.&lt;/P&gt;&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 12:55:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356245#M75836</guid>
      <dc:creator>BrodieChisholm</dc:creator>
      <dc:date>2023-12-04T12:55:39Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356260#M75840</link>
      <description>&lt;P&gt;Called it. This is a lot more straightforward than what I did.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 13:51:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356260#M75840</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-12-04T13:51:06Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356264#M75841</link>
      <description>&lt;P&gt;So, a key error is when you go to look something up in a dictionary and it doesn't exist. So it's looking for the value of 52 and not finding it and freaking out.&lt;/P&gt;&lt;P&gt;A way to get around this normally is to use dictionary.get(), in which you tell it what key to look for and what to return if you don't find it. (I don't use this in general because I find it confusing lol).&lt;/P&gt;&lt;P&gt;In this case, however, I'm not really sure what happened here. The str() is important because bad things happen if you use a true number (e.g. an integer) as a dictionary key, so I convert to string to get around that. But your value should be in there so ???&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd give&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/1371"&gt;@JoshuaBixby&lt;/a&gt;'s solution a shot. It's a lot more straightforward than mine.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 13:55:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356264#M75841</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-12-04T13:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356270#M75842</link>
      <description>&lt;P&gt;Hello Joshua,&lt;BR /&gt;&lt;BR /&gt;This is great, thank you for doing the work and getting this written up for me.&lt;BR /&gt;&lt;BR /&gt;I have been able to run the first part independently - however I am running into an issue with the second half.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a screen shot - along with the error message I am receiving:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="BrodieChisholm_0-1701698971428.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/87887iEA86FD26CA448B68/image-size/medium?v=v2&amp;amp;px=400" role="button" title="BrodieChisholm_0-1701698971428.png" alt="BrodieChisholm_0-1701698971428.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Note that I modified the beginning of the code to add: import re #Edited to add this line in since I forgot it whoops.&lt;BR /&gt;fc = r"D:\Brodie\Projects\2023\FRMG Exercises\Coding Exercise\FRMG_Coding_Exercise\ftg_true_all_change.shp"&lt;BR /&gt;reg_exp = "(?:([A-Za-z]+)\s*([0-9]+))+?"&lt;BR /&gt;&lt;BR /&gt;Thanks again, I really appreciate your help with this (I am a floundering newbie with python).&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 14:15:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356270#M75842</guid>
      <dc:creator>BrodieChisholm</dc:creator>
      <dc:date>2023-12-04T14:15:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356273#M75843</link>
      <description>&lt;P&gt;Take that offending line out entirely.&lt;/P&gt;&lt;P&gt;In addition to the indent error, that line is redefining the fc variable to be ???? because you haven't put a value there, just a comment telling you to put a value there. (Edit: this will throw an end of line error, since it's expecting a value for fc. It just hit the indent error first.) You already defined the fc variable at the top, so you don't need it here. (Probably the same for the second instance of the regular expression)&lt;/P&gt;&lt;P&gt;Python relies on indentation to figure out how each line relates to each other, lines belonging to a loop are indented further than the loop open itself.&lt;/P&gt;&lt;P&gt;e.g.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for i in [1,2,3]:
    #Do something
    # Do something else&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this case, the offending line has one space at the beginning, throwing it out of line with everything else.&lt;/P&gt;&lt;P&gt;Also, so does the following line (with arcpy.da.UpdateCursor...)&lt;/P&gt;&lt;P&gt;Generally, 4 spaces (Or a tab, pick one or the other but not both) is recommended per indent level, but I think technically as long as they're consistent within whatever loop they belong to, you're fine. Four spaces is better for readability, though.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 14:25:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356273#M75843</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-12-04T14:25:36Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate field/ python code question</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356276#M75844</link>
      <description>&lt;P&gt;HALLELUJAH!&lt;BR /&gt;&lt;BR /&gt;It's working, you are all the greatest, I am so appreciative of all of your help.&lt;BR /&gt;&lt;BR /&gt;It takes a community as they say.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you all.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 14:32:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/calculate-field-python-code-question/m-p/1356276#M75844</guid>
      <dc:creator>BrodieChisholm</dc:creator>
      <dc:date>2023-12-04T14:32:46Z</dc:date>
    </item>
  </channel>
</rss>

