1

I want to run a python code that, if 4 fields are empty, insert in the first of them a text. The fields are all designed to contain text. My code is:

import arcpy
import xlrd 
import numpy 
import pdb
arcpy.env.workspace = r"Directory"
arcpy.env.overwriteOutput = True
shape = r"C:\Directory\Shapefiles.shp"
fields = ['A', 'B', 'C', 'D']
cursor = arcpy.da.UpdateCursor(shape,fields)
for row in cursor:
    if row[0] == None:
        if row[1]== None:
            if row[2]== None:
                if row[3]== None:
                    row[0] = 'Not_Officially_Recorded'
    cursor.updateRow(row)
arcpy.FeatureClassToShapefile_conversion(shape, r'Anotherdirectory')
print('Done')

Anyway, the code runs suspiciously quickly and after I look at it, I see that nothing has been done. So what may the error be?

2
  • Are you sure they are "empty" or Null? Are you sure they don't contain empty strings ('')?
    – alexGIS
    Commented May 9, 2016 at 15:21
  • Actually they contained a " " string, that I had not idea how to notice. Now I replaced the correction criterion and it works Commented May 9, 2016 at 15:33

1 Answer 1

3

It would be more efficient to use and with your if statements, or how I would do it would be to check if the length of the list of your row values is 0 using a filter (using all is another good option here). This is how I would do it:

import arcpy

arcpy.env.workspace = r"C:\Directory"
arcpy.env.overwriteOutput = True
shape = r"C:\Directory\Shapefiles.shp"
fields = ['A', 'B', 'C', 'D']
with arcpy.da.UpdateCursor(shape,fields) as cursor:
    for row in cursor:
        if len(filter(None, row)) == 0:  
            row[0] = 'Not_Officially_Recorded'
            cursor.updateRow(row)

arcpy.FeatureClassToShapefile_conversion(shape, 'Anotherdirectory')
print('Done')

You also have a few modules you're importing that don't appear to be used...You also only need to use the raw string syntax for full paths, doing it for a simple string with no special characters isn't necessary.

4
  • 1
    Thank you for the suggestion. I just realized the issue was a little bit different, i.e. the feature was not actually empty but had a " " and that was the reason why it did not worked. Still your suggestions gave me a good way to make the code smoother and more elegant. Thank you. PS: the packages not reported here are used later in the code, that is the reason why they are there but I forgot to remove them while I posted the question. Commented May 9, 2016 at 15:31
  • Yeah, I have ran into that problem too where the field appears to be empty, but there is a " " value. One way you can bypass that problem is to use something like if len(filter(lambda x: str(x) not in ("", "None"), row)) == 0:
    – crmackey
    Commented May 9, 2016 at 15:35
  • Ok, thank you! I will keep it in mind for the next time I need to do it (probably quite soon). Thank you a lot Commented May 9, 2016 at 15:45
  • No problem, and I actually made a mistake, I forgot to add .strip() to that expression, so it should be: if len(filter(lambda x: str(x).strip() not in ("", "None"), row)) == 0:
    – crmackey
    Commented May 9, 2016 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.