Multivalue Field Parameter Usage

199
3
Jump to solution
09-03-2019 01:23 PM
ConnorMcivor
New Contributor III

I am using a multivalue field parameter which is supposed to be used in a "keepList" of fields. Script makes a copy of a user defined feature class and deletes all fields from the copy except for required fields and fields from the "keepList". I am not receiving any run errors however, the fields I specify in the parameter don't show in the output feature class. What I have so far:

# Appends fields detailed in the user input to keepList
Keep_Fields2 = Keep_Fields.split(";")
for item in Keep_Fields2:
	keepList.append(item)

# Deletes unnecesary fields from the Copy_KP	  
fieldNames = [f.name for f in arcpy.Describe(Copy_KP).Fields if not (f.type in ["OID", "Geometry"] or f.name in ["Shape_Length", "Shape_Area"] or f.name.upper() in  keepList)]  
if fieldNames:  
    arcpy.DeleteField_management(Copy_KP, fieldNames) ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Notes: "Copy_KP" is the copied feature class variable. "Keep_Fields" is the the multivalue field parameter variable. The "keepList" is an empty list '[ ]' (I know this empty list may be redundant, however I have been experimenting with various methods of accomplishing this task). I am going to make the variables more intuitive after I get this thing working.

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

In line 7, you are using 'f.name.upper()' to force field names to upper case for a comparison.  I don't see where you are forcing field names to upper case when you append them to 'keepList'.  So, perhaps change line 4 to:

    keepList.append(item.upper())

View solution in original post

3 Replies
RandyBurton
MVP Regular Contributor

In line 7, you are using 'f.name.upper()' to force field names to upper case for a comparison.  I don't see where you are forcing field names to upper case when you append them to 'keepList'.  So, perhaps change line 4 to:

    keepList.append(item.upper())

ConnorMcivor
New Contributor III

This was correct. I am just beginning to better familiarize myself with various string manipulation methods, and your comment makes sense although I don't fully understand it. I will research more into working with string methods. Thank you Randy!

0 Kudos
RandyBurton
MVP Regular Contributor

To remove case sensitivity when comparing text the .upper() method is often used; it converts all letters in a string to upper case.  If both strings are using the same case, it is easier to check for a case insensitive match.  To illustrate, note the capital F in field1's value:

>>> field1 = "myField"
>>> field2 = "myfield"
>>> if field1 == field2:
... 	print "match"
... 	
>>> if field1.upper() == field2.upper():
... 	print "MATCH"
... 	
MATCH
>>> field1.upper()
'MYFIELD'
>>> field2.upper()
'MYFIELD'
>>> 
0 Kudos