Please Help: Python Script to Automatically add Unique IDs to new features

850
5
10-12-2011 06:51 AM
ChristinaGnadinger
New Contributor II
I found a script on here, somewhere, and have been trying to edit it for my purposes. Right now I'm trying to test it out on a sample on my local computer to make sure it will work before attempting anything more.

Ultimately, I am trying to set up a script to run through the Scheduled Tasks that will go through a series of SDE datasets and assign unique IDs to the newly added features. I actually want it to assign the unique ID as a letter/number combination (i.e. FH0003001) but at the moment I can't get the simple number part to work in my favor.

(I'm coming from the VB side and still very green with Python.)


This is what I've been trying to use so far, however I am getting the error that says "return convertArcObjectToPythonObject(self._arc_object.UpdateRow(*gp_fixargs(args)))" RuntimeError: ERROR 999999: Error executing function

Anyway, any guidance you can offer both with this sample set and in moving forward to my ultimate goal would be greatly appreciated!


import arcpy
from arcpy import env
import string

env.workspace = "C:\\Users\\gisadmin\\Desktop\\TEST"

filter = '' 

cur = arcpy.UpdateCursor("C:\\Users\\gisadmin\\Desktop\\TEST\\FH_Test.shp", filter)

i = 0
for row in cur:
    i += 1
    row.FACILITY_I = str(i) #This is the label for the column you want to update, eg. OID

cur.updateRow(row)



As far as this: C:\\Users\\gisadmin\\Desktop\\TEST\\FH_Test.shp I've done it with \\ and with \ .. I read somewhere about the \t causing an issue but regardless it hasn't worked either way.
Tags (2)
0 Kudos
5 Replies
MathewCoyle
Frequent Contributor
I think your indentation might be off

import arcpy
from arcpy import env
import string

env.workspace = "C:\\Users\\gisadmin\\Desktop\\TEST"

filter = ''

cur = arcpy.UpdateCursor("C:\\Users\\gisadmin\\Desktop\\TEST\\FH_Test.shp", filter)

i = 0
for row in cur:
    i += 1
    row.FACILITY_I = str(i) #This is the label for the column you want to update, eg. OID
    cur.updateRow(row)
0 Kudos
ChristinaGnadinger
New Contributor II
That would be my luck .. thank you!


I think your indentation might be off

import arcpy
from arcpy import env
import string

env.workspace = "C:\\Users\\gisadmin\\Desktop\\TEST"

filter = ''

cur = arcpy.UpdateCursor("C:\\Users\\gisadmin\\Desktop\\TEST\\FH_Test.shp", filter)

i = 0
for row in cur:
    i += 1
    row.FACILITY_I = str(i) #This is the label for the column you want to update, eg. OID
    cur.updateRow(row)
0 Kudos
MarcNakleh
New Contributor III
As an additional note, Python has a built-in function for adding loop counters to iterations, enumerate()

So, you could even write your example as:
import arcpy
from arcpy import env
import string

env.workspace = "C:\\Users\\gisadmin\\Desktop\\TEST"
filter = ''
cur = arcpy.UpdateCursor("C:\\Users\\gisadmin\\Desktop\\TEST\\FH_Test.shp", filter)

for i, row in enumerate(cur):
    row.FACILITY_I = str(i) #This is the label for the column you want to update, eg. OID
    cur.updateRow(row)


Cheers,
0 Kudos
PeterPalacios
New Contributor
Christina,

Did you have success with your script?  I'm in need of exactly the same thing: a script to auto-generate unique IDs that are a letter+number combo.  If so, would you mind sharing your code and/or lessons learned?

peter[dot]palacios[at]yahoo[dot]com

Thanks in advance for your time and courtesy.
0 Kudos
RichardFairhurst
MVP Honored Contributor
Christina,

Did you have success with your script?  I'm in need of exactly the same thing: a script to auto-generate unique IDs that are a letter+number combo.  If so, would you mind sharing your code and/or lessons learned?

peter[dot]palacios[at]yahoo[dot]com

Thanks in advance for your time and courtesy.


See this post for the code that worked for her.
0 Kudos