JamesSLC

Trouble with soft coding and updating row values

Discussion created by JamesSLC on Jul 6, 2012
Latest reply on Jul 6, 2012 by JamesSLC
I have developed a hard coded script that removes more than one consecutive white space from a field in a table that works fine. However when I have now transitioned into creating a soft-coded version it does not work properly and populates the affectedField with whatever string the user supplies for the second GetParameterAsText arguement.

It seems to me that the script only views my supplied agruement as a string in the re.sub portion of the script. How can I get the re.sub to view the supplied arguement not as a string but more as a pointer to the correct fieldName.

Any insight you can give me on this would be great. Thank you! Here is a sample of my code:

#get necessary libraries for script
import arcpy, os, re, sys

#set up workspace
arcpy.env.overwriteOutput = True

try:
#arcpy.env.workspace = arcpy.GetParameter(0)
  fc = arcpy.GetParameterAsText(0)
  fieldName = arcpy.GetParameterAsText(1)
  affectedField = 'row.' + fieldName
    
#Open Search Cursor for table
  rows = arcpy.UpdateCursor(fc,'', '', fieldName)
  row = rows.next()

  desc = arcpy.Describe(fc)

  while row:
    newValue = re.sub('\s+' , ' ', row.CtySt)  #this one works but is hard-coded
    #newValue = re.sub('\s+' , ' ', affectedField) #this one does not work but is soft-coded...  
    row.setValue(fieldName, newValue)
    rows.updateRow(row)
    row = rows.next()

  del row, rows

#Report a success message
  arcpy.AddMessage("The unwanted blank spaces have been removed.")

except:
  #Report an error message
  arcpy.AddError("Could not complete the process.")

  #Report any error messages that ESRI generates
  arcpy.AddMessage(arcpy.GetMessages())

Outcomes