AnsweredAssumed Answered

String Manipulation Issues in Python

Question asked by ksargent on Feb 14, 2011
Latest reply on Feb 15, 2011 by ksargent
Hello all,

I'm having trouble with a script that I'm writing. I want to iterate through an FC's fields, get each field name and add to a string of field names seperated by semicolons to be used as an argument for:

gp.deletefield(InFC, delFields).

But! I first want to remove from the string the 4 fields that I want to keep, i.e. OBJECTID,Shape, Layer, and Shape_Length.

As can be seen by the message output, I can iterate through the fields, get their names, add each name to a string with ";" inbetween, and print this final string. However, once I try to use

str.replace(old, new[, count])


to remove the items I wish to remove, the script terminates with no error message.

Any ideas??

I'm using ArcEditor 9.3.1 sp2



__________________________________________________ ____
oidFld = "OBJECTID;"
shpFld = "Shape;"
lyrFld = "Layer;"
lenFld = "Shape_Length"

fldList = gp.ListFields(cadToFC)
inputs = ''

gp.addmessage("got fldList")

for fld in fldList:
fldName = fld.name

gp.AddMessage(fldName)

inputs = inputs + fldName+";"
delfields = inputs[:-1]


gp.addmessage("\n" + delfields + "\n")

if delfields.find(oidFld) != -1:
gp.addmessage("found oidfld")
delFields.replace(oidFld, "")
gp.addmessage("removed oid")
if delfields.find(shpFld) != -1:
gp.addmessage("found shpfld")
delFields.replace(shpFld, "")
gp.addmessage("removed shpfild")
if delfields.find(lyrFld) != -1:
gp.addmessage("found lyrfld")
delFields.replace(lyrFld, "")
gp.addmessage("removed lyrfld")
if delfields.find(lenFld) != -1:
gp.addmessage("found lenfld")
delFields.replace(lenFld, "")
gp.addmessage("removed lyrfld")
gp.addmessage("Edited: " + delfields + "\n")

__________________________________________________ ______________________

Messages generated:

getting field list from WINNIPEGOSIS_SUB_TRANS_dxf
got fldList
OBJECTID
Shape
Entity
Handle
Layer
LyrFrzn
LyrLock
LyrOn
LyrVPFrzn
LyrHandle
Color
EntColor
LyrColor
BlkColor
Linetype
EntLinetype
LyrLnType
BlkLinetype
Elevation
Thickness
LineWt
EntLineWt
LyrLineWt
BlkLineWt
RefName
LTScale
ExtX
ExtY
ExtZ
DocName
DocPath
DocType
DocVer
Shape_Length

OBJECTID;Shape;Entity;Handle;Layer;LyrFrzn;LyrLock ;LyrOn;LyrVPFrzn;LyrHandle;Color;EntColor;LyrColor ;BlkColor;Linetype;EntLinetype;LyrLnType;BlkLinety pe;Elevation;Thickness;LineWt;EntLineWt;LyrLineWt; BlkLineWt;RefName;LTScale;ExtX;ExtY;ExtZ;DocName;D ocPath;DocType;DocVer;Shape_Length

found oid

Outcomes