I'm developing a script tool in ArcGIS 10.0 that has two parameters: the input feature class and an input field. The objective is to remove extra spaces from the strings in the input field. The following code works fine.
import arcpyarcpy.env.overwriteOutput = Truetry: # Set input parameters inFC = arcpy.GetParameterAsText(0) inField = arcpy.GetParameterAsText(1) # Iterate through strings in fields and remove extra spaces arcpy.AddMessage("Removing extra spaces from strings in fields...") cursor = arcpy.UpdateCursor(inFC) for row in cursor: name = row.getValue(inField) if name: removesp = " ".join(name.split()) row.setValue(inField, removesp) cursor.updateRow(row) del row, cursor arcpy.AddMessage("Calculation complete")except: arcpy.AddError("Could not complete the calculation") arcpy.AddMessage(arcpy.GetMessages())I'd like to be able to perform this calculation on multiple fields at once. I changed the second input parameter to multivalue and modified the code as follows:
try: # Set input parameters FC = arcpy.GetParameterAsText(0) Fields = arcpy.GetParameterAsText(1) # Iterate through strings in fields and remove extra spaces arcpy.AddMessage("Removing extra spaces from strings in fields...") cursor = arcpy.UpdateCursor(FC) for row in cursor: for Field in Fields: name = row.getValue(Field) if name: removesp = " ".join(name.split()) row.setValue(Field, removesp) cursor.updateRow(row) del row, cursor arcpy.AddMessage("Calculation complete")I've studied other examples, such as arcpy.UpdateCursor for multiple fields. I suspect that my input parameter for the fields list isn't getting formatted correctly for the interpreter but I can't figure out how to print what the actual list looks like during runtime. I could be off in my loop too. Any ideas?
أكثر...
import arcpyarcpy.env.overwriteOutput = Truetry: # Set input parameters inFC = arcpy.GetParameterAsText(0) inField = arcpy.GetParameterAsText(1) # Iterate through strings in fields and remove extra spaces arcpy.AddMessage("Removing extra spaces from strings in fields...") cursor = arcpy.UpdateCursor(inFC) for row in cursor: name = row.getValue(inField) if name: removesp = " ".join(name.split()) row.setValue(inField, removesp) cursor.updateRow(row) del row, cursor arcpy.AddMessage("Calculation complete")except: arcpy.AddError("Could not complete the calculation") arcpy.AddMessage(arcpy.GetMessages())I'd like to be able to perform this calculation on multiple fields at once. I changed the second input parameter to multivalue and modified the code as follows:
try: # Set input parameters FC = arcpy.GetParameterAsText(0) Fields = arcpy.GetParameterAsText(1) # Iterate through strings in fields and remove extra spaces arcpy.AddMessage("Removing extra spaces from strings in fields...") cursor = arcpy.UpdateCursor(FC) for row in cursor: for Field in Fields: name = row.getValue(Field) if name: removesp = " ".join(name.split()) row.setValue(Field, removesp) cursor.updateRow(row) del row, cursor arcpy.AddMessage("Calculation complete")I've studied other examples, such as arcpy.UpdateCursor for multiple fields. I suspect that my input parameter for the fields list isn't getting formatted correctly for the interpreter but I can't figure out how to print what the actual list looks like during runtime. I could be off in my loop too. Any ideas?
أكثر...