Select to view content in your preferred language

Trouble with soft coding and updating row values

869
2
07-06-2012 08:14 AM
JamesHensleigh
Emerging Contributor
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())
Tags (2)
0 Kudos
2 Replies
SolomonPulapkura
Frequent Contributor
try this

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


You probably won't need affectedField variable any more
0 Kudos
JamesHensleigh
Emerging Contributor
Great! This worked well. This embedded getValue() will also help while working with the rest of the code as this was just step one of many operations similar to this inorder to overhaul a badly organized/formatted table.

Thank you Solomon!
0 Kudos