0

I have an attribute field with string data values (e.g. "P0001","C3492","00123"). I need to replace them so that they're all unique and begin with "W" followed by digits totaling 5 characters.

I thought perhaps ranking may work but not sure how to rank a string. The resulting output will look like ("W0001","W0002","W0003"). I prefer a Python solution in the field calculator but can also use VB.

closed as off-topic by PolyGeo Dec 7 '16 at 0:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Even though you have been a user of the site for over a year I note that you have not yet taken its 2-minute Tour which is designed to introduce all users to its protocols. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet (in this case your current expression and code block) in your question. There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely. – PolyGeo Dec 7 '16 at 0:57
3

If I understand your question correctly, I would suggest using a python generator in combination with an arcpy.da.UpdateCursor First you would define your generator function. Something like this:

def get_unique_identifier(n):
    num = 0
    for i in xrange(n):
        while num < n:
            yield 'W{0:04}'.format(num)
            num += 1

Then you can use an update cursor like this to update the proper field with the value generated by the function/generator above:

in_feature_class = r'C:\path_to_data\your_data.shp'
field_to_update = 'the_field'
number_of_records = arcpy.GetCount_management(in_feature_class)
unique_values = get_unique_identifier(number_of_records)
with arcpy.da.UpdateCursor(in_feature_class, field_to_update) as u_cur:
    for row in u_cur:
        row[0] = unique_values.next()
        u_cur.updateRow(row)

This is a quick go and there are certainly things that could be improved upon. If the order in which these fields are updated matters you may want to utilize an ORDER BY field in the sql_clause when creating your update cursor so that you update the values in the sorted order that you want. However this should get you on your way.

  • 2
    Instead of a function, you could also do for i,row in enumerate(u_cur):...u_cur.update('W{0:04}'.format(i)) -- Exact same thing and you can define the start value as well. – Paul Dec 7 '16 at 0:55

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