<?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: Creating a Function with an Update Cursor in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045286#M60729</link>
    <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/3374"&gt;@JoeBorgione&lt;/a&gt;and @Anonymous User&amp;nbsp; offer good solutions.&amp;nbsp; But you also need to use the&amp;nbsp; proper form in your query since 'week_a' is a variable name and not the actual field name.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def weekly_update(week_a, hours_a, week_b, hours_b):
     with arcpy.da.UpdateCursor (fc, ['OBJECTID_1', week_a,hours_a, week_b,hours_b], "{} LIKE '%Planned%'".format(week_a)) as cursor:&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or, with Jeff's idea:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fldList = ["Wk25_Oct12Oct15_ACTIV", "Wk25_HRS", "Wk26_Oct19Oct22_ACTIV", "Wk26_HRS"]

def weekly_update(fldList):
     with arcpy.da.UpdateCursor (fc, fldList, "{} LIKE '%Planned%'".format(fldList[0])) as cursor:

   ....&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 08 Apr 2021 23:02:36 GMT</pubDate>
    <dc:creator>RandyBurton</dc:creator>
    <dc:date>2021-04-08T23:02:36Z</dc:date>
    <item>
      <title>Creating a Function with an Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045265#M60725</link>
      <description>&lt;P&gt;I have an update cursor that updates rows of a feature class on AGOL. The code below works.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor (fc, ['OBJECTID_1', 'Wk4_March29April1_ACTIV','Wk4_HRS', 'Wk5_April5April8_ACTIV','Wk5_HRS'], "Wk4_March29April1_ACTIV LIKE '%Planned%'") as cursor:
    for row in cursor:
        row[3] = row[1]
        row[4] = row[2]
        row[1] = None
        row[2] = None
        cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;I'm now trying to turn this code into a function so I don't have to keep hard coding the values. However, the function I've made isn't working when I call it and pass in the variables I want to use. The code below is the function.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def weekly_update(week_a, hours_a, week_b, hours_b):
     with arcpy.da.UpdateCursor (fc, ['OBJECTID_1', 'week_a','hours_a', 'week_b','hours_b'], "week_a LIKE '%Planned%'") as cursor:
        for row in cursor:
            row[3] = row[1]
            row[4] = row[2]
            row[1] = None
            row[2] = None
            cursor.updateRow(row)

#call the function and pass in the variables 

weekly_update("Wk25_Oct12Oct15_ACTIV", "Wk25_HRS", "Wk26_Oct19Oct22_ACTIV", "Wk26_HRS")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the error I keep getting&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class="ansi-red-fg"&gt;RuntimeError&lt;/SPAN&gt;: A column was specified that does not exist. [week_a]&lt;/PRE&gt;&lt;P&gt;Any suggestions would be great. Thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Apr 2021 21:52:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045265#M60725</guid>
      <dc:creator>JamesMcAllister1</dc:creator>
      <dc:date>2021-04-08T21:52:54Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Function with an Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045276#M60726</link>
      <description>&lt;LI-CODE lang="python"&gt;def weekly_update(week_a, hours_a, week_b, hours_b):
     with arcpy.da.UpdateCursor (fc, ['OBJECTID_1', 'week_a','hours_a', 'week_b','hours_b']&lt;/LI-CODE&gt;&lt;P&gt;Try removing the quotes around your parameter-variable names. When you enclose them like that the cursor is looking for a field of those names instead of the value of the variables.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Apr 2021 22:25:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045276#M60726</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-04-08T22:25:28Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Function with an Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045282#M60727</link>
      <description>&lt;P&gt;Unfortunately, that doesn't work.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Apr 2021 22:46:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045282#M60727</guid>
      <dc:creator>JamesMcAllister1</dc:creator>
      <dc:date>2021-04-08T22:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Function with an Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045283#M60728</link>
      <description>&lt;P&gt;Joe is correct. ...An alternative that might help keep variables and parameter assignments from getting tangled is to just pass a list of the fields like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fldList = ["Wk25_Oct12Oct15_ACTIV", "Wk25_HRS", "Wk26_Oct19Oct22_ACTIV", "Wk26_HRS"]

def weekly_update(fldList):
     with arcpy.da.UpdateCursor (fc, fldList, "{} LIKE '%Planned%'".format(fldList[0])) as cursor:
        for row in cursor:
            row[3] = row[1]
            row[4] = row[2]
            row[1] = None
            row[2] = None
            cursor.updateRow(row)

#call the function and pass in the variables 

weekly_update(fldList)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Apr 2021 23:26:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045283#M60728</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-04-08T23:26:20Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Function with an Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045286#M60729</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/3374"&gt;@JoeBorgione&lt;/a&gt;and @Anonymous User&amp;nbsp; offer good solutions.&amp;nbsp; But you also need to use the&amp;nbsp; proper form in your query since 'week_a' is a variable name and not the actual field name.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def weekly_update(week_a, hours_a, week_b, hours_b):
     with arcpy.da.UpdateCursor (fc, ['OBJECTID_1', week_a,hours_a, week_b,hours_b], "{} LIKE '%Planned%'".format(week_a)) as cursor:&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or, with Jeff's idea:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fldList = ["Wk25_Oct12Oct15_ACTIV", "Wk25_HRS", "Wk26_Oct19Oct22_ACTIV", "Wk26_HRS"]

def weekly_update(fldList):
     with arcpy.da.UpdateCursor (fc, fldList, "{} LIKE '%Planned%'".format(fldList[0])) as cursor:

   ....&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 08 Apr 2021 23:02:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045286#M60729</guid>
      <dc:creator>RandyBurton</dc:creator>
      <dc:date>2021-04-08T23:02:36Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Function with an Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045287#M60730</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;(fc, ['OBJECTID_1', week_a, hours_a, week_b, hours_b], "week_a LIKE '%Planned%'")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then it must be the reference to week_a in your selection statement that's bombing it.&amp;nbsp; (I didn't notice that).&lt;/P&gt;&lt;P&gt;If you are using python 3.x I suggest using an f string like this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;(fc, ['OBJECTID_1', week_a, hours_a, week_b, hours_b], f'{week_a} LIKE "%Planned%"')&lt;/LI-CODE&gt;&lt;P&gt;of if you are using python 2.x you'll need to use the format() method:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;(fc, ['OBJECTID_1', week_a, hours_a, week_b, hours_b], '{} LIKE "%Planned%"'.format(week_a)&lt;/LI-CODE&gt;&lt;P&gt;(Of course these are untested, but I think that'll get you going...)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Apr 2021 22:58:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045287#M60730</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-04-08T22:58:10Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Function with an Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045288#M60731</link>
      <description>&lt;P&gt;This is a good solution. Just be aware&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/419076"&gt;@JamesMcAllister1&lt;/a&gt;&amp;nbsp;that if you don't pass the right number of fields, the field assignment will error out. You could validate the&amp;nbsp;&lt;FONT face="courier new,courier"&gt;fldList&lt;/FONT&gt; argument before opening the cursor to make sure it's got four items.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;assert len(fldList) == 4, "fldList must have exactly 4 field names; like week_a, hours_a, week_b, hours_b"&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 08 Apr 2021 23:01:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045288#M60731</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-04-08T23:01:31Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Function with an Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045295#M60732</link>
      <description>&lt;P&gt;Thank you, I like the idea of using a list and having an assert statement.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Apr 2021 23:21:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045295#M60732</guid>
      <dc:creator>JamesMcAllister1</dc:creator>
      <dc:date>2021-04-08T23:21:22Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Function with an Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045296#M60733</link>
      <description>&lt;P&gt;Thank you,&amp;nbsp; the .format() is exactly what I needed.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;"{} LIKE '%Planned%'".format(fldList[0])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Apr 2021 23:22:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-a-function-with-an-update-cursor/m-p/1045296#M60733</guid>
      <dc:creator>JamesMcAllister1</dc:creator>
      <dc:date>2021-04-08T23:22:59Z</dc:date>
    </item>
  </channel>
</rss>

