Trying to revise the placement of numbers and letters in a field part 2

693
3
08-31-2016 04:13 PM
jamesschoolar
New Contributor

I recently place a question on how to change the placement of a combination of digits, letter or digits and letters in the rows of a single field in a dBase file so that it follow a particular output format. I got many helpful responses and am now trying to take this to the next level. The digits and letters in each row are different lengths and do not follow any standard pattern. Here are three examples for hypothetical values: 12NESW3SESE    NWNW    1.  The first contains a combination of digits and letters, the second, letters only, and the third a single digit The desired modification follows a rule where starting at the left of the string, the first consecutive digits, if any, are to be placed after the first consecutive letters, if any, separated by a semicolon, and the next consecutive digits, if any, are to be place after the next consecutive letters, if any, separated by and semicolon.  A comma is placed between any modified set of combo sets of letters;digits. This patter continues regardless of the number of sets of digits or letters. So, the three rows above are changed to NESW;12,SESE;3    NWNW    ;1. I used a script provided by Darren Wiens in his response to my original post. His script works perfect. However, it uses a single quoted value for variable aa. I attempted to modify the script as posted below, so that it would change the values per arcpy.da.UpdateCursor and changing variable aa = row[0]. I have a couple of failures. First, the final list does not include the characters from list_2. Second, I receive a a runtime error "NameError: name 'cursor' is not defined".

I am attaching a small sample dbf table with the original values in the field "aliquot_pa" and the desired modifications shown in the field "format". I am wanting to update "aliquot_pa" to match "format".

Thanks all of you that responded before and any help with this is very much appreciated. Bob Schoolar

featureClass = "E:\\GLO_Conversion\\test.dbf"
fields = ('aliquot_pa')
with arcpy.da.UpdateCursor(featureClass,fields) as cursor:
    for row in cursor:
        aa = row[0]
        prev_type = aa[0].isdigit()
        list_1 = []
        cur_word = ''
        for char in aa:
            if char.isdigit() == prev_type:
                cur_word += char
        else:
            list_1.append(cur_word)
            cur_word = char
        prev_type = char.isdigit()
    list_1.append(cur_word)
    print list_1
    list_2 = []
    for i in range(0,len(list_1),14):
        list_2.append(list_1[i+1] + ';' + list_1)
    print list_2
    final_list = ','.join(list_2)
    print final_list
    cursor.updateRow(row)

    del cursor

    del row

0 Kudos
3 Replies
DanPatterson_Retired
MVP Esteemed Contributor
DarrenWiens2
MVP Honored Contributor

Don't delete the cursor inside the loop - you're still using it. In fact, I don't think you need to del cursor or row at all - 'with' does that for you. 

jamesschoolar
New Contributor

Thanks again Darren. I'm making good progress. Bob

0 Kudos