Select to view content in your preferred language

Python script if/for/while

5970
22
Jump to solution
07-09-2015 09:46 AM
CoyPotts1
Deactivated User

*UPDATE*

For samples of the correct code, see posts below.

I'll start off by apologizing for not having any sort of sample of my code, but I am really just unsure on how to begin the code, so all I have is notes of what I need.

I have a feature class that has a number of fields that are duplicated for 5 different customers.  The fields that I need to focus on in this exercise are the Cust1Name, Cust1NodeID, Cust1HomeID, and 10 Cust1CircuitID fields.  I'm using "Cust1", "Cust2", etc only as examples in place of their actual names...just stating that to eliminate your efforts in trying to add the customer number in a potential for/while loop.  Essentially the fields look as they do below:

Cust1Name

Cust1NodeID

Cust1HomeID

Cust1CircuitID1

...

Cust1CirctuitID10

Cust2Name

Cust2NodeID

Cust2HomeID

Cust2CircuitID1

...

Cust2CircuitID10

etc...

My feature class has fields for all 5 customers, but not all 5 will be populated.  I've used the Cust#NodeID field as my reference for whether calculations will run or not because that field determines if they are needed or not.  Populated means run the calculation, while Null means do nothing.

if Cust1NodeID != None:
     return # insert calculation here
else:
     return None

I also need to get a users input to know the amount of circuit ID's needed.  The number varies from 1 to 10 circuit ID's.

numberIDs = arcpy.GetParameterAsText(0)

With this information I need the script to go through each Cust#NodeID field to check and see if it's null or not.  If it's null, keep moving...if it has data, calculate the amount of circuit ID fields needed based on the input.


Again, sorry for having nothing to start with.  I'm just confused on which route to take (for loop, while loop, if/if...), and I can't figure out any of them either way.  I'm just not sure how to add so many arguments together.

*EDIT*

I'll also note that the circuit ID calculation itself is just a concatenation of the given fields, as shown below:

Cust1CircuitID1 = (Cust1Name) - (Cust1NodeID) - (Cust1HomeID) - (01)

Cust1CircuitID2 = (Cust1Name) - (Cust1NodeID) - (Cust1HomeID) - (02)

etc...

0 Kudos
22 Replies
DanPatterson_Retired
MVP Emeritus

Your first two tries don't work since you are checking for None AND if some of the entries are numbers they have to be converted to strings before concatenating

for row in cursor: 

    print("row: {},  row[0]: {}".format(row,row[0]))  # or arcpy.AddMessage(...)

    if row[0]: 

        if numIDs < 2

            row[3] = "(" + row[2] + ") - (" + row[0] + ") - (" + row[1] + ") - (01)" 

        elif numIDs < 3

            row[3] = "(" + row[2] + ") - (" + row[0] + ") - (" + row[1] + ") - (01)" 

            row[4] = "(" + row[2] + ") - (" + row[0] + ") - (" + row[1] + ") - (02)" 

        cursor.updateRow(row)

put some print or arcpy messages in your work to see where it gets


					
				
			
			
				
			
			
				
			
			
			
			
			
			
		
DanPatterson_Retired
MVP Emeritus

It would be useful if you recorded your workflow.  Specifically, do you use tools in ArcToolbox...like calculating values for fields, or doing selections... or are you doing things through their menu/tool/button equivalent in ArcMap.  It is much easier to produce a script if you can replicate your workflow through arctoolbox tools and examine/copy the documentation from the Results Window (customize menu) to begin structuring your work (several options are documentated on my blog)

CoyPotts1
Deactivated User

I use a script that calculates a lot of fields and runs a lot of summary statistics before exporting the results to Excel tables (a lot of the work that you have helped me with in other posts over the previous weeks).  That script is ran from an ArcToolbox within ArcMap.  This script is ran after we have created the data and made sure that all of the manually populated fields are populated, and then this script populates the rest and exports for us. 

Essentially the circuit ID fields have either gone unpopulated or done manually by the users, and I've been trying find the time to go through and figure out a way to automate the process of their creation. 

I could technically just do individual calculations for every single CircuitID field, but there are 10 per customer, and 5 total customers, so I would prefer not to rewrite the code for all 50 fields.  Darren Wiens​'s suggestion above seems to be a good solution, and I think I have it nearly worked out, with the exception of the string issue. 

0 Kudos