0

I need to iterate through a file GDB feature classes to replace values found in all the columns of attribute tables with the blank ones (empty'').

Here is my code, that doesn't work. Truth be told I'm not familiar with programming languages, but couldn't implement this task in ModelBuilder, that is why it seems the only way. What's wrong with it?

import arcpy

fc = "C:\Users\Vadim\Downloads\GML\MolokovGP_p10.gdb"
fieldList = arcpy.ListFields(fc, field_type="integer")

fieldObs = arcpy.ListFields (fc)  
fieldNames = []  
for field in fieldObs:  
    fieldNames.append(field.name)  
del fieldObs  
fieldCount = len(fieldNames)  


with arcpy.da.UpdateCursor(fc, fieldNames) as curU:  
    for row in curU:  
        rowU = row  
        for field in range(fieldCount):  
            if rowU[field] == None:  
                rowU[field] = replacementValue  


        curU.updateRow(rowU)

This error occurs:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 4, in <module>
  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\__init__.py", line 1131, in ListFields
    return gp.listFields(dataset, wild_card, field_type)
  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\geoprocessing\_base.py", line 344, in listFields
    self._gp.ListFields(*gp_fixargs(args, True)))
RuntimeError: ERROR 999999: Error executing function.
0

2 Answers 2

3

In your code error that you presented in the comment this is the line causing problems.

fieldList = arcpy.ListFields(fc, field_type="integer")

I think integer needs a capital I. But you don't use fieldList later. So you could either remove this line and just use

fieldObs = arcpy.ListFields (fc) 

But the problem here is that you are getting fields that are not updateable. Something like FID. So you may want to filter the list of fields before processing.

Otherwise, the rest of the code looks okay at first glance.

EDIT:

Based on the comment, the code now looks something like this?

arcpy.env.workspace = r'path to database' #Change to your database

featureclasses = arcpy.ListFeatureClasses()
replacementValue = 1
for fc in featureclasses:
    fields = [f.name for f in arcpy.ListFields(fc) if f.type=='Integer']
    with arcpy.da.UpdateCursor(fc,fields) as cursor:
        for row in cursor:
            for field in range(len(fields)):  
                if row[field] == None:  
                    row[field] = replacementValue
            cursor.updateRow(row)
3
  • thank you for response. I replaced the line but it as I understood there must be additional action. I also found similar topic on the forum and tried to use offered script from here: gis.stackexchange.com/questions/298575/…
    – Vadim
    Oct 23, 2019 at 13:25
  • I modified it but still have problems: {import arcpy arcpy.env.workspace = r'C:\Users\Vadim\Downloads\GML\MolokovGP_p10.gdb' #Change to your database featureclasses = arcpy.ListFeatureClasses() #Use this line to list all feature classes in workspace for fc in featureclasses: fields = [f.name for f in arcpy.ListFields(fc) if f.type=='Integer'] with arcpy.da.UpdateCursor(fc,fields) as cursor: for row in cursor: row = ["" if v is None else v for v in row] #If value is none, replace with empty string, else reuse the value cursor.updateRow(row)}
    – Vadim
    Oct 23, 2019 at 13:27
  • Thank you fot the help. It works!
    – Vadim
    Oct 24, 2019 at 10:13
0

2 scripts for different types of the fields:

import arcpy

arcpy.env.workspace = r'C:\Users\Vadim\Downloads\GML\MolokovGP_p102.gdb' #Change to your database

featureclasses = arcpy.ListFeatureClasses()
replacementValue = ''
for fc in featureclasses:
    fields = [f.name for f in arcpy.ListFields(fc) if f.type=='String']
    with arcpy.da.UpdateCursor(fc,fields) as cursor:
        for row in cursor:
            for field in range(len(fields)):  
                if row[field] == None:  
                    row[field] = replacementValue
            cursor.updateRow(row)

and for the integer/double ones:

import arcpy

arcpy.env.workspace = r'C:\Users\Vadim\Downloads\GML\MolokovGP_p102.gdb' #Change to your database

featureclasses = arcpy.ListFeatureClasses()
replacementValue = '-9999'
for fc in featureclasses:
    fields = [f.name for f in arcpy.ListFields(fc) if f.type=='Double' or f.type=='Integer']
    with arcpy.da.UpdateCursor(fc,fields) as cursor:
        for row in cursor:
            for field in range(len(fields)):  
                if row[field] == None:  
                    row[field] = replacementValue
            cursor.updateRow(row)

Your Answer

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

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