Using a wildcard inside DeleteField_Management?

2188
5
Jump to solution
08-27-2013 05:11 AM
GregParent
New Contributor III
Hello,
I am trying to put a wildcard inside this delete field. I am using PythonWin 2.6 and hitting Feature Class data in SDE. I already use a system argument inside the field name and had to convert it to a string for it to work. Is that the problem with trying to use a wildcard for a number inside a string?

arcpy.DeleteField_management(PROJ4_ADMIN_DELETE_AFTER_TEST, "FID_GE41_REG_SBPD_MOB_"+PR+"_WOR_9")
                                                                                                                                                   
I would like to use a wildcard for the number 9 in the field name. 

Thanks in advance for any input you may have.

Greg
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Greg,

You can use a wildcard in the ListFields function, and then pass the returned list to the DeleteField function.  Ex:

fc = r"C:\temp\test.gdb\project" list = [] PR = "some text"  for field in arcpy.ListFields(fc, "FID_GE41_REG_SBPD_MOB_"+PR+"_WOR_*"):     list.append(field.name)  arcpy.DeleteField_management(fc, list)

View solution in original post

0 Kudos
5 Replies
markdenil
Occasional Contributor III
You don't mention what error you are getting back.

I assume you can drop the field if you hard code the field name?

Have you tried printing out  "FID_GE41_REG_SBPD_MOB_"+PR+"_WOR_9" ,
or comaparing it to the field name as returned by  ListFields,
to make sure it resolves as a match for your field name?

Is PR (9) a string or int?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Greg,

You can use a wildcard in the ListFields function, and then pass the returned list to the DeleteField function.  Ex:

fc = r"C:\temp\test.gdb\project" list = [] PR = "some text"  for field in arcpy.ListFields(fc, "FID_GE41_REG_SBPD_MOB_"+PR+"_WOR_*"):     list.append(field.name)  arcpy.DeleteField_management(fc, list)
0 Kudos
GregParent
New Contributor III
You don't mention what error you are getting back.
I assume you can drop the field if you hard code the field name?
Have you tried printing out  "FID_GE41_REG_SBPD_MOB_"+PR+"_WOR_9" ,
or comaparing it to the field name as returned by  ListFields,
to make sure it resolves as a match for your field name?
Is PR (9) a string or int?


Hello mdenil,
Yes, I can drop the field if it is hard coded. The issue stems from the automatic renaming of the field after an Intersect. The long field name gets FID_ then is concatenated and given a number. The number is changing depending on when the code is run and how many times the code is run.

As a Python noob, I banged away at trying various things for wildcards ("%", +%+, etc) and got numerous error messages.

"%" gives;  not all arguments converted during string formatting

This lead me to think it was a INT versus string issue. The +PR+ system argument has been converted to a string. Is the 9 an INT when it is inside the " "?

Thanks.

Greg
0 Kudos
GregParent
New Contributor III
Hi Greg,

You can use a wildcard in the ListFields function, and then pass the returned list to the DeleteField function.  Ex:

fc = r"C:\temp\test.gdb\project"
list = []
PR = "some text"

for field in arcpy.ListFields(fc, "FID_GE41_REG_SBPD_MOB_"+PR+"_WOR_*"):
    list.append(field.name)

arcpy.DeleteField_management(fc, list)


Hello JSkinn3,
I will give it a try and let you know what happens.
Thanks
Greg
0 Kudos
GregParent
New Contributor III
Hi Greg,
You can use a wildcard in the ListFields function, and then pass the returned list to the DeleteField function.  Ex:
fc = r"C:\temp\test.gdb\project"
list = []
PR = "some text"
for field in arcpy.ListFields(fc, "FID_GE41_REG_SBPD_MOB_"+PR+"_WOR_*"):
    list.append(field.name)
arcpy.DeleteField_management(fc, list)



This worked. Thank you very much.
0 Kudos