Hi,
I have my attribute table sorted ascending according to my street names (Aprox. 1914 records). I need to make a new field that starts at 10,000 and increments by 1 until it is filled. Is there a way I can do this? I'm thinking of doing some loop that increments by one (because the default for loop in the field calculator repeats the same number) but I don't know how to do that. Any help? Thanks.
Kimball Hansen
updateTheRows = arcpy.UpdateCursor("C:\\Filename.shp","","","","AddressName A" ) #get the rows from the shapefile order by address ascending i = 0 for updateTheRow in updateTheRows: updateTheRow.setValue("FieldNameToUpdate",10000 + i) updateTheRows.updateRow(updateTheRow) i= i+1 del updateTheRow del updateTheRows
I copied that code and when I run the Field Calculator I get an error. The error number is 000539 if that has any relevance to you. Does the field have to be a certain type? (I tried int and double and both didn't work). Or do all the records need to have zero or 1 first in them to us the code? I don't have any other guesses of what to do.
There should have been a text message after the number, the text will be more useful to determine the issue, however make sure your indenting is correct for python and that Python is checked as the code type. Python REQUIRES block indenting
However, I don't believe this code takes into account that you wanted the records numbered in address order.
I don't know Python so I don't know what parts to indent.
rec=0 def autoIncrement(): global rec pStart = 10000 #adjust start value, if req'd pInterval = 1 #adjust interval value, if req'd if (rec == 0): rec = pStart else: rec = rec + pInterval return rec
I know this is an old thread, but how does it know to loop through the rows? I've seen this bit of code posted elsewhere and I never understood why just a for loop wouldn't work.
In short....that is what the field calculator does... it provides the framework to loop through the rows and apply a function, which is also why it works on selections leaving the unselected untouched. This would require a separate bit of code in itself to work with selections. It does have its drawbacks, like having to use globals to keep track of what has gone on in the previous row (ie a previous value).
Sometimes the field calculator is just faster for simple things. It only gets difficult when people try to push the envelop and get it to do things for which it wasn't designed.