String Manipulation Issues in Python

526
5
Jump to solution
02-14-2011 07:05 AM
KurtSargent
New Contributor II
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
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ChrisSnyder
Regular Contributor III
Two options:

1. Use a try except loop:

fieldList = gp.listfields(fc)
for field in fieldList:
   try:
      gp.DeleteField(fc, field.name)
   except:
      print "Couldn't delete " + field.name #because it was OBJECTID, SHAPE, SHAPE_AREA, etc.

2.
dsc = gp.describe(fc)
keepList = [dsc.oidfieldname, dsc.shapefieldname, "MY_FAV_FIELD", dsc.shapefieldname + "_Length", dsc.shapefieldname + "_Area"]
deleteString = ""
fieldList = gp.listfields(fc)
if field.name not in keepList:
   deleteString = deleteString + field.name + ";"
      gp.DeleteField(fc, deleteString[:-1])

View solution in original post

0 Kudos
5 Replies
ChrisSnyder
Regular Contributor III
Two options:

1. Use a try except loop:

fieldList = gp.listfields(fc)
for field in fieldList:
   try:
      gp.DeleteField(fc, field.name)
   except:
      print "Couldn't delete " + field.name #because it was OBJECTID, SHAPE, SHAPE_AREA, etc.

2.
dsc = gp.describe(fc)
keepList = [dsc.oidfieldname, dsc.shapefieldname, "MY_FAV_FIELD", dsc.shapefieldname + "_Length", dsc.shapefieldname + "_Area"]
deleteString = ""
fieldList = gp.listfields(fc)
if field.name not in keepList:
   deleteString = deleteString + field.name + ";"
      gp.DeleteField(fc, deleteString[:-1])
0 Kudos
ChrisSnyder
Regular Contributor III
Two options:

1. Use a try except loop:

fieldList = gp.listfields(fc)
for field in fieldList:
   try:
      gp.DeleteField(fc, field.name)
   except:
      print "Couldn't delete " + field.name #because it was OBJECTID, SHAPE, SHAPE_AREA, etc.

2.
dsc = gp.describe(fc)
keepList = [dsc.oidfieldname, dsc.shapefieldname, "MY_FAV_FIELD", dsc.shapefieldname + "_Length", dsc.shapefieldname + "_Area"]
deleteString = ""
fieldList = gp.listfields(fc)
if field.name not in keepList:
   deleteString = deleteString + field.name + ";"
      gp.DeleteField(fc, deleteString[:-1])
0 Kudos
KurtSargent
New Contributor II
Thanks Chris,

I'm still puzzled as to why I could't find/replace with str.Replace(old, new,[]), or why all the other things I tried failed, but I've combined your two suggestions and it works like a charm.

Cheers!!

(p.s. I don't see an option to mark question as answered but maybe you can't do that in the new forums?)


dsc = gp.describe(cadToFC)
        keepList = [dsc.oidfieldname, dsc.shapefieldname, "Layer", dsc.shapefieldname + "_Length", dsc.shapefieldname + "_Area"]
        deleteString = ""
        
        fldList = gp.ListFields(cadToFC)

        gp.addmessage("got fldList")

        for fld in fldList:
            if fld.name not in keepList:
                gp.addmessage(fld.name+ "...not in keeplist")
                deleteString = deleteString + fld.name + ";"
                gp.addmessage("added "+ fld.name +" to deletestring")
        gp.addmessage("about to delete fields" + "\n")
        gp.addmessage("deletString looks like this..." + "\n        " + deleteString[:-1])
        try:
                               
            gp.DeleteField(cadToFC, deleteString[:-1])
        except:
            gp.addmessage("failed to delete fields")
0 Kudos
ChrisSnyder
Regular Contributor III
Glad it worked - I like your hybrid approach.

FYI: The .replace() only works on text strings - not lists, so you would have to convert the fieldList to a string (or accumulate a string by looping through the list items which is what I do in my code with the deleteString variable).

p.s. I don't see an option to mark question as answered but maybe you can't do that in the new forums?)


Nope - ESRI is still working on that...
0 Kudos
KurtSargent
New Contributor II
You see, I think that it is a txt string in the original code and I'm still baffled as to why it fails at the noted point. Not a huge deal, but I'm just starting to get the hang of geoprocessing scripts, but I keep running into these weird seeminly unexplainable quirks.


oidFld = "OBJECTID;"
shpFld = "Shape;"
lyrFld = "Layer;"
lenFld = "Shape_Length"
inputs = ""

#returns a list
fldList = gp.ListFields(cadToFC)
for fld in fldList:

[INDENT]#returns a string
fldName = fld.name

#adds to fieldname string + ";"
inputs = inputs + fldName+";"
[/INDENT]

#a string minus the last ";"
delfields = inputs[:-1]
gp.addmessage("here is the delFields string:  "+ delfileds)

if delfields.find(oidFld) != -1:
[INDENT]gp.addmessage("found oidfld")
delFields.replace(oidFld, "") #EXCECUTION ENDS HERE FOR SOME REASON
gp.addmessage("removed oid")[/INDENT]
if delfields.find(shpFld) != -1:
[INDENT]gp.addmessage("found shpfld")
delFields.replace(shpFld, "")
gp.addmessage("removed shpfild")[/INDENT]
if delfields.find(lyrFld) != -1:
[INDENT]gp.addmessage("found lyrfld")
delFields.replace(lyrFld, "")
gp.addmessage("removed lyrfld")[/INDENT]if delfields.find(lenFld) != -1:
[INDENT]gp.addmessage("found lenfld")
delFields.replace(lenFld, "")
gp.addmessage("removed lyrfld")[/INDENT]

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



message results:

here is the delFields string: 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
0 Kudos