Need Help with Python Script

430
7
06-16-2021 08:48 AM
markheinrichs
New Contributor

In my attribute i have 2 columns  i need to address. the layer is water valves and the 2 columns are valve size and exercised picture is attached. i need to reset my exercised column from yes back to null annually  which i have the script for but i need to address the valve size 6",8",10",12" etc. individually so i can reset just that valve size. any ideas are appreciated.

thanks in advance 

0 Kudos
7 Replies
DavidPike
MVP Frequent Contributor

I would just use a simple field calculation with python. either GP tool or right-click on the field in the attribute table and select calculate field.  I would also make a copy/backup of your data before doing anything.

Calculate Field (Data Management)—ArcGIS Pro | Documentation

select Python as the language and then something like (NB you have aliases for your fields so use their real names in the below code):

if !valve_size! == '12"':
  return None

 

 

 

markheinrichs
New Contributor
i tried the line but i get the error in the attachment. i tried valve size without the underscore and no space same error.

thanks
0 Kudos
DavidPike
MVP Frequent Contributor

Can you perhaps check what the real field name is?

markheinrichs
New Contributor
attached is a picture of the field names.

thanks
0 Kudos
DavidPike
MVP Frequent Contributor

No that's a picture of the attribute table with the field aliases.

Just find the fieldname in the expression window, then:

#enter this in the box below the '='
myFunction(!The name of your field as it appears in the fields window above!)

#enter this in the 'code block' window
def myFunction(size)
  if size == '12"':
    return None
0 Kudos
DougGreen
Occasional Contributor

So it sounds like you'd like to modify your python script to not only set the "Exercised" column back to NULL but also to do something with valve size. I can't exactly tell from your question what you need to do with the "Valve Size" column. Perhaps, you are wanting to just reset the "Exercised" column to NULL for valves of a certain size? If that is the case, I would use an update cursor with a where clause.

# set up an update cursor including any fields that you'll need updated or to determine what to update
with arcpy.da.UpdateCursor(fcPath, field_names=['UNITID', 'VALVESZ', 'Exercised'], where_clause='VALVESZ IN (6, 8, 10)') as cursor:
    for valve in cursor:
        if (valve[2] != None): # None is python's equivalent of NULL
            valve[2] = None

            cursor.updateRow(valve)
0 Kudos
markheinrichs
New Contributor
Thanks for the reply. i have 1774 different valves made up of 6 different sizes and we have to exercise them at different times to meet the state regs. i use python 3 script !Exercised!.replace("Yes","") to set everything that is yes back to null like it says and that works, but it is almost all the valves and i do want to pick the size and do just that size. the state wants 10" and 12" done every 2 years and anything smaller every 4 . i'm new to this and got lost in your answer but appreciate the quick response.

thanks
0 Kudos