0

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

enter image description here

  • 2
    You've got a bunch of isses here. Fist off, make sure your code is indented correctly by using the code formatting button ({}). Second, cursors can process all fields, so you've nested incorrectly. Third, you need to use an arcpy.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. – Vince Jan 6 '18 at 13:30
  • 2
    It's curious that you had a great many more coding details right in the question asked three hours earlier. Why isn't this a duplicate of that? – Vince Jan 6 '18 at 13:40
  • 2
    Also a flaw in your logic is that searching for "-" is a text character not a number, you need to deal with numeric fields as well as the string fields you want to update. – Hornbydd Jan 6 '18 at 17:48
  • Thanks for comments. First of all, I want search for empty or blank values("-" is not blank. it is an example of output after update) and i want to update all blank attributes with "-". Anyhow numeric fields will have zero(0) by default.So i want to update all text blank fields values with "-" for text. if one field meas i can use – Arb Jan 7 '18 at 9:34
  • for one field, I may use if row[0] == " " – Arb Jan 7 '18 at 9:41
1

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 file – Arb Jan 9 '18 at 16:52
  • Updated. Was a typo. fields and field. – MacroZED Jan 9 '18 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 Jan 9 '18 at 17:06
  • sorry, i forgot to use a list comprehension to make sure that it only returns the field names. try the updated code – MacroZED Jan 10 '18 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 Jan 24 '18 at 15:48

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.