Field type in Python. Please help

3243
3
09-30-2010 05:23 AM
RickyCraig
New Contributor
Hi all,

trying to build a model using a combination of model builder and python. I'm not a programmer so my preference is just model builder but I can't do a certain step without a python script (at least I can't think of a way).

I have a shapefile and I need to test whether or not it has a specific field name (I found a code for that in the ESRI help files) and also if it does have that field that it is of a particular type and length e.g. text and 15.

The ESRI script works fine for determining the presence of the field. I can't get the code adjusted to allow for the field type though.

The code allows for 2 outputs, essentially True or False, which i can use as preconditions for the rest of the model.

If anyone has a hint on how to alter this so that I can test whether the field is of the correct type and length that would be much appreciated.

Cheers
0 Kudos
3 Replies
RDHarles
Occasional Contributor
You could do something like this to test the TYPE and LENGTH, simply using an "if" statement:

loop = gp.listFields(In_Table, "*", "ALL")
field = loop.next()
while field:
    
    # List the name, length and type of every field in the shapefile
    print "Name:   " + field.Name
    print "Length: " + str(field.Length)
    print "Type:   " + field.Type+"\n"


    if field.name == "PlanzNr" and field.length == 20 and field.type == "TEXT":
        ...do something...
0 Kudos
JoelCalhoun
New Contributor III
FYI,  using the ListFields method with a feature class in a file geodatabase returns a field type of "string" not "text".  I'm not sure if this is different for shapefiles.

However if you are creating a new field in a feature class in a file geodatabase using the AddField method a type of "text" is required.  I'm not sure if this is different for shapefiles.

This is not the only difference.

A couple others are:

ListFields returns: Integer
AddField needs:    Long

List fields returns: SmallInteger
Addfield needs:     Short



I hope this helps,


Joel
0 Kudos
RickyCraig
New Contributor
Hey guys,

thanks for the replies. I'll try your suggestions and see how far I get.

Cheers
0 Kudos