As I dive further into Python, I find myself making a "GIS Module" that calls functions that are useful to my work.
I created a quick find and replace function but i want to print off the total number items replaced.
I also have a show unique value function that lists all the unique items in a field and their values. I want to include just a simple line like:
547 items replaced
here are my 2 functions can anyone explain how to include this:
def field_counter(table,fieldname): values = [r[0] for r in arcpy.da.SearchCursor(table, [fieldname])] unique_values = set(values) count_dict = {} for value in unique_values: count_dict[value] = values.count(value) print("{0} Unique Value: {1} has {2} attribute(s)".format(fieldname, value, count_dict[value])) def find_and_replace(table, field, find, replace): with arcpy.da.UpdateCursor(table, [field]) as cursor: for row in cursor: if row[0] == find: row[0] = replace cursor.updateRow(row) print(row[0] + ' was replaced by' + replace) elif row[0] != find: print(row[0] + ' was not replaced')
Solved! Go to Solution.
Untested, but should work like so:
counter = 0 with arcpy.da.UpdateCursor(table, [field]) as cursor: for row in cursor: if row[0] == find row[0] = replace cursor.updateRow(row) counter = counter + 1 print counter return counter
To pass counter back to the calling function, you would 'return counter', but since we don't see where you call the function, it's hard to write it. It would be something like:
count = find_and_replace(table, field, find, replace) print count # the number returned by function
Untested, but should work like so:
counter = 0 with arcpy.da.UpdateCursor(table, [field]) as cursor: for row in cursor: if row[0] == find row[0] = replace cursor.updateRow(row) counter = counter + 1 print counter return counter
To pass counter back to the calling function, you would 'return counter', but since we don't see where you call the function, it's hard to write it. It would be something like:
count = find_and_replace(table, field, find, replace) print count # the number returned by function
I didn't realize that it was that easy.... haha. Here I was thinking I needed to do a bunch of stuff in the loop to make this work, Thanks!
Matthew,
To do iterative counts I calculate the modulo of the desired increment. This can be useful when you have a large dataset and want to see its progress. It would look something like this:
counter = counter + 1 if (counter%1000) == 0: print str(counter)
Note that in order to print an integer you will need to make it a string too.
Regards,
Tom
Python will print integers just fine without casting it to string. The problem I think you are referring to arises when you try to concatenate strings with integers.
This works:
>>> for i in range(100): ... count = count + 1 ... print count
This doesn't work:
>>> for i in range(100): ... count = count + 1 ... print "Count: " + count
Darren,
You are correct sir.
counter = counter + 1
if (counter%1000) == 0:
print str(counter) + " items processed."
would be the correct syntax.
Regards,
Tom
Might be better (cleaner) to use string format(). Based on the correct answer from Darren Wiens, this is what I would do.
counter = 0 with arcpy.da.UpdateCursor(table, [field]) as cursor: for row in cursor: if row[0] == find: row[0] = replace cursor.updateRow(row) counter += 1 print "{} items processed".format(count) return counter
Good information. I still consider myself a Novice at this by I am trying to absorb as much as possible.
Thanks Darren Wiens and Tom