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?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
cbunn
  • 1,417
  • 17
  • 37
  • 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 08 '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 08 '17 at 13:48
  • Try adding a print statement within your cursor to see what the true value is of your row. – Aaron Jun 08 '17 at 13:52
  • Would you mind providing a full answer, then I can mark it as correct if it runs. – cbunn Jun 08 '17 at 13:56
  • Using CalculateField_management might be an easier approach – Bjorn Jun 08 '17 at 15:01
  • I have several values that I need to update so it would be much easier to run a script than to do it in the field calculator. That is what I am currently doing. – cbunn Jun 08 '17 at 15:23
  • Shouldn't row.YourFieldName = 'Contractor' be row.FixedBy = 'Contractor' if you are trying to update this field? – Emil Brundage Jun 08 '17 at 16:24
  • Yes it should, I must have copied the wrong code snippet for the questiont. I had been running it with the correct field name there though. – cbunn Jun 08 '17 at 16:43

2 Answers2

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)
umbe1987
  • 3,785
  • 2
  • 23
  • 56
  • 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. https://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.

Richard Morgan
  • 2,819
  • 1
  • 21
  • 28
  • 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 08 '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 08 '17 at 21:18
  • @cbunn Try "\"FixedBy\" = ''" as your where clause – Bjorn Jun 08 '17 at 22:47