Select to view content in your preferred language

Use update cursor to find empty fields

5275
3
01-12-2011 02:02 PM
ChristinaHerrick1
Emerging Contributor
I used the 'Merge' tool to combine two feature classes (call them FC1 and FC2), and the resulting feature class (FC3) was put into a file geodatabase. 

I then added new fields to FC3.  I want to be able to populate the newly added fields by copying the values from fields that were carried over from FC1 and FC2, and then eventually delete the old fields.

I am trying to create a script that will use an update cursor to look at each row, and if there's a value for field1, then it would copy it to field3.  If there's no value (aka it's null), then it would copy field2 to field3.  However, the script doesn't recognize when there's a null value, and therefore it always copies from field1, even if it's an empty cell (there's never an instance where both field1 and field2 are empty).

desc = arcpy.Describe(fc3)
fields = desc.fields
rows = arcpy.UpdateCursor(fc3)

for row in rows:
    if row.field1 != "":
        row.field3 = row.field1
    else:
        row.field3 = row.field2
    rows.updateRow(row)

del rows


When I was testing it out, I modified the code (below) to see what arcpy is seeing, and when there were null values, it returned "None" as the value.

desc = arcpy.Describe(fc3)
fields = desc.fields
rows = arcpy.UpdateCursor(fc3)

for row in rows:
    if row.field1 != "":
        print row.field1
    else:
        print row.field2
    rows.updateRow(row)

del rows


I tried fixing it by changing the code to
if row.field1 != "None":
but it didn't change anything.

Any ideas?  What am I doing wrong?

Thanks!
0 Kudos
3 Replies
JasonScheirer
Esri Alum
No quotes on the None:

if row.field1 != None:


or better (grab empty string AND null values):

if row.field1:


I used the 'Merge' tool to combine two feature classes (call them FC1 and FC2), and the resulting feature class (FC3) was put into a file geodatabase. 

I then added new fields to FC3.  I want to be able to populate the newly added fields by copying the values from fields that were carried over from FC1 and FC2, and then eventually delete the old fields.

I am trying to create a script that will use an update cursor to look at each row, and if there's a value for field1, then it would copy it to field3.  If there's no value (aka it's null), then it would copy field2 to field3.  However, the script doesn't recognize when there's a null value, and therefore it always copies from field1, even if it's an empty cell (there's never an instance where both field1 and field2 are empty).

desc = arcpy.Describe(fc3)
fields = desc.fields
rows = arcpy.UpdateCursor(fc3)

for row in rows:
    if row.field1 != "":
        row.field3 = row.field1
    else:
        row.field3 = row.field2
    rows.updateRow(row)

del rows


When I was testing it out, I modified the code (below) to see what arcpy is seeing, and when there were null values, it returned "None" as the value.

desc = arcpy.Describe(fc3)
fields = desc.fields
rows = arcpy.UpdateCursor(fc3)

for row in rows:
    if row.field1 != "":
        print row.field1
    else:
        print row.field2
    rows.updateRow(row)

del rows


I tried fixing it by changing the code to
if row.field1 != "None":
but it didn't change anything.

Any ideas?  What am I doing wrong?

Thanks!
0 Kudos
ChristinaHerrick1
Emerging Contributor
Thanks for the speedy reply!  The first suggestion worked just fine

row.field1 != None

But the second suggestion didn't work.  When I tried it, nothing happened.
0 Kudos
JasonScheirer
Esri Alum
Whoops, meant to put a not in there like

if not row.field1:


glad it works.
0 Kudos