1

I have a script I want to use to update a value for a group of selected features in ArcMap in a string field. I am using the UpdateCursor method but I cannot get the output generated that I want. I know my code is close but something is off. Here is my code:

import arcpy

fc = r'D:\_data\sidewalk.gdb\repairs'

cursor = arcpy.UpdateCursor(fc)

for row in cursor:
    if row.FixedBy == '':
        row.FixedBy = 'Contractor'
        cursor.updateRow(row)

print "Processing complete"

del row

I am trying to update the string "FixedBy" field where values are blank, not 'Null', with the value 'Contractor'. What is my code missing?

8
  • cursor.updateRow(row) should be unindented. Try replacing row.FixedBy == '' with row.FixedBy == None. Also, I would recommend using the new data access da cursors.
    – Aaron
    Jun 8 '17 at 13:41
  • I get a syntax error when i unindent curcor.updateRow(row). Any my originaly value is not Null but rather blank.
    – cbunn
    Jun 8 '17 at 13:48
  • Try adding a print statement within your cursor to see what the true value is of your row.
    – Aaron
    Jun 8 '17 at 13:52
  • Would you mind providing a full answer, then I can mark it as correct if it runs.
    – cbunn
    Jun 8 '17 at 13:56
  • Using CalculateField_management might be an easier approach
    – Bjorn
    Jun 8 '17 at 15:01
2

This should work if the value of FixedBy is NULL or blank (meaning no text at all, including spaces):

import arcpy

fc = r'D:\_data\sidewalk.gdb\repairs'

with arcpy.da.UpdateCursor(fc,['FixedBy']) as cursor:
    for row in cursor:
        if row[0] == None or row[0] == '':
            row[0] = 'Contractor'
        cursor.updateRow(row)
2
  • This runs perfectly thank you! If the GDB is stored on an SDE server what would the file path look like?
    – cbunn
    Jun 15 '17 at 21:04
  • Glad it helped. You could look at this answer to know how to do that. gis.stackexchange.com/a/5541/9518.
    – umbe1987
    Jun 16 '17 at 11:01
1

One idea is to pass in a where clause to the UpdateCursor; then you don't need to check any values. All rows returned could be updated.

Assuming you are using 10.1+, switching to arcpy.da cursors is also recommended.

import arcpy

fc = r'D:\_data\sidewalk.gdb\repairs'
i = 0

with arcpy.da.UpdateCursor(fc, ['FixedBy'], "FixedBy =''") as cursor:
    for row in cursor:
        row[0] = 'Contractor'
        cursor.updateRow(row)
        i += 1

print "Processed {}...".format(i)

(Untested code.)

If the count is 0, there is a problem with your where clause or your data isn't what you think you are seeing.

3
  • OP mentioned in the comments that other edits were also being made in the cursor loop, so the where clause may not be appropriate in this specific case.
    – Bjorn
    Jun 8 '17 at 21:00
  • It says it processed all the records despite me only selecting 2 records for it to run against. Also it did not update the "FixedBy" field. It is a string field in a File GDB. How do I adjust my where clause?
    – cbunn
    Jun 8 '17 at 21:18
  • @cbunn Try "\"FixedBy\" = ''" as your where clause
    – Bjorn
    Jun 8 '17 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.