gp.listfields Method

397
4
03-08-2011 09:54 AM
DuncanRager
New Contributor
Greetings,

Basic question... I'm trying to list out the names of all the fields in a particular shapefile.

Using the gp.listfields method in the following manner...

fields = gp.listfields("[shapefile_location]")
print fields


...I get a list of items resembling, "geoprocessing describe field object object at 0x032A2F0".

How can I print the actual field names?

Thanks,

DR
Tags (2)
0 Kudos
4 Replies
JasonScheirer
Occasional Contributor III
print [f.name for f in fields]
0 Kudos
DuncanRager
New Contributor
Worked great, thanks.

So I'm getting a 'u' in front of each field name... what does that mean?

Also, is this method specific to anything, like the geoprocessing object maybe? In other words, if I had a simple list, say...

silverware = ["forks", "knives", "spoons"]


...and I input...

print [f.name for f in silverware]


...I would get an error. What's the difference between the lists?

Thanks,

DR
0 Kudos
DanPatterson_Retired
MVP Emeritus
the u stands for unicode (just ignore it)

>>> silverware = ["forks", "knives", "spoons"]
>>> for utensil in silverware:
...  print utensil
...  
forks
knives
spoons

as for the "name" thing, your simple example above has no name property whereas a field has a name property as well as several others (eg type, precision, scale if appropriate etc etc)
0 Kudos
JasonScheirer
Occasional Contributor III
u means it is a Unicode object, as opposed to a regular (locale-specific) C-style string.

Doing this:
print [f.name for f in silverware]

is trying to get the name property off the string, which does not exist, hence the error.
0 Kudos