Listing Values in a Field

615
8
Jump to solution
01-26-2012 02:56 AM
TommyBurk
New Contributor II
Is there a way to list values in a field using python? I can list the field names or the field types in a certain feature class, but I want to list the actual values associated with the field. Is this possible? Thanks.

Tommy Burk
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
TommyBurk
New Contributor II
Mathew, Rafael,

I really appreciate your assistance with this. The final output ended up looking something like this:

IdList = [] rows = arcpy.SearchCursor("INPUT_FILE")  for row in rows:      IdPlusOne = int(row.ID_FIELD) + int(1)      IdList.append(IdPlusOne)  del row, rows  arcpy.MakeFeatureLayer_management("FC SELECTING FROM", "layer_lyr")  i = 0 for Id in IdList:      if IdList == 0:           arcpy.SelectLayerByAttribute_management("layer_lyr", "NEW_SELECTION", "\"ID_FIELD\" = " + str(IdList))           i+=1      elif IdList >= 1:           arcpy.SelectLayerByAttribute_management("layer_lyr", "ADD_TO_SELECTION", "\"ID_FIELD\" = " + str(IdList))           i+=1  arcpy.CopyFeatures_management("layer_lyr", "NEW_FEATURE_CLASS")


This allowed me to create a list from a previous feature class, scroll through that feature class and list the values in an ID field, create a selection based on the list with 1 added to every value (which allows me to select the next row based on a consecutive ID field), and create new feature class from the new selection. I then merged the previous feature class and the new feature class to create a feature class the contains the original features in the list, and each following feature. I hope that's not too confusing. Nevertheless, it worked. Thanks again. You both were very helpful.

Tommy Burk

View solution in original post

0 Kudos
8 Replies
RaphaelR
Occasional Contributor II
hi Tommy,

you can do this with a search cursor:

import arcpy

inFile = "e:/test.shp"

# create searchcursor
rows = arcpy.SearchCursor(inFile)

#go through rows and print the values of Fields FIELD_A and FIELD_B
for row in rows:
    print urow.FIELD_A
    print urow.FIELD_B

del row, rows
0 Kudos
TommyBurk
New Contributor II
Hi Rafael,

Thanks for your response. The script you gave me does what I want. It prints the values of the OBJECTID field. Can I make this something that I can reference now? I want to use the OBJECTID field in one feature class to create a query to in another class. Essentially I want to do a OBJECTID +1 and use that statement to create a selection in another feature class. I originally thought a list would do it, because then I could call that list again.....Thanks again for your help!

Tommy Burk
0 Kudos
RaphaelR
Occasional Contributor II
getting the OIDs+1 into a list shouldn´t be a problem, but unfortunately i don´t quite understand the rest of your last post.

    IdList = []
    for row in rows:
        IdPlusOne = row.ID + 1
        
        IdList.append(IdPlusOne)

    del row, rows
    print IdList
0 Kudos
TommyBurk
New Contributor II
Thanks again. That gave me the list +1 which is perfect. Now I want to do a selection from a feature class that goes something like from FIELD in the feature class select values in the LIST.

where_clause = "FIELD = LIST"

Is that possible? Is there a special way I have to format the clause? After exporting from model builder, it looks like the format would be something like "\"ID_Field\" = List". Thanks so much.

Tommy Burk
0 Kudos
MathewCoyle
Frequent Contributor
I have done something quite similar, though with strings and not int or objectid types, so you may need slightly different formatting based on your needs.

For whatever tool you want to plug the where clause in, this should get you started. Should just need some slight modification to accommodate your int type.
where = "FIELD in ('" + '\',\''.join(dataList) + "')"


Here's a link to a forum thread I think you may be interested in. Correct me if I'm wrong.
http://forums.arcgis.com/threads/42445-Python-list-to-where-clause
0 Kudos
TommyBurk
New Contributor II
Mathew, Rafael,

I really appreciate your assistance with this. The final output ended up looking something like this:

IdList = [] rows = arcpy.SearchCursor("INPUT_FILE")  for row in rows:      IdPlusOne = int(row.ID_FIELD) + int(1)      IdList.append(IdPlusOne)  del row, rows  arcpy.MakeFeatureLayer_management("FC SELECTING FROM", "layer_lyr")  i = 0 for Id in IdList:      if IdList == 0:           arcpy.SelectLayerByAttribute_management("layer_lyr", "NEW_SELECTION", "\"ID_FIELD\" = " + str(IdList))           i+=1      elif IdList >= 1:           arcpy.SelectLayerByAttribute_management("layer_lyr", "ADD_TO_SELECTION", "\"ID_FIELD\" = " + str(IdList))           i+=1  arcpy.CopyFeatures_management("layer_lyr", "NEW_FEATURE_CLASS")


This allowed me to create a list from a previous feature class, scroll through that feature class and list the values in an ID field, create a selection based on the list with 1 added to every value (which allows me to select the next row based on a consecutive ID field), and create new feature class from the new selection. I then merged the previous feature class and the new feature class to create a feature class the contains the original features in the list, and each following feature. I hope that's not too confusing. Nevertheless, it worked. Thanks again. You both were very helpful.

Tommy Burk
0 Kudos
ChrisSnyder
Regular Contributor III
If anyone is interested, this v9.3 code will list (i.e. print) field values in a sort of ArcInfo Workstation-esque sort of way. Needs updating to arcpy probably...

Sytax is like this:

listRecords(myTable, 20, "MYFIELD = 1")

def listRecords(inputValue, numberOfRecordsToList = 25, whereClause = ""):
    "Lists field names and values in inputValue"
    if gp.exists(inputValue) == 1:
        fieldNamesList = []
        fieldList = gp.listfields(inputValue)
        field = fieldList.next()
        while field:
            fieldNamesList.append(field.name)
            field = fieldList.next()
        searchRows = gp.SearchCursor(inputValue, whereClause)
        searchRow = searchRows.next()
        rowCount = 1
        recordCount = int(gp.GetCount_management(inputValue))
        if int(numberOfRecordsToList) > recordCount:
            numberOfRecordsToList = recordCount
        while rowCount <= int(numberOfRecordsToList):
            print "RECORD #" + str(rowCount)
            print "-" * 100
            for fieldName in fieldNamesList:
                try:
                    print fieldName + ": " + str(searchRow.getvalue(fieldName))
                except:
                    print fieldName + ": !?!" 
            print ""
            print ""
            rowCount = rowCount + 1
            searchRow = searchRows.next()
        del searchRow
        del searchRows
        print ""
        print ""
        print "LISTED " + str(numberOfRecordsToList) + " RECORDS"
    else:
        print "ERROR: " + inputValue + " does not exist!"
 


The output looks like this:

RECORD #1
----------------------------------------------------------------------------------------------------
OBJECTID: 1
REMSOFT_ID: 13
ALTERNATIVE: Landscape
T3_ID: 102
WAU_NM: Sekiu
LPU_NM: Sekiu
REGIME_NM: Undefined rotation
ROTATION: 0
REGEN_PERS: 9
REGEN_CNT: 1
THIN_PERS:
THIN_CNT: 0
ENTRIES: 1
PRE_THINS: 0
POST_THINS: 0
INTR_THINS: 0
MGMT_SEVR: 1.0
FREQUENCY: 10
MIN_ACRES: 0.372585348944
FIRST_HARV_AGE: None


RECORD #2
----------------------------------------------------------------------------------------------------
OBJECTID: 2
REMSOFT_ID: 13
ALTERNATIVE: No Action
T3_ID: 102
WAU_NM: Sekiu
LPU_NM: Sekiu
REGIME_NM: No harvest expected
ROTATION: 0
REGEN_PERS:
REGEN_CNT: 0
THIN_PERS:
THIN_CNT: 0
ENTRIES: 0
PRE_THINS: 0
POST_THINS: 0
INTR_THINS: 0
MGMT_SEVR: 0.0
FREQUENCY: 10
MIN_ACRES: 0.372585348944
FIRST_HARV_AGE: None


RECORD #3
----------------------------------------------------------------------------------------------------
OBJECTID: 3
REMSOFT_ID: 15
ALTERNATIVE: Landscape
T3_ID: 102
WAU_NM: Sekiu
LPU_NM: Sekiu
REGIME_NM: Undefined rotation
ROTATION: 0
REGEN_PERS: 7
REGEN_CNT: 1
THIN_PERS:
THIN_CNT: 0
ENTRIES: 1
PRE_THINS: 0
POST_THINS: 0
INTR_THINS: 0
MGMT_SEVR: 1.0
FREQUENCY: 10
MIN_ACRES: 0.563606092746
FIRST_HARV_AGE: None
0 Kudos
DominickAlfonso
New Contributor
Can anyone from this thread assist me? I am trying to do something similar to the original poster's question; however, my fields are tied to domains and I do not need coded values, but need their description values instead. I have already posted here about this.

Any help is appreciated, thanks!! 🙂
0 Kudos