6

I am trying to create a table of coded values so each textual description has a numerical attribute. The order of numbers is not important - they just need to be unique.

What I have so far is a python list of all unique values, so all I need to do now is add this to a table and create numbers. Is there a method in arcpy that will allow me to save my list as a table with an objectID?

  • If you are dealing in unique values, you should consider using a set instead of a list. – Richard Law Nov 12 '15 at 20:01
6

To do this you should use a combination of ArcPy and Python.

You can use Python's enumerate function to get a number for each item in your list.

You use arcpy.da.InsertCursor() to create and populate a new row for each item in the list.

6

An alternative method is to pass your list into a Structured Array and then convert that. When writing to a table it automatically puts in an ObjectID field.

Below is some sample code.

import arcpy,numpy

try:

    # List of unique values, note this is a
    # list of lists with each list holding a single value
    myList = [[1],[2],[3],[4],[99]]

    # Convert myList into a list of tuples,
    # this is the required input format of structured Array
    tupList = [tuple(row) for row in myList]

    # Convert to numpy array
    dt = numpy.dtype([('myValue',numpy.int32)])
    arr = numpy.array(tupList,dtype=dt)

    # Write out to dBase table
    sTable = r"C:\temp\test.dbf"
    arcpy.da.NumPyArrayToTable(arr,sTable)
except Exception as e:
    print str(e)

The output is a dBase table with an ObjectID field and an integer field called myValue with 5 rows of data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.