I'm trying to find and update all empty attributes for all fields in shapefile with "-" by iterate through all fields in shapefile to find which fields are empty.
I tried tp update all empty attributes using the UpdateCursor and it is not done
import arcpy
from arcpy import env
import os
# Set the workspace for the ListFeatureClass function
env.workspace = "c:/base"
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
fields = arcpy.ListFields(fc)
for field in fields:
with arcpy.da.UpdateCursor(fc2, field) as cursor:
for row in cursor:
**#here I donot know how to check blank values in all feileds#**
I am struck here and from here I don't know how to iterate all rows in all fields and update all empty rows with "-".
I am attaching example
2 Answers 2
You don't need to loop through the fields list if you're using an Update Cursor, as stated by Vince. Also you can specify that the ListFields module only returns string fields only.
for fc in fcList:
fields = [f.name for f in arcpy.ListFields(fc, "STRING")]
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if row[0] is None or row[0] == " ":
row[0] = "-"
cursor.updateRow(row)
-
i am getting an error as " 'field_names' must be string or non empty sequence of strings".if my understanding is correct, in shape fileArb– Arb2018年01月09日 16:52:16 +00:00Commented Jan 9, 2018 at 16:52
-
Updated. Was a typo. fields and field.MacroZED– MacroZED2018年01月09日 16:53:49 +00:00Commented Jan 9, 2018 at 16:53
-
Hi macroZED. Thank you for your code.I have updated "fields" but still I am getting an error as " 'field_names' must be string or non empty sequence of strings". below are the points 1.fields are not only string but also double,float but only string feild need to update 2..all rows in fields are not fully empty.some fields have some rows and reaming rows are empty 3. update cursor need to update all empty rows to be update wth "-"Arb– Arb2018年01月09日 17:06:52 +00:00Commented Jan 9, 2018 at 17:06
-
sorry, i forgot to use a list comprehension to make sure that it only returns the field names. try the updated codeMacroZED– MacroZED2018年01月10日 09:42:30 +00:00Commented Jan 10, 2018 at 9:42
-
Hi, sorry for late comment. still i am getting same error " 'field_names' must be string or non empty sequence of strings". I don't know what is missing in above code.Arb– Arb2018年01月24日 15:48:02 +00:00Commented Jan 24, 2018 at 15:48
An alternative arcpy approach would be to use the Field Calculator. Although more verbose than a cursor, this method captured my empty strings as non-null values (whereas cursors would not).
Below is a function I would use to replace empty strings with "-" (although as others have stated, if you truly want null values - the correct approach would be to return None
, which are actually null type within the attribute table.
import arcpy , os
def replaceBlanksAsNull(input_fc, use_field):
''' For each field in input fc, replace blanks with null values.
'''
theExpression = "makeNull(!{}!)".format(use_field)
theCode = 'def makeNull(value):\n if value == "":\n return None\n else: return value'
arcpy.CalculateField_management(in_table= input_fc,
field= use_field,
expression= theExpression,
expression_type= "PYTHON_9.3",
code_block= theCode)
print('Replaced blanks in {} fc, within field {}'.format(os.path.basename(input_fc), use_field))
{}
). Second, cursors can process all fields, so you've nested incorrectly. Third, you need to use anarcpy.da.UpdateCursor
to make edits. Finally, a '-' isn't a "blank" field value. The documentation on UpdateCursors has all you need to start processing rows. I'd recommend you spend some more time learning how cursors work before giving up and asking here.