code for changing nulls to blanks

1822
4
07-15-2011 05:25 AM
by Anonymous User
Not applicable
Original User: alnesbit

Hello all,

I used to do a bit with VBA but I have no idea how to do anything in python. I'm wondering if anyone could help me write a little code to change all of my null values to blanks in all of my fields in my feature classes? http://forums.arcgis.com/threads/28208-show-null-values-as-blank

Thanks in advance,
0 Kudos
4 Replies
StacyRendall1
Occasional Contributor III
By 'blank' you mean...? It also depends on the data type of your field. The easy way: if your field is a string you can do the following in a field calculator (to be safe: create a new field, then use this to get the values into it, then delete the old field; but you can just have the field calculate on itself):
Pre-Logic Script Code:
def nullToBlank(fieldValue):
    if fieldValue == None:
        return ' ' # returns a string with just a space in it (looks blank).
    else:
        return fieldValue

Expression:
nullToBlank(!oldField!)


The hard way would be to make a Python script that iterates through all the rows (using UpdateCursor) and replaces the values that are == None with something...
0 Kudos
by Anonymous User
Not applicable
Original User: alnesbit

By 'blank' you mean...? It also depends on the data type of your field. The easy way: if your field is a string you can do the following in a field calculator (to be safe: create a new field, then use this to get the values into it, then delete the old field; but you can just have the field calculate on itself):
Pre-Logic Script Code:
def nullToBlank(fieldValue):
    if fieldValue == None:
        return ' ' # returns a string with just a space in it (looks blank).
    else:
        return fieldValue

Expression:
nullToBlank(!oldField!)


The hard way would be to make a Python script that iterates through all the rows (using UpdateCursor) and replaces the values that are == None with something...


Hi Stacy,

Thanks for getting back to me. Yes, by blank I mean ' ' and my fields are string fields. I'm having trouble using your code and I think it's because I'm so rusty at using code. I copied it and put the top part in the Pre-Logic Script code box and I put the bottom one in the box below that says "PoleUseType = " (which is my field name I'm calculating). What do I need to change?

The code is a lot like what I usually do - select values that are Null for each field, one by one, then calculate those selected records to ' '. But at least it would save me one step of selecting the values first.

The hard way-you mention creating a python script. I think that is what I am ultimately trying to find. Do you have any idea how I could start to create a script like that?

Thank you for your help,
0 Kudos
ChrisSnyder
Regular Contributor III
Didn't test it, but this code should "blank-ify" any NULL text fields:

#One way...
txtFieldList = arcpy.ListFields(myFC, "", "STRING")
updateRows = arcpy.UpdateCursor(myFC)
for updateRow in updateRows:
   for txtField in txtFieldList:
      if updateRow.isNull(txtField.name):
         updateRow.setValue(txtField.name, '')
   updateRows.updateRow(updateRow)
del updateRow, updateRows


#Another way that might? be faster/better:
txtFieldList = arcpy.ListFields(myFC, "", "STRING")
for txtField in txtFieldList:
   updateRows = arcpy.UpdateCursor(myFC, txtField.name + " IS NULL", "", txtField.name)
   for updateRow in updateRows:
      updateRow.setValue(txtField.name, '')
      updateRows.updateRow(updateRow)
del updateRow, updateRows
0 Kudos
by Anonymous User
Not applicable
Original User: alnesbit

Didn't test it, but this code should "blank-ify" any NULL text fields:

#One way...
txtFieldList = arcpy.ListFields(myFC, "", "STRING")
updateRows = arcpy.UpdateCursor(myFC)
for updateRow in updateRows:
   for txtField in txtFieldList:
      if updateRow.isNull(txtField.name):
         updateRow.setValue(txtField.name, '')
   updateRows.updateRow(updateRow)
del updateRow, updateRows


#Another way that might? be faster/better:
txtFieldList = arcpy.ListFields(myFC, "", "STRING")
for txtField in txtFieldList:
   updateRows = arcpy.UpdateCursor(myFC, txtField.name + " IS NULL", "", txtField.name)
   for updateRow in updateRows:
      updateRow.setValue(txtField.name, '')
      updateRows.updateRow(updateRow)
del updateRow, updateRows


Chris,

I tried the second code because you said it might be faster/better. And it worked like a charm! thank you so much!!
0 Kudos