Python Script for Selecting and Calculating Field Totals

274
6
01-27-2020 07:59 AM
Highlighted
New Contributor

I currently am required to complete a monthly Excel report listing per circuit both the Primary Conductor Mileage and Secondary Conductor Mileage of an electrical network. I have to manually select the values using the select by attributes tool in the tables. Then I have to calculate the distance in Miles. This takes an exceedingly long time because there are around 90 individual circuits. Is there anyone out there that could help me write a Python script to automate this process? Or if you know of someone who can help or some resource I can use to do this, that would also be appreciated.

Reply
0 Kudos
6 Replies
Highlighted
MVP Esteemed Contributor

In order to craft a reasonable response, please provide more details such as your current manual work flow, database structure (egdb versus fgdb versus published feature service).  You might want to get familiar with arcpy and using it.  It's not that daunting....

Reply
0 Kudos
Highlighted
Frequent Contributor II

Try creating it using Model Builder, then export the model as a python script.

However, using generic procedures it should only take a minute or two every month.

Export your primary and secondary to a MS Access database and have premade queries set up to create calculations and another for a report button

Highlighted
MVP Regular Contributor

Daniel,

As the others have said this can easily be put together quite quickly.

Use the arcpy.da search cursor to go through your fields and total them up.

Conceptually  each time your record matches the attribute query, it grabs the corresponding primary and secondary conductor mileage and sums the values up when it hits another attribute match.

Highlighted
MVP Regular Contributor

I'm on my phone so  youd need to check the syntax with arc help. 

Import arcpy

My_database = r'mygdb.gdb'

Query_list = ['location1','loc2','loc3']

For query in query_list :

    Prim_miles=0.0

    Sec_miles = 0.0

    With arcpy.da.SearchCursor(my_database,        'loc' , 'primlength'   seclength') as cursor:

         For row in cursor:

                If query == row[0] :

                       Prim_miles += row[1]

                       Sec_miles += row[2]

      Print(query + " - " + str(Prim_miles) +"pm"

      Print(query + " - " + str(Sec_miles) +"sm"

      Del Prim_miles

      Del Sec_miles

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

Code always looks better when the syntax highlighter is deployed....

Import arcpy



My_database = r'mygdb.gdb'

Query_list = ['location1','loc2','loc3']

For query in query_list :



Prim_miles=0.0

Sec_miles = 0.0

With arcpy.da.SearchCursor(my_database, 'loc' , 'primlength' seclength') as cursor:

For row in cursor:

If query == row[0] :

Prim_miles += row[1]

Sec_miles += row[2]

Print(query + " - " + str(Prim_miles) +"pm"

Print(query + " - " + str(Sec_miles) +"sm"



Del Prim_miles

Del Sec_miles
Reply
0 Kudos
Highlighted
MVP Regular Contributor

Thanks for editing to make readable (was written on phone which doesn't appear to offer that functionality).

I dont think it could look any worse, so better is a good step up.

Reply
0 Kudos